【问题标题】:Cannot turn off/on CameraCapture using Python/opencv: Device or resource busy无法使用 Python/opencv 关闭/打开 CameraCapture:设备或资源繁忙
【发布时间】:2012-04-03 19:53:19
【问题描述】:

当我尝试使用 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

【问题讨论】:

    标签: python opencv camera capture v4l2


    【解决方案1】:

    问题是您没有使用 OpenCV API 发布捕获组件。

    你不应该这样做del capture。正确的做法是通过:

    cvReleaseCapture(capture)
    

    【讨论】:

    • 谢谢!显然,当 Python 对象“捕获”被删除时,底层 C++ 结构不会被释放。不知何故,我太笨了,找不到 C++ API 调用 - 再次感谢。
    • 还有一件事:我需要 from opencv.highgui import cvReleaseCapture 而不是 from opencv.highgui import cvReleaseCapture 在我的命名空间中拥有该方法。这就是我一开始找不到它的原因。
    • 你有哪个opencv版本?我有from cv2 import __version__; print __version__; '$Rev: 4557 $',但我不能使用opencv.highgui.cvReleaseCapture,还有其他方法可以释放捕获吗?只找到this
    • 我使用的是 OpenCV 2.4.2 和 Python 2.7。
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2017-04-18
    • 2016-08-29
    • 2015-09-24
    • 2022-12-03
    • 2018-10-25
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多