首页 > 解决方案 > 使用 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 中)

谢谢你

标签: pythonimagecomputer-visioncv2industrial

解决方案


我在我的电脑上启动了你的代码,它工作正常。尝试手动设置相机分辨率,如果你有一个错误size.widthsize.height像这样:

cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,920)

推荐阅读