python - 使用 python 从通过 USB 连接的相机获取实时流
问题描述
我想从通过 USB 与我的计算机连接的相机中获取实时图像。
我正在使用带 USB 端口的工业相机。
在设备管理器中,相机会显示其名称和 ID,因此我认为它已连接到 PC。
我运行了一个“findcam”程序,但它没有显示任何相机的存在
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我尝试的给定代码正在我的笔记本电脑上为网络摄像头运行,但是当我在带有外部摄像头的 PC 上运行相同的代码时。
它不断显示错误。
错误:
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/industrialcamera/ICvideocapture.py", line 11, in <module>
cv2.imshow('Live Video', frame)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
我尝试更改 Index -1 , 0 , 1 但错误是恒定的
请帮助查找,天气是我的 PC 问题或相机问题,还是他们的任何其他流式传输方式(在 python 中)
谢谢你
解决方案
我在我的电脑上启动了你的代码,它工作正常。尝试手动设置相机分辨率,如果你有一个错误size.width
,size.height
像这样:
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,920)
推荐阅读
- ios - How to stop navigation bar title from changing color when moving from controller to controller
- php - 更改语法 ul、li、span
- javascript - 如何解决 postgres 中的散列问题
- sms - Jasmin:MT 路由到 http api
- django - django 过滤器 - 如何从 3 列中搜索电话号码
- python - 简单的指数平滑预测不在 Python 中的实际数据之上绘制
- javascript - 如何通过锚点复制点击的值并通过输入添加这个值并移除?
- python - 在 html 模板中使用 Jinja2 时出错:[UnicodeDecodeError]
- python - RegEx 用于字母数字文本字符串直至特殊模式
- matlab - Matlab trainNetwork 的问题。格式化观察的正确方法是什么?