VideoCapture open and source switching problems [closed]

asked 2012-10-25 23:21:15 -0500

blarg gravatar image

I'm using Python 2.7.2 OpenCV on Win7 64 bit (but 32 bit Python) to display two cameras at once. I have only 2 capturable devices installed currently, a TV tuner on #0 and a webcam on #1.. I have a combobox that allows me to switch cameras for each display. Because of errors I had early on when both cameras would show the same video when trying to change video source, I prevent the two cameras from choosing the same index (the index given to However, because of weird behavior in 2.4.2 and in 2.4.3rc., I get these issues again.


I initialize each:

camera1 = cv2.VideoCapture(0)
camera2 = cv2.VideoCapture(1)

If the source is changed (to a valid index)

#camera.release() # test if this makes a difference
# checking camera.isOpen() always yields true, even for an index > 1

To read a frame:

_,frame =

Here are results of when errors occur. NOTE: I'm switch the camera inputs while BOTH CAMERAS ARE NOT GRABBING FRAMES.

Results in OpenCV 2.4.2:

  • using alone:

switching from #0 to anything > #1, I get camera #1.

switching from #1 to anything > #1, I get the following error when I call

SystemError: NULL object passed to Py_BuildValue

This kills the thread handling that camera. The other camera will continue to operate fine unless I switch from some position that gives camera #1 to another position that gives camera #1, and I get the same error.

It seems that selecting a camera after the last "real" one gives the last real camera.

  • adding camera.release() first:

same behavior, but no errors. Also notice "setVideoSetting - QueryInterface Error" appears in console output when closing the program??

Results in OpenCV 2.4.3rc:

  • using alone:

no errors thrown, but it seems that the camera source can't change, always shows the starting source feed, can't change it.

  • adding camera.release() first:

same behavior as in 2.4.2 when adding camera.release() first

Finally, I was working on this project on a work computer earlier today, with the same versions of everything installed, using OpenCV 2.4.2, when I switch to a camera index that wasn't there, I got a blank frame (which is better than getting the last "real" source, but not as nice as OpenCV indicating that there is no source present).

Is there some bug here or something different I should do here? The reason I'm having an issue is that if the two cameras show the same source, I get a crashing error, the same as above, when I try to switch one of the sources away and then try to read a frame from that switch source.

-- Aside: Is there some way that OpenCV can tell the difference between a real and fake source? It almost seems like, in this example, it can tell I ... (more)

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-27 06:24:56.790991