简体   繁体   English

外部网络摄像头不适用于 Opencv 和 Python

[英]External Webcam not working for Opencv and Python

I'm using Python cv2 module to use to run a program.我正在使用 Python cv2 模块来运行程序。

I'm able to access the inbuilt webcam but when I using the external webcam, it doesn't work.我可以访问内置网络摄像头,但是当我使用外部网络摄像头时,它不起作用。

I have used all codes like我已经使用了所有代码,例如

VideoCapture(1) # or any other no.s like 0,1,2,3 etc etc.

I have even disabled the inbuilt webcam using device manager.我什至使用设备管理器禁用了内置网络摄像头。

but nothing works.但没有任何效果。 I'm using pythonxy.我正在使用 pythonxy。 I have tried in command prompt also.我也试过在命令提示符下。 It still doesn't work.它仍然不起作用。

I get the following error我收到以下错误

runfile('C:/Users/Prashant/Documents/Python Scripts/Circledetection.py',     wdir=r'C:/Users/Prashant/Documents/Python Scripts')
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file ..\..\..\modules\imgproc\src\color.cpp, line 3402
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "C:/Users/Prashant/Documents/Python Scripts/Circledetection.py", line 27, in <module>
    prev_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.error: ..\..\..\modules\imgproc\src\color.cpp:3402: error: (-215) scn == 3 || scn == 4

You mentioned VideoCapture(0) to VideoCapture(1) .你提到VideoCapture(0)VideoCapture(1)
"1" means external camera number. “1”表示外部摄像机编号。

import cv2
import numpy as np

cap = cv2.VideoCapture(1) 
while(1):
    ret, frame = cap.read()
    #print(height)
    #cv2.imshow("Cropped Image", crop_img)
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

That assertion means that your image does not have 3 (RGB, BGR, etc) channels, or 4 channels (3 channels + alpha).该断言意味着您的图像没有 3 个(RGB、BGR 等)通道或 4 个通道(3 个通道 + alpha)。 This can be caused, obviously, by the image not existing at all.显然,这可能是由根本不存在的图像引起的。

While this might be because your camera isn't working, it's equally likely that your camera does not give usable frames for the first few read(), as it's still 'preparing' itself.虽然这可能是因为您的相机不工作,但同样有可能您的相机没有为前几个 read() 提供可用的帧,因为它仍在“准备”自己。

The easiest way to solve this, assuming you use something like retv, frame = cam.read() is to check if frame: or if retv: .解决这个问题的最简单方法,假设您使用类似retv, frame = cam.read()是检查if frame:if retv:

Have it skip over your processing if there's no image, and within a few frames you should have input that is readable.如果没有图像,让它跳过你的处理,在几帧内你应该有可读的输入。

Are you sure you are using the correct index?您确定您使用的是正确的索引吗? Using Videocapture you can also try to use Videocapture(-1) , since the external cam sometimes appears as the last element in the list of input.使用Videocapture您还可以尝试使用Videocapture(-1) ,因为外部摄像头有时会显示为输入列表中的最后一个元素。 Videocapture also can return device names, so you could try to loop over the input devices index list and print their " friendly names", to make sure you are using the correct one. Videocapture还可以返回设备名称,因此您可以尝试遍历输入设备索引列表并打印它们的“友好名称”,以确保您使用的是正确的名称。

OpenCV requires the following dependencies to work with the USB cameras. OpenCV 需要以下依赖项才能与 USB 摄像头配合使用。 Install the dependencies from the following commands:从以下命令安装依赖项:

  • libav video input/output development libraries libav 视频输入/输出开发库

    sudo apt-get install libavformat-dev libavutil-dev libswscale-dev
  • Video4Linux camera development libraries Video4Linux 相机开发库

    sudo apt-get install libv4l-dev
  • OpenGL development libraries (to allow creating graphical windows) OpenGL 开发库(允许创建图形窗口)

     sudo apt-get install libglew-dev
  • GTK development libraries (to allow creating graphical windows) GTK 开发库(允许创建图形窗口)

     sudo apt-get install libgtk2.0-dev

Thanks it works for me.谢谢它对我有用。

OpenCV requires the following dependencies to work with the USB cameras. OpenCV 需要以下依赖项才能与 USB 摄像头配合使用。 Install the dependencies from the following commands:从以下命令安装依赖项:

libav video input/output development libraries libav 视频输入/输出开发库

sudo apt-get install libavformat-dev libavutil-dev libswscale-dev Video4Linux camera development libraries sudo apt-get install libavformat-dev libavutil-dev libswscale-dev Video4Linux 相机开发库

sudo apt-get install libv4l-dev OpenGL development libraries (to allow creating graphical windows) sudo apt-get install libv4l-dev OpenGL 开发库(允许创建图形窗口)

sudo apt-get install libglew-dev GTK development libraries (to allow creating graphical windows) sudo apt-get install libglew-dev GTK 开发库(允许创建图形窗口)

sudo apt-get install libgtk2.0-dev sudo apt-get install libgtk2.0-dev

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

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