2

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

Michael
  • 29
  • 3
  • That error typically means that whatever image you are trying to display is actually not there. Because of this, its width/height are both probably 0 (meaning no image). I'd double check your infer() function and make sure you are actually passed back an image. – Gunner Stone Jul 28 '22 at 04:57
  • duplicate of [OpenCV Error: (-215)size.width>0 && size.height>0 in function imshow](https://stackoverflow.com/questions/27953069/opencv-error-215size-width0-size-height0-in-function-imshow) – Yunus Temurlenk Jul 28 '22 at 06:24
  • @YunusTemurlenk - not a duplicate as the cause is almost certainly different. – Brad Dwyer Jul 28 '22 at 11:06
  • still thinking its duplicate – Yunus Temurlenk Jul 28 '22 at 11:10
  • @YunusTemurlenk I changed that but that didn't solve the problem, I know 0 is for webcam and 1 is for usb camera etc. , I still get the same error – Michael Jul 29 '22 at 04:30

1 Answers1

0

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.

Brad Dwyer
  • 6,305
  • 8
  • 48
  • 68