OpenCV doesn't show Yolov8 output

I’m trying to detect gamepieces with the code at the bottom; it simply takes the frame from camera, detects the object in frame and shows it.

import cv2
from roboflow import Roboflow
rf = Roboflow(api_key="9QtTqKO1kLdvctLfx0YR")
project = rf.workspace().project("frc-charged-up-game-pieces")
model = project.version(7).model

capture = cv2.VideoCapture(0)

while True:
    capture = cv2.VideoCapture(0)
    ret, frame=capture.read()
    a = model.predict(frame , confidence=40, overlap=30).json()
    cv2.imshow("Webcam",a)

    cv2.waitKey(100)

capture.release()
cv2.destroyAllWindows()

But it gives this error :

Traceback (most recent call last):
  File "C:\Users\narog\Desktop\opencv\main.py", line 13, in <module>
    cv2.imshow("Webcam",a)
cv2.error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - mat is not a numpy array, neither a scalar
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'mat'
>  - Expected Ptr<cv::UMat> for argument 'mat'

How can I fix this, or there are any other ways to do object detection?

From a cursory google, model.predict does not return an opencv image- it returns a struct containing prediction data.

3 Likes

Oh, yes, I completely forgot about that. Tried it with a sudden inspiration that came in the morning and wondered why it didn’t work. Now I understand why, lack of sleeping.

You might want to remove that first line after while True. You’re opening up the video capture on startup and then again on every loop. I could see that eventually being a problem.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.