Hey there, I want to say sorry beforehand that I ask this (possibly) stupid question, but I am new to object detection and all it's nooks and crevices. I wanted to ask you if you could help me with this problem? I have modified the webcam_demo.py
file so that it loads the yolov3-modanet.cfg
model, the modanet.names
classes, and yolov3.weights
weights. I have followed the tutorial in this video: https://www.youtube.com/watch?v=yWwzFnAnrLM&ab_channel=AlSangam but to no avail, perhaps the weight file is different?
from __future__ import division
from yolo.utils.models import *
from yolo.utils.utils import *
from yolo.utils.datasets import *
from predictors.YOLOv3 import YOLOv3Predictor
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
import cv2
from yolo.utils.utils2 import load_model, cv_img_to_tensor
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# params = \
# {
# "model_def" : "yolo/df2cfg/yolov3-df2.cfg",
# "weights_path" : "yolo/weights/yolov3.weights",
# "class_path":"yolo/df2cfg/df2.names",
# "conf_thres" : 0.25,
# "nms_thres" :0.4,
# "img_size" : 416,
# "device" : device
# }
params = \
{
"model_def" : "yolo/modanetcfg/yolov3-modanet.cfg",
"weights_path" : "yolo/weights/yolov3.weights",
"class_path":"yolo/modanetcfg/modanet.names",
"conf_thres" : 0.25,
"nms_thres" :0.4,
"img_size" : 416,
"device" : device
}
classes = load_classes(params['class_path'])
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
cmap = plt.get_cmap("tab20b")
colors = np.array([cmap(i) for i in np.linspace(0, 1, 20)])
np.random.shuffle(colors)
yolov3 = YOLOv3Predictor(params=params)
model = yolov3.load_model()
cap = cv2.VideoCapture(0)
while(True):
# img = cv2.imread('tests/amazon.jpg')
_, frame = cap.read()
img = frame.copy()
x , _,_ = cv_img_to_tensor(img)
x.to(device)
# Get detections
with torch.no_grad():
input_img = Variable(x.type(Tensor))
detections = model(input_img)
detections = non_max_suppression(detections, params['conf_thres'], params['nms_thres'])
if detections[0] is not None:
# Rescale boxes to original image
detections = rescale_boxes(detections[0], params['img_size'], img.shape[:2])
unique_labels = detections[:, -1].cpu().unique()
n_cls_preds = len(unique_labels)
# bbox_colors = random.sample(colors, n_cls_preds , seed)
bbox_colors = colors[:n_cls_preds]
for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
print("\t+ Label: %s, Conf: %.5f" % (classes[int(cls_pred)], cls_conf.item()))
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
color = tuple(c*255 for c in color)
color = (color[2],color[1],color[0])
cv2.rectangle(frame, (x1,y1), (x2,y2), color=color, thickness=3)
# cv2.rectangle(img, (x1,y1), (x2,y2), color=color, thickness=3)
# print(int(cls_pred))
font = cv2.FONT_HERSHEY_SIMPLEX
text = "%s conf: %.3f" % (classes[int(cls_pred)] ,cls_conf.item())
cv2.rectangle(frame,(x1-2,y1-25), (int(x1 + 8.5*len(text)),y1) , color=color, thickness=-1)
cv2.putText(frame,text,(x1,y1-5), font, 0.5, color=(255,255,255), thickness=1, lineType=cv2.LINE_AA)
# cv2.rectangle(img,(x1-2,y1-25), (int(x1 + 8.5*len(text)),y1) , color=color, thickness=-1)
# cv2.putText(img,text,(x1,y1-5), font, 0.5, color=(255,255,255), thickness=1, lineType=cv2.LINE_AA)
cv2.imshow('Detections',frame)
# cv2.imshow('Detections',img)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
I have tested the model with my webcam and the test images but to no avail, no bounding boxes are created and the model does not seem to detect anything at all! If I change the model to darknet53.conv.74
weights file, there are bounding boxes everywhere! Like so:
Can you give me a hint or a solution to this problem, or the thing that might went wrong? Thank you so much! I need this for my personal research :( A timely response will be appreciated. This GitHub Issue is my last hope in finishing my research on time...
Thank you so much beforehand, for your possible help and this very helpful repository! ๐