使用OpenCV和Python显示网络摄像头 [英] Displaying a webcam feed using OpenCV and Python

查看:120
本文介绍了使用OpenCV和Python显示网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Python 创建一个简单的程序,该程序使用OpenCV从我的网络摄像头获取视频供稿并将其显示在屏幕上.

I have been trying to create a simple program with Python which uses OpenCV to get a video feed from my webcam and display it on the screen.

我知道我之所以在这里,是因为已经创建了窗口,并且网络摄像头上的灯闪烁了,但是窗口似乎什么都没显示.希望有人可以解释我在做什么错.

I know I am partly there because the window is created and the light on my webcam flicks on, but it just doesn't seem to show anything in the window. Hopefully someone can explain what I'm doing wrong.

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(0)

def repeat():

    frame = cv.QueryFrame(capture)
    cv.ShowImage("w1", frame)


while True:
    repeat()

在不相关的注释上,我注意到我的网络摄像头有时会更改其在cv.CaptureFromCAM中的索引号,有时即使我只连接了一个摄像头并且没有拔出插头,也需要输入0、1或2.它(我知道,除非更改索引,否则指示灯不会亮起).有没有办法让Python确定正确的索引?

On an unrelated note, I have noticed that my webcam sometimes changes its index number in cv.CaptureFromCAM, and sometimes I need to put in 0, 1 or 2 even though I only have one camera connected and I haven't unplugged it (I know because the light doesn't come on unless I change the index). Is there a way to get Python to determine the correct index?

推荐答案

尝试将c = cv.WaitKey(10)行添加到repeat()方法的底部.

Try adding the line c = cv.WaitKey(10) at the bottom of your repeat() method.

这将等待10毫秒,以便用户输入密钥.即使您根本不使用密钥,也请放进去.我认为这只需要稍加延迟,因此time.sleep(10)也许也可以.

This waits for 10 ms for the user to enter a key. Even if you're not using the key at all, put this in. I think there just needed to be some delay, so time.sleep(10) may also work.

关于相机索引,您可以执行以下操作:

In regards to the camera index, you could do something like this:

for i in range(3):
    capture = cv.CaptureFromCAM(i)
    if capture: break

这将至少在0-2的索引中找到第一个工作"捕获设备的索引.您的计算机中可能有多个设备被识别为正确的捕获设备.我知道确认您正确的唯一方法是手动查看您的灯光.也许获取图像并检查其属性?

This will find the index of the first "working" capture device, at least for indices from 0-2. It's possible there are multiple devices in your computer recognized as a proper capture device. The only way I know of to confirm you have the right one is manually looking at your light. Maybe get an image and check its properties?

要向该过程添加用户提示,您可以在重复循环中将一个键绑定到切换摄像头:

To add a user prompt to the process, you could bind a key to switching cameras in your repeat loop:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    frame = cv.QueryFrame(capture)
    cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
    if(c=="n"): #in "n" key is pressed while the popup window is in focus
        camera_index += 1 #try the next camera index
        capture = cv.CaptureFromCAM(camera_index)
        if not capture: #if the next camera index didn't work, reset to 0.
            camera_index = 0
            capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

免责声明:我尚未对此进行测试,因此它可能存在错误或无法正常工作,但至少可以为您提供解决方法的想法.

disclaimer: I haven't tested this so it may have bugs or just not work, but might give you at least an idea of a workaround.

这篇关于使用OpenCV和Python显示网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆