将网络摄像头与yolov5型号一起使用
我试图举办示例,但是我无法成功,因为执行命令行(image = cv2.imdecode(image,cv2.imread_color))时,代码正在返回无值。
- 代码:
def infer():
# Get the current image from the webcam
ret, img = video.read()
# Resize (while maintaining the aspect ratio) to improve speed and save bandwidth
height, width, channels = img.shape
scale = ROBOFLOW_SIZE / max(height, width)
img = cv2.resize(img, (round(scale * width), round(scale * height)))
# Encode image to base64 string
retval, buffer = cv2.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 = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
始终在IMDecode映像为数组之前,并非没有,Bellow显示了一个调试示例。
- 但是
resp: <urllib3.response.HTTPResponse object at 0x0000015055DE7070>
image: array([ 11, 214, 13, ..., 170, 1, 3], dtype=uint8)
,当我运行cv2.imdecode(image,cv2.imread_color)时),我会获得图像的无值。
3. error
Exception has occurred: error OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:816:
error: (-215:Assertion failed) !buf.empty() in function 'cv::imdecode_'
File "C:\Users\diego\codes\Webcam\infer-simple.py", line 48, in infer
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
File "C:\Users\diego\codes\Webcam\infer-simple.py", line 63, in <module>
image = infer()
我根据 https://blog.roboboflow.com/python---网络摄像头。
我应用了在互联网上找到的解决方案,还重新安装了所有必要的软件包,但没有任何效果。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是从相机中获得图像,而是从服务器获得图像时。
您必须在Roboflow.com上创建项目,并获取Model的名称和API键 - 并使用所有这些来创建正确的URL。
我创建了带有名称
国际样本cpuhx/1
的项目和受过训练的模型,并获得了API密钥,例如TE7XXXXXXXXXXXX
,因此我的代码有几个更改。
我检查
200
以及状态是否不同,请从服务器显示消息 - 例如b'{“ message”:“ nt of condut”}'}'
或b'{“ message”:“ forbidden”}'
Problem is not when you get image from camera but when you get image from server.
You have to create project on roboflow.com and gets model's name and api key - and use all this to create correct url.
I created project and trained model with name
chess-sample-cpuhx/1
and got API KEY liketE7xxxxxxxxx
so I have full urlMy code with few changes.
I check
200
and if status is different then display message from server - likeb'{"message":"Not Found"}'
orb'{"message":"Forbidden"}'