简体   繁体   English

如何在 Python OpenCV 中显示和读取实时摄像机视频

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

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.我一直在研究 Roboflow 团队的代码,该代码假设显示实时视频捕获并给出我训练的 model 的预测,但我遇到了错误。

Get webcam interface via opencv-python通过 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 // 同步从 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' 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> function 'cv::imshow' 中的 0 && size.height>0

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 网络摄像头的 Roboflow 代码

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).正如 Gunner Stone 在他们的评论中提到的,这可能意味着 API 没有返回图像(它可能会给出某种错误消息)。

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.我建议备份一个刻度并通过尝试获取单个图像的预测来确保您的参数(例如 model ID、api 键)是正确的。 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.在链接代码的第 51 行,就在requests.post位之后,查看 API 是否正在返回特定的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM