多目标跟踪(2)解决问题:OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:40

1.问题出现背景:

在ByteTrack代码中添加了一个deepsort的跟踪算法,也就是实现YOLOx的检测器+deepsort的组合,可是在使用MOT17评测的过程中有些序列可以跑完,但是有些序列跑到中间就会报错
网上有很多文章指出是路径的问题,但是路径并没有出错,还是出现了这样的问题

2.问题报错函数:

(1)报错提示如下:

Loading weights from C:/Users/zxc/Desktop/ByteTrack-main/deep_sort/deep/checkpoint/ckpt.t7... Done!
Traceback (most recent call last):
  File "C:/Users/zxc/Desktop/ByteTrack-main/tools/demo_track.py", line 558, in 
    main(exp, args)
  File "C:/Users/zxc/Desktop/ByteTrack-main/tools/demo_track.py", line 549, in main
    image_demo_deepsort2(predictor, vis_folder, current_time, args)
  File "C:/Users/zxc/Desktop/ByteTrack-main/tools/demo_track.py", line 378, in image_demo_deepsort2
    outputs = deepsort.update((torch.Tensor(bbox_xywh)), (torch.Tensor(confs)), img_info['raw_img'])
  File "C:\Users\zxc\Desktop\ByteTrack-main\deep_sort\deep_sort.py", line 29, in update
    features = self._get_features(bbox_xywh, ori_img)
  File "C:\Users\zxc\Desktop\ByteTrack-main\deep_sort\deep_sort.py", line 99, in _get_features
    features = self.extractor(im_crops)
  File "C:\Users\zxc\Desktop\ByteTrack-main\deep_sort\deep\feature_extractor.py", line 58, in __call__
    im_batch = self._preprocess(im_crops)
  File "C:\Users\zxc\Desktop\ByteTrack-main\deep_sort\deep\feature_extractor.py", line 42, in _preprocess
    im=_resize(im, self.size)
  File "C:\Users\zxc\Desktop\ByteTrack-main\deep_sort\deep\feature_extractor.py", line 38, in _resize
    return cv2.resize(im.astype(np.float32)/255., size)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

(2)找到报错的函数位置:
位于\feature_extractor.py文件中,在deepsort的特征提取中的resize函数

        def _resize(im, size):
 
                return cv2.resize(im.astype(np.float32)/255., size)
 
        im_batch = torch.cat([self.norm(_resize(im, self.size)).unsqueeze(0) for im in im_crops], dim=0).float()
        return im_batch

3.问题解决办法:

通过报错的提示:

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

可知可能是在resize函数处有空帧的情况,因此解决的办法是在使用resize前先判断一下是否为空帧,代码修改如下:

  def _resize(im, size):
            try:
                return cv2.resize(im.astype(np.float32)/255., size)
            except:
                pass

这是网上的其他人分享的方法,但是我在使用后并没有解决问题,可以看一下resize的下一步需要什么处理

im_batch = torch.cat([self.norm(_resize(im, self.size)).unsqueeze(0) for im in im_crops], dim=0).float()

可以看到,每一个img在经过resize后,还需要经过self.norm()函数的处理,因为我们上一步将空帧跳过了,所以它空帧经过resize之后的结果还是为空,所以在这一步仍然需要判断,代码修改如下:

    def _preprocess(self, im_crops):
        """
        TODO:
            1. to float with scale from 0 to 1
            2. resize to (64, 128) as Market1501 dataset did
            3. concatenate to a numpy array
            3. to torch Tensor
            4. normalize
        """
        def _resize(im, size):
            try:
                return cv2.resize(im.astype(np.float32)/255., size)
            except:
                pass
        img=[]
        for im in im_crops:
            im=_resize(im, self.size)

            if im is not None:
               im=self.norm(im).unsqueeze(0)
               img.append(im)


        im_batch=torch.cat(img, dim=0).float()
        #im_batch = torch.cat([self.norm(_resize(im, self.size)).unsqueeze(0) for im in im_crops], dim=0).float()
        return im_batch

将原函数注释掉,并按步骤拆开,对im进行判断,如果不是空,进行norm()处理,并将成功处理的im加入到img列表管理,这是img中就没有空的结果了

问题得到解决!!!

你可能感兴趣的:(python,opencv,目标跟踪,深度学习)