简体   繁体   English

OpenCV Python:QueryFrame返回无

[英]OpenCV Python : QueryFrame return None

I'm using OpenCV 2.4 and python 2.7.5 on a macbook. 我在Macbook上使用OpenCV 2.4和python 2.7.5。 I want to display the live stream of the inbuilt camera with the following code : 我想用以下代码显示内置摄像机的实时流:

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)
    print type(frame)
    #cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
while True:
    repeat()

However, it seems QueryFrame does not always return an iplimage, here is what I get on the terminal: 但是,似乎QueryFrame并不总是返回一个iplimage,这是我在终端上得到的:

<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>

Does anybody know where the problem comes from ? 有人知道问题出在哪里吗?

Thank you 谢谢

EDIT: I noticed it takes some seconds before my camera turn on, so I put some delay before entering the "while". 编辑:我注意到打开相机需要花费几秒钟的时间,所以我在进入“ while”之前放置了一些延迟。 As for the noneType problem I have no idea why I get a correct image every 3 frames..anyway I just "fixed" it by putting a condition that checks if we get a correct image, here is the code: 至于noneType问题,我不知道为什么我每3帧会得到一张正确的图像。无论如何,我只是通过放置一个条件来“固定”它,以检查我们是否获得了正确的图像,这是代码:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
c = cv.WaitKey(5000)
def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    frame = cv.QueryFrame(capture)

    if frame:

        cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
while True:
    repeat()

Suggestion as follow: 建议如下:

  • camera_index = -1 camera_index = -1
  • drop first frame just in case 放下第一帧以防万一

    firstImage = copy.deepcopy(cv.QueryFrame(capture)) firstImage = copy.deepcopy(cv.QueryFrame(capture))

Share my code for your reference: 分享我的代码以供参考:

import cv2 as cv
import copy
camera_index = -1 

capture = cv.CaptureFromCAM(camera_index)

isRunning = True
firstImage = copy.deepcopy(cv.QueryFrame(capture)) #drop first frame which might empty
cv.NamedWindow("Webcam",  cv.CV_WINDOW_AUTOSIZE);

def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    global isRunning
    global firstImage
    currImage = cv.QueryFrame(capture) 
    cv.ShowImage("Webcam",currImage)
    c = cv.WaitKey(10)
    if(c==27):
            isRunning = False
    if (c!=-1):
            print str(c)

while isRunning:
    repeat()

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

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