我试着用我的摄像头制作一个目标检测程序,但却无法工作
这是我的密码
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()错误消息:成功打开视频流或文件
追溯(最近一次调用):文件"C:\Users\Administrator\Documents\Atom\testingCode\webcamRoboflow\infer.py",第64行,在cv.imshow(“图像”,图像)cv2中。错误: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967:错误:(-215:断言失败) size.width>0 & size.height>0中的函数'cv::imshow‘
在22.357秒内完成
发布于 2022-09-24 00:02:26
看起来,您忘记了包含您想要推断的模型的版本,因此您到达了服务器上未定义的端点。
ROBOFLOW_MODEL = "chess-full-ddsxf" # eg xx-xxxx--#这应该是
ROBOFLOW_MODEL = "chess-full-ddsxf/1" # eg xx-xxxx--#如果您将模型训练为数据集的第一个生成版本。
https://stackoverflow.com/questions/73826984
复制相似问题