简体   繁体   English

使用 OpenCV 和 Python 显示网络摄像头源

[英]Displaying a webcam feed using OpenCV and Python

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.我一直在尝试使用 Python 创建一个简单的程序,它使用OpenCV从我的网络摄像头获取视频源并将其显示在屏幕上。

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.我知道我在那里的部分原因是创建了 window 并且我的网络摄像头上的灯闪烁,但它似乎在 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()

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).在不相关的说明中,我注意到我的网络摄像头有时会更改其在cv.CaptureFromCAM中的索引号,有时我需要输入 0、1 或 2,即使我只连接了一个摄像头并且我没有拔下它(我知道,因为除非我更改索引,否则灯不会亮起)。 Is there a way to get Python to determine the correct index?有没有办法让 Python 确定正确的索引?

An update to show how to do it in the recent versions of OpenCV: 更新以显示如何在最新版本的OpenCV中执行此操作:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

cv2.destroyWindow("preview")
vc.release()

It works in OpenCV-2.4.2 for me. 它适用于OpenCV-2.4.2。

Try adding the line c = cv.WaitKey(10) at the bottom of your repeat() method. 尝试在repeat()方法的底部添加c = cv.WaitKey(10)行。

This waits for 10 ms for the user to enter a key. 等待10毫秒,用户输入密钥。 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. 即使你根本没有使用密钥,也要把它放进去。我认为只需要一些延迟,所以time.sleep(10)也可以工作。

In regards to the camera index, you could do something like this: 关于相机索引,你可以这样做:

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

This will find the index of the first "working" capture device, at least for indices from 0-2. 这将找到第一个“工作”捕获设备的索引,至少对于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. 免责声明:我没有对此进行过测试,因此它可能存在错误或只是不起作用,但可能至少会让您了解一种解决方法。

If you only have one camera, or you don't care which camera is the correct one, then use "-1" as the index. 如果您只有一台摄像机,或者您不关心哪台摄像机是正确的摄像机,则使用“-1”作为索引。 Ie for your example capture = cv.CaptureFromCAM(-1) . 即你的例子capture = cv.CaptureFromCAM(-1)

import cv更改import cv2.cv as cv请参见此处的帖子。

Try the following. 请尝试以下方法。 It is simple, but I haven't figured out a graceful way to exit yet. 这很简单,但我还没有想出一个优雅的退出方式。

import cv2.cv as cv
import time

cv.NamedWindow("camera", 0)

capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
        break
cv.DestroyAllWindows()

As in the opencv-doc you can get video feed from a camera which is connected to your computer by following code. 与在opencv-doc中一样,您可以通过以下代码从连接到计算机的摄像头获取视频。

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

You can change cap = cv2.VideoCapture(0) index from 0 to 1 to access the 2nd camera. 您可以将cap = cv2.VideoCapture(0)索引从0更改为1以访问第二个摄像头。
Tested in opencv-3.2.0 opencv-3.2.0测试过

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

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