Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inference a onnx yolov9 model? #141

Closed
KyriakosChris opened this issue Mar 1, 2024 · 3 comments
Closed

How to inference a onnx yolov9 model? #141

KyriakosChris opened this issue Mar 1, 2024 · 3 comments

Comments

@KyriakosChris
Copy link

I found how to inference a trt yolov9 model using this detect function:

def detect(self, bgr_img):   
    ## Padded resize
    h, w, _ = bgr_img.shape
    scale = min(self.imgsz[0]/w, self.imgsz[1]/h)
    inp = np.zeros((self.imgsz[1], self.imgsz[0], 3), dtype = np.float32)
    nh = int(scale * h)
    nw = int(scale * w)
    inp[: nh, :nw, :] = cv2.resize(cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB), (nw, nh))
    inp = inp.astype('float32') / 255.0  # 0 - 255 to 0.0 - 1.0
    inp = np.expand_dims(inp.transpose(2, 0, 1), 0)

    ## Inference
    t1 = time.time()
    num_detection, nmsed_bboxes, nmsed_scores, nmsed_classes = self.model.run(inp)
    t2 = time.time()

    ## Apply NMS
    num_detection = num_detection[0][0]
    nmsed_bboxes  = nmsed_bboxes[0]
    nmsed_scores  = nmsed_scores[0]
    nmsed_classes  = nmsed_classes[0]
    print('Detected {} object(s)'.format(num_detection))
    # Rescale boxes from img_size to im0 size
    _, _, height, width = inp.shape
    h, w, _ = bgr_img.shape
    nmsed_bboxes[:, 0] /= scale
    nmsed_bboxes[:, 1] /= scale
    nmsed_bboxes[:, 2] /= scale
    nmsed_bboxes[:, 3] /= scale
    visualize_img = bgr_img.copy()
    for ix in range(num_detection):       # x1, y1, x2, y2 in pixel format
        cls = int(nmsed_classes[ix])
        label = '%s %.2f' % (self.names[cls], nmsed_scores[ix])
        x1, y1, x2, y2 = nmsed_bboxes[ix]

        cv2.rectangle(visualize_img, (int(x1), int(y1)), (int(x2), int(y2)), self.colors[int(cls)], 2)
        cv2.putText(visualize_img, label, (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 1, self.colors[int(cls)], 2, cv2.LINE_AA)

    cv2.imwrite('result.jpg', visualize_img)
    return `visualize_img`

But I can't find out how to do the same for an ONNX model. Has someone already made something like this, or should I do it and post it? Thank you.

@jdiaz97
Copy link

jdiaz97 commented Mar 1, 2024

I have an example in Julia

https://github.com/jdiaz97/yolov9-in-julia/

Applying NMS is needed

@WongKinYiu
Copy link
Owner

More ONNX examples: https://github.com/WongKinYiu/yolov9?tab=readme-ov-file#useful-links

Added Julia example to readme.

@yxl502
Copy link

yxl502 commented Mar 8, 2024

https://zhuanlan.zhihu.com/p/683979999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants