When I try to re-open opencv's CameraCapture using Python I get:
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT
Although my application runs in a bigger context using PyQt and various other modules, I was able to isolate the problem. So when I hit "r" (reload) the capture object is deleted but I'm not able to re-open a connection to the camera since it is still active:
#!/usr/bin/env python
from opencv.cv import *
from opencv.highgui import *
import sys
import time
import gc
cvNamedWindow("w1", CV_WINDOW_AUTOSIZE)
camera_index = 1
capture = cvCreateCameraCapture(camera_index)
def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cvQueryFrame(capture)
cvShowImage("w1", frame)
c = cvWaitKey(10)
if c == "q":
sys.exit(0)
if c == "r":
print 'reload'
#del frame
del capture
# pretty useless sleeping, garbage collecting, etc.
#gc.collect()
#import pdb; pdb.set_trace()
#print gc.get_objects()
#print gc.DEBUG_UNCOLLECTABLE
#time.sleep(2)
capture = cvCreateCameraCapture(camera_index)
if __name__ == "__main__":
while True:
repeat()
The hints given for similar questions did not work for me: cant find ReleaseCapture in opencv while using python? and/or OpenCV / Array should be CvMat or IplImage / Releasing a capture object
The problem is that you are not releasing the capture component using the OpenCV API.
You shouldn't do del capture
. The right way to do it is through:
cvReleaseCapture(capture)