How to display and read a live camera video in Python OpenCV

Viewed 283

I've been working on a code from the Roboflow team that is suppose to display a live video capture and give it's predictions from the model that I trained, but I encounter an error.

Get webcam interface via opencv-python

video = cv2.VideoCapture(0)

Get the current image from the webcam

ret, img = video.read()

// Synchronously get a prediction from the Roboflow Infer API

 image = infer()

// And display the inference results

 cv2.imshow('image', image)

However I got this error :

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I've seen online saying that my image file location is wrong and that's why I get this error, but I'm confused as it is suppose to be showing a live camera constantly instead of just a still image.

I'm new to programming any help would be appreciated, thanks.

Roboflow code for webcam

1 Answers

As Gunner Stone mentioned in their comment, this likely means that the API is not returning an image (it’s probably giving some sort of error message instead).

I’d recommend backing up a tick and making sure your parameters (Eg model ID, api key) are correct by trying to get a prediction for a single image. Then once you’ve verified that’s working come back to the video inference.

Alternatively, I think you might be able to do

print(resp.read())

At line 51 in the linked code, right after the requests.post bit to see if the API is returning a specific error message.

Related