【发布时间】:2021-09-27 12:01:24
【问题描述】:
我正在尝试对视频进行对象检测,我使用了 YoloV5S。有两种识别对象的方法。
1) 首先是从 torch.hub.load() 加载模型
2) 第二个来自 python detect.py
我从两种方法而不是相同的结果在同一视频上得到不同的结果。如果我只是使用
!python detect.py --weights 'yolov5s.pt' --conf-thres=0.25 --source /home/hamza/Desktop/House_Video.mp4
我得到了检测到不同计数对象的结果,例如在这个视频的最后一帧中,我得到了 3 把椅子、1 张餐桌和 1 个时钟,但是如果我使用下面的代码 (torch.hub.加载) 。我要 3 把椅子和 1 张餐桌。所有帧都一样,此方法中缺少一些对象,第一种方法具有。
我的代码:
import cv2, torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
model.conf = 0.25
list_of_original_frames = []
list_of_detected_classes_in_all_frames = []
detected_classes = []
check_dict = {}
def detect_video(video_path):
cap = cv2.VideoCapture(video_path)
while (cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
list_of_original_frames.append(frame)
cap.release()
cv2.destroyAllWindows()
for file in list_of_original_frames:
results = model(file)
new_df = results.pandas()
new_result = new_df.xywh[0]['name']
list_of_detected_classes_in_all_frames.append(new_result.tolist())
for outer_loop in list_of_detected_classes_in_all_frames:
for inner_loop in outer_loop:
if inner_loop:
detected_classes.append(inner_loop)
for iterate in detected_classes:
if iterate not in check_dict:
check_dict[iterate] = 1
else:
check_dict[iterate] = check_dict[iterate] + 1
return check_dict
video_path = '/home/hamza/Desktop/House_Video.mp4'
check_dict = detect_video(video_path)
我做错了哪一部分?
【问题讨论】:
标签: python list computer-vision object-detection yolo