0

I am new to the Python opencv. Can anybody please help me to sort out the error

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 1
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(100)
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 =1
        capture = cv.CaptureFromCAM(camera_index)

while True:
repeat()

this is the error what I am getting -

OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /home/paraste/OpenCV-2.3.1/modules/core/src/array.cpp, line 2382
Traceback (most recent call last):
  File "dualcamara.py", line 10, in <module>
img = cv.GetMat(cv.QueryFrame(capture), 500)
cv2.error: NULL array pointer is passed

2 Answers 2

1

It seems likely that either cv.CaptureFromCAM() or cv.QueryFrame() is failing (maybe camera_index is wrong?), and thus you get a NULL in frame which causes that error. You should check the result of those two functions and make sure they succeed (I'm just printing a message in this case, you could of course do something else):

capture = cv.CaptureFromCAM(camera_index)
if not capture:
     print "Failed to initialize capture"

frame = cv.QueryFrame(capture)
if not frame:
     print "Failed to get frame"
Sign up to request clarification or add additional context in comments.

1 Comment

hello, Thank you for your reply. The CaptureFromCAM function is returning some hexadecimal value but the QueryFrame function is returning none. i thought camera is not on so, i manually switched it on using cheese command but there is no change in the return value. Please let me know how to switch on the camera using inbuilt function. Even i tried using the function cv2.VideoCapture.open but it ends with the following error:AttributeError: 'builtin_function_or_method' object has no attribute 'open'
0

cv.QueryFrame() may be returning None and you are not handling the possibility. I find that cv.QueryFrame() sometimes returns None at the beginning, so I simply put:

if frame == None:
    return

This way, even if your first call fails, or calls fail intermittently, your loop will continue and feed images as they are captured. I had the same problem on my Mac Book Pro using python 2.7 and this solved it for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.