RizwanMunawar / yolov7-object-tracking

YOLOv7 Object Tracking Using PyTorch, OpenCV and Sort Tracking
GNU General Public License v3.0
557 stars 168 forks source link

SORT implementation and issue with videos with a large number of unique tracks #37

Open C-van-der-Laan opened 1 year ago

C-van-der-Laan commented 1 year ago

Hi,

I've been using your SORT/yolov7 implementation, thanks for your work! There were 2 issues I ran in to with your current implementation.

The first is that it's mentioned in sort.py itself that update should be called even if there are no detections in the image. This is important for SORT to work properly if you have some frames where the detection is failing. https://github.com/RizwanMunawar/yolov7-object-tracking/blob/936097eb8f24b32e2b5188d6d0a34c01a37baa57/sort.py#L222-L232

However this is not actually done in the current implementation. In crowded scenes there will rarely be any frames with no detections, so this isn't a big issue, but for my use case I had a large number of frames with no detections.

This can easily be corrected by inserting a

else:
     dets_to_sort = np.empty((0,6))
     tracked_dets = sort_tracker.update(dets_to_sort)

here https://github.com/RizwanMunawar/yolov7-object-tracking/blob/936097eb8f24b32e2b5188d6d0a34c01a37baa57/detect_and_track.py#L238

Another issue I ran in to is that the current implementation will fail if you have an input that results in more than 5005 unique tracks and you're using --colored-trk.

I fixed this by changing this from 5005 to 5003, since that is a prime number. https://github.com/RizwanMunawar/yolov7-object-tracking/blob/936097eb8f24b32e2b5188d6d0a34c01a37baa57/detect_and_track.py#L78

then just changing this

https://github.com/RizwanMunawar/yolov7-object-tracking/blob/936097eb8f24b32e2b5188d6d0a34c01a37baa57/detect_and_track.py#L207

to rand_color_list[track.id % len(rand_color_list)], thickness=2), since after that many detections it won't really matter anyway that the same colours are picked again. Not the most elegant solution, but it works :).

RizwanMunawar commented 1 year ago

Thanks @C-van-der-Laan! Can you please send me a pull request, you can then become a contributor to the repository. Otherwise, I will do changes on my own. 👍🏻

C-van-der-Laan commented 1 year ago

Alright done! Realized that since the update method had a default argument the dets_to_sort = np.empty((0,6)) wasn't even needed.