<jupyter_start><jupyter_text>Person detection set paths<jupyter_code>import sys
sys.path.append("../") # go to parent dir
import os
import glob
import argparse
import pickle
import cv2
import numpy as np
from Yolo_v2_pytorch.src.utils import *
from Yolo_v2_pytorch.src.yolo_net import Yolo
from PIL import Image
import time
import matplotlib.pyplot as plt

MissOh_CLASSES = ['person']
def get_args():
    parser = argparse.ArgumentParser(
        "You Only Look Once: Unified, Real-Time Object Detection")
    parser.add_argument("--image_size",
                        type=int, default=448,
                        help="The common width and height for all images")
    parser.add_argument("--conf_threshold",
                        type=float, default=0.35)
    parser.add_argument("--nms_threshold",
                        type=float, default=0.5)
    parser.add_argument("--pre_trained_model_type",
                        type=str, choices=["model", "params"],
                        default="model")
    parser.add_argument("--pre_trained_model_path",
                        type=str,
                        default="../checkpoint/anotherMissOh.pth")
    parser.add_argument("--data_path_test",
                        type=str,
                        default="../Yolo_v2_pytorch/missoh_test/",
                        help="the root folder of dataset")
    args = parser.parse_args(args=[]) # for jupyter 
    return args
opt = get_args()
print(opt)<jupyter_output>Namespace(conf_threshold=0.35, data_path_test='../Yolo_v2_pytorch/missoh_test/', image_size=448, nms_threshold=0.5, pre_trained_model_path='../checkpoint/anotherMissOh.pth', pre_trained_model_type='model')<jupyter_text>load a model<jupyter_code>global colors
if torch.cuda.is_available():
    if opt.pre_trained_model_type == "model":
        model1 = torch.load(opt.pre_trained_model_path)
    else:
        model1 = Yolo(1)
        model1.load_state_dict(torch.load(opt.pre_trained_model_path))

colors = pickle.load(open("../Yolo_v2_pytorch/src/pallete", "rb"))

model1.eval()
img_list = sorted(glob.glob(os.path.join(opt.data_path_test, '*.jpg')))

print(img_list)

save_dir = '../Yolo_v2_pytorch/anotherMissOh_Test_Result/'<jupyter_output>YoloD(
  (stage1_conv1): Sequential(
    (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): LeakyReLU(negative_slope=0.1, inplace)
    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (stage1_conv2): Sequential(
    (0): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): LeakyReLU(negative_slope=0.1, inplace)
    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (stage1_conv3): Sequential(
    (0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): LeakyReLU(negative_slope=0.1, inplace)
  )
  (stage1_conv4): Sequential(
    (0): Conv2d(128,[...]<jupyter_text>show the person detection results<jupyter_code>for idx,idx_img in enumerate(img_list):
    image = Image.open(idx_img)
    np_img = np.asarray(image)
    image = cv2.cvtColor(np.float32(np_img), cv2.COLOR_RGB2BGR)
    height, width = image.shape[:2]
    image = cv2.resize(image, (opt.image_size, opt.image_size))
    image = np.transpose(np.array(image, dtype=np.float32), (2, 0, 1))
    image = image[None, :, :, :]
    width_ratio = float(opt.image_size) / width
    height_ratio = float(opt.image_size) / height
    
    data = Variable(torch.FloatTensor(image))

    if torch.cuda.is_available():
        data = data.cuda()

    with torch.no_grad():
        logits = model1(data)
        predictions = post_processing(logits, opt.image_size, MissOh_CLASSES, model1.anchors, opt.conf_threshold,
                                      opt.nms_threshold)
    print(len(predictions))
    output_image = cv2.cvtColor(np.float32(np_img), cv2.COLOR_RGB2BGR)
    if len(predictions) != 0:
        predictions = predictions[0]
        for pred in predictions:
            xmin = int(max(pred[0] / width_ratio, 0))
            ymin = int(max(pred[1] / height_ratio, 0))
            xmax = int(min((pred[0] + pred[2]) / width_ratio, width))
            ymax = int(min((pred[1] + pred[3]) / height_ratio, height))
            color = colors[MissOh_CLASSES.index(pred[5])]

            cv2.rectangle(output_image, (xmin, ymin), (xmax, ymax), color, 2)
            text_size = cv2.getTextSize(pred[5] + ' : %.2f' % pred[4], cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
            cv2.rectangle(output_image, (xmin, ymin), (xmin + text_size[0] + 3, ymin + text_size[1] + 4),
                          color, -1)
            cv2.putText(
                output_image, pred[5] + ' : %.2f' % pred[4],
                (xmin, ymin + text_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1,
                (255, 255, 255), 1)
        cv2.imwrite(save_dir + "entire_{}.png".format(idx), output_image)
    
    print("{} + entire_{}.png".format(save_dir, idx))
    output_image = np.uint8(cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB))
    plt.imshow(output_image)
    plt.show()<jupyter_output>0
../Yolo_v2_pytorch/anotherMissOh_Test_Result/ + entire_0.png
{'fraction_non_alphanumeric': 0.14358974358974358, 'fraction_numerical': 0.03626373626373627, 'mean_word_length': 3.121509433962264, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '104483', 'license': 'MIT', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1972, 'n_words': 343, 'path': 'jupyter/PersonDetection.ipynb', 'repo_name': 'ihaeyong/drama-graph', 'size': '5460'}