无法使用Python / opencv关闭/打开CameraCapture:设备或资源繁忙

当我尝试使用Python重新打开opencv的CameraCapture时,我得到:

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

尽管我的应用程序使用PyQt和其他各种模块在更大的上下文中运行,但我能够隔离问题.因此,当我按“ r”(重新加载)时,捕获对象将被删除,但由于它仍处于活动状态,因此无法重新打开与摄像机的连接:

#!/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()

针对类似问题的提示对我不起作用:
cant find ReleaseCapture in opencv while using python?和/或OpenCV / Array should be CvMat or IplImage / Releasing a capture object

解决方法:

问题是您没有使用OpenCV API释放捕获组件.

您不应该进行del捕获.正确的方法是通过:

cvReleaseCapture(capture)
上一篇:找回了当年一篇V4L2 linux 摄像头驱动的博客


下一篇:V4L2框架分析