简体   繁体   English

从使用 python 通过 usb 连接的摄像机获取实时 Stream

[英]Get Live Stream from a Camera connected via usb using python

I want to grab live images from a camera which is connected via USB with my computer.我想从通过 USB 与我的计算机连接的相机中获取实时图像。

I am using an Industrial Camera with usb port.我正在使用带有 usb 端口的工业相机。

At device manager the camera is shown with its name and id so I think it is connected to PC.在设备管理器中,相机会显示其名称和 ID,因此我认为它已连接到 PC。

I ran a 'findcam' program but it is not showing any existance of camera我运行了一个“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()

The given code which i tried is running for webcam on my laptop but when the same code i run on my PC with an external camera.我尝试的给定代码正在我的笔记本电脑上为网络摄像头运行,但是当我在带有外部摄像头的 PC 上运行相同的代码时。

it constantly showing an error.它不断显示错误。

The Error:错误:

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'

I tried changing Index -1, 0, 1 but the error is constant我尝试更改 Index -1, 0, 1 但错误是恒定的

please help to find, weather it is my PC problem or camera problem or is their is any other way to stream(in python)请帮助查找,天气是我的 PC 问题或相机问题,还是他们的任何其他流式传输方式(在 python 中)

Thank You谢谢你

I started your code on my PC and it works fine.我在我的电脑上启动了你的代码,它运行良好。 Try to set the camera resolution manually, if you have an error with size.width and size.height , something like this:尝试手动设置相机分辨率,如果size.widthsize.height有错误,如下所示:

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

You may have installed the wrong version of OpenCV which doesn't support video.您可能安装了不支持视频的错误版本的 OpenCV。 Uninstall everything of opencv and then run:卸载 opencv 的所有内容,然后运行:

pip install opencv-python

(Source: cv2.VideoCapture.open() always returns FALSE ) (来源: cv2.VideoCapture.open() 总是返回 FALSE

尝试通过 pip 安装opencv-contrib-pythonpip install opencv-contrib-python或者尝试升级它的版本: pip install opencv-python --upgrade

you can debugg by checking the ret value:您可以通过检查 ret 值进行调试:

import cv2
cap = cv2.VideoCapture(0)
counter = 0
while True:.       
    ret, frame = cap.read()
    if ret :
        cv2.imshow('Live Video', frame)
        print(f" frame {counter} : ok ")
    if not ret :
        print(f" frame {counter} : ...")
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

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

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