3

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

Community
  • 1
  • 1
Mathias
  • 398
  • 2
  • 6

1 Answers1

3

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)
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks! Apparently the underlying C++ structure is not released when the Python object "capture" is deleted. Somehow, I was too dumb to find the C++ API call - thanks again. – Mathias Mar 19 '12 at 13:51
  • One more thing: Instead of `import cv` I need `from opencv.highgui import cvReleaseCapture` to have the method in my namespace. This is why I couldn't find it in the first place. – Mathias Mar 20 '12 at 10:16
  • which opencv version do you have? I have `from cv2 import __version__; print __version__; '$Rev: 4557 $'` and I can't use `opencv.highgui.cvReleaseCapture`, is there another way to release the capture? Only found [this](http://stackoverflow.com/a/6974470/1094999) – auraham Oct 21 '12 at 23:37
  • I'm using OpenCV 2.4.2 and Python 2.7. – karlphillip Oct 22 '12 at 17:07