成功解决在目标跟踪时检测不到物体:IndexError: too many indices for tensor of dimension 1

问题:

在使用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)

成功解决!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前方|

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值