问题:
在使用Yolov5+DeepSort目标跟踪代码时,开发qt界面调用摄像头,
因为摄像头或视频里检测不到目标对象,程序报错:
报错信息:
File ".\deep_sort\deep_sort.py", line 32, in update
bbox_tlwh = self._xywh_to_tlwh(bbox_xywh)
File ".\deep_sort\deep_sort.py", line 55, in _xywh_to_tlwh
bbox_tlwh[:, 0] = bbox_xywh[:, 0] - bbox_xywh[:, 2]/2.
IndexError: too many indices for tensor of dimension 1
进程已结束,退出代码-1073740791 (0xC0000409)
解决办法 :
在执行下列两行代码之前
bbox_tlwh[:, 0] = bbox_xywh[:, 0] - bbox_xywh[:, 2]/2.
bbox_tlwh[:, 1] = bbox_xywh[:, 1] - bbox_xywh[:, 3]/2.
添加一行空判断语句,例如:
if len(bbox xywh) > 0:
即:
if len(bbox_xywh) > 0:
bbox_tlwh[:, 0] = bbox_xywh[:, 0] - bbox_xywh[:, 2]/2.
bbox_tlwh[:, 1] = bbox_xywh[:, 1] - bbox_xywh[:, 3]/2.
这样就可以在检测不到目标对象时避免出现 Index Error 错误,程序不会中止;
当 bbox xywh 数组中没有元素时,直接跳过这两行代码即可。
如果出现新的报错,比如:
********************************,in show_video_frame
self.out.write(frame)
AttributeError: 'NoneType' object has no attribute 'write'
可以使用同样的方法,在报错位置:
self.out.write(frame)
前面添加空判断:
if self.out is not None:
即:
videoWriter.write(frame)
if self.out is not None:
self.out.write(frame)
成功解决!