Cannot turn off/on CameraCapture using Python/opencv: Device or resource busy

笑着哭i 提交于 2019-12-07 11:53:42

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/9768312/cannot-turn-off-on-cameracapture-using-python-opencv-device-or-resource-busy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!