levan92 / deep_sort_realtime

A really more real-time adaptation of deep sort
MIT License
166 stars 51 forks source link

Implementation of Deep sort with yolo as an object detection #45

Closed TestPrab closed 1 year ago

TestPrab commented 1 year ago
# %%
import cv2
from deep_sort_realtime.deepsort_tracker import DeepSort

# %%
from ultralytics import YOLO

# %%
cap = cv2.VideoCapture('840_iSXIa0hE8Ek_seg.mp4')

# Read the first frame
ret, frame = cap.read()

# %%
model=YOLO('yolov8n.pt')

# %%
results=model(frame)

# %%
results[0].boxes

# %%
import numpy as np

# %%
answers=results[0].to('cpu')

# %%
boxes = answers.boxes
bbs=[]
for box in boxes[2]:
    temp_answer={}
    pred_conf = float(box.conf)
    # print(int(box.cls))
    bbox = np.array(box.xywh[0])
    x1, y1, x2, y2 = float(bbox[0]), float(bbox[1]), float(bbox[2]), float(bbox[3])
    bbs.append(([x1,y1,x2,y2],pred_conf,int(box.cls)))
    # print(x1,y1,x2,y2)

# %%
bbs

# %%
tracker = DeepSort(max_age=2,n_init=1,embedder="clip_ViT-B/16")

# %%
tracks=tracker.update_tracks(bbs,frame=frame)

# %%
for track in tracks:
    print(track.is_confirmed())
    if not track.is_confirmed():
        print("yo")
        continue
    track_id = track.track_id
    print(track_id)
    ltrb = track.to_ltrb()
    tlwh = track.to_tlwh()
    tlbr = track.to_tlbr()
    xyah = track.to_xyah()
    print(ltrb)

# %%

I was able to get the bounding boxes properly, and even put them in the format required yet I keep getting track.is_confirmed() as false, is there something that I am missing ?

raulperezalejo commented 1 year ago

I am having the same problem, no confirmation at all. I have tried to change the DeepSort parameters, but nothing changed.

levan92 commented 1 year ago

A track needs to be associated to a detection n_init times (default n_init=3), meaning you need to go through at least 3 frames of your video first.

raulperezalejo commented 1 year ago

Yes, my video is 30 fps, the code is pretty much the same as above with the exception that I am using the default embedder. I have played around with this three arguments with no results max_iou_distance=0.9, n_init=3, max_age=30. It is a Tennis game, nothing super complicated from my point of view. And I have waited for more than three frames.

Detections are made perfectly with YOLO8, I can filter through person class, but confirmation never reach.

raulperezalejo commented 1 year ago

Hello, After going back into my code, I found that I had an implementation error, I correct it and now it is working as a charm. If you have a similar problem described here, Id recommend taking a brake and looking at your code again.

levan92 commented 1 year ago

Great, thanks for your input @raulperezalejo!

salahmak commented 3 months ago

@raulperezalejo can you please inform us what "implementation error" you had?