Reputation: 1
Im trying to make a object detection program using my webcam but cant get it to work
This is my code
import cv2 as cv
import base64
import numpy as np
import requests
ROBOFLOW_API_KEY = "*********"
ROBOFLOW_MODEL = "chess-full-ddsxf" # eg xx-xxxx--#
ROBOFLOW_SIZE = 416
upload_url = "".join([
"https://detect.roboflow.com/",
ROBOFLOW_MODEL,
"?access_token=",
ROBOFLOW_API_KEY,
"&format=image",
"&stroke=5"
])
video = cv.VideoCapture(1)
# Check if camera opened successfully
if (video.isOpened()):
print("successfully opening video stream or file")
# Infer via the Roboflow Infer API and return the result
def infer():
# Get the current image from the webcam
ret, frame = video.read()
# Resize (while maintaining the aspect ratio)
# to improve speed and save bandwidth
height, width, channels = frame.shape
scale = ROBOFLOW_SIZE / max(height, width)
img = cv.resize(frame, (round(scale * width), round(scale * height)))
# Encode image to base64 string
retval, buffer = cv.imencode('.jpg', img)
img_str = base64.b64encode(buffer)
# Get prediction from Roboflow Infer API
resp = requests.post(upload_url, data=img_str, headers={
"Content-Type": "application/x-www-form-urlencoded"
}, stream=True).raw
# Parse result image
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv.imdecode(image, cv.IMREAD_COLOR)
return image
# Main loop; infers sequentially until you press "q"
while video.isOpened():
# On "q" keypress, exit
key = cv.waitKey(5)
if key == ord("q"):
break
# Synchronously get a prediction from the Roboflow Infer API
image = infer()
# And display the inference results
cv.imshow('image', image)
# Release resources when finished
video.release()
cv.destroyAllWindows()
Error message: successfully opening video stream or file
Traceback (most recent call last): File "C:\Users\Administrator\Documents\Atom\testingCode\webcamRoboflow\infer.py", line 64, in cv.imshow('image', image) 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'
[Finished in 22.357s]
Upvotes: 0
Views: 981
Reputation: 6494
Looks like you forgot to include the version of your model that you want to infer against so you're hitting an endpoint that isn't defined on the server.
ROBOFLOW_MODEL = "chess-full-ddsxf" # eg xx-xxxx--#
This should be eg
ROBOFLOW_MODEL = "chess-full-ddsxf/1" # eg xx-xxxx--#
If you trained your model on the first generated version of your dataset.
Upvotes: 1