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.41k stars 247 forks source link

Using .onnx files with Yolov7 in Norfair #237

Closed moooises closed 1 year ago

moooises commented 1 year ago

Describe the situation you are working on

Is there any way to use .onnx files in YoloV7? I have seen some examples using onnxruntime, but the class YoloV7 made by them is quite different from the one in the demos. So I'm not sure if this option will work with Norfair.

Describe what is it that you need help with I need help to use .onnx files with Yolov7 in Norfair.

Additional context It would be helpful, because at the moment there aren't almost any optimization tool for Yolov7 file, but for onnx files there are quite a lot and can even be used with TensorRT.

DiegoFernandezC commented 1 year ago

Hi @moooises!

It is possible to run any detector with Norfair, the difference between that example and our demo is the yolo_detections_to_norfair_detections function to map the model output with the input required by Norfair (List[Detection]).

This line defines the model output, you can compare this output with the one on our demo and create this function to your case.

Probably looks similar to the following. This is an intuition of this function but check the types returned by your model to prevent any bugs.

def yolo_detections_to_norfair_detections(yolo_detections, scores, ids) -> List[Detection]:
    """convert detections_as_xyxy to norfair detections"""
    norfair_detections: List[Detection] = []
    for yolo_detection, score, class_id in zip(yolo_detections, scores, ids):
        bbox = np.array(
            [
                [yolo_detection[0], yolo_detection[1]],
                [yolo_detection[2], yolo_detection[3]],
            ]
        )
        scores = np.array(
            [score[0], score[1]]
        )
        norfair_detections.append(
            Detection(
                points=bbox, scores=scores, label=int(class_id)
            )
        )

    return norfair_detections

After that, the situation is similar to our demo and the Norfair code will be similar.

Ask again any questions you have. Greetings!

moooises commented 1 year ago

Hi, Thank you so much, I was able to make it work. The code you provied was very helpful. The only thing I had to change was the score, because in this case is only a float, not an array.

I had to set to True the _officialnms variable to display the result properly, but the rest works perfect.