tryolabs / norfair

Lightweight Python library for adding real-time multi-object tracking to any detector.
https://tryolabs.github.io/norfair/
BSD 3-Clause "New" or "Revised" License
2.42k stars 247 forks source link

How to use the object coordinate for tracking? #199

Closed dsnsabari closed 2 years ago

dsnsabari commented 2 years ago

When I checked the Yolo v7 demo codes . It uses a bounding box or centre point for tracking. I want to use the SSD object coordinates( xmin,ymin,xmax,ymax) for tracking. If I pass the coordinates in the below format, I am getting an error. The below box structure is (xmin,ymin,xmax,ymax). Could you help me fixing the issue

reference script: https://github.com/tryolabs/norfair/blob/master/demos/yolov7/src/demo.py

    boxes, scores, classes, num = SSDEfficienet(image)

    box_value = []
    score_value = []
    norfair_detections = []
    for row in range(len(boxes)):      

        if classes[row]=="person":

            width = boxes[row][2]- boxes[row][0]
            height = boxes[row][3]- boxes[row][1]

            box_value.append(np.array([
                    [boxes[row][0],boxes[row][1]]
            ,[boxes[row][2],boxes[row][3]]
            ]))
            score_value.append(np.array([scores[row],scores[row]]))
            norfair_detections.append(
                Detection(
                    points=box_value, scores=score_value, label=int(2)
                )
            )
exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Person_Tracking/test.py", line 83, in <module>
    tracked_objects = tracker.update(detections=norfair_detections)

  File "C:\Users\SABARI\Anaconda3\lib\site-packages\norfair\tracker.py", line 236, in update
    self.abs_to_rel,

  File "C:\Users\SABARI\Anaconda3\lib\site-packages\norfair\tracker.py", line 449, in __init__
    exit()

NameError: name 'exit' is not defined
facundo-lezama commented 2 years ago

Hi @dsnsabari!

Please note that Detection expects points and scores as np.ndarray and you are actually using lists of np.ndarray instead.

Based on your code, something like this should work:

boxes, scores, classes, num = SSDEfficienet(image)

norfair_detections = []
for row in range(len(boxes)):      

    if classes[row]=="person":

        box_value = np.array(
            [
                [boxes[row][0], boxes[row][1]],
                [boxes[row][2], boxes[row][3]],
            ]
        )
        score_value = np.array(
            [scores[row], scores[row]]
        )
        norfair_detections.append(
            Detection(
                points=box_value, scores=score_value, label=int(2)
            )
        )

You can also check the documentation for more specific details on the Detection class.

dsnsabari commented 2 years ago

@facundo-lezama Thank you. It is working now.