2012-03-19 254 views
2

當我嘗試使用Python重新開放的OpenCV的CameraCapture我得到:無法打開/關閉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

回答

3

問題是,您沒有使用OpenCV API發佈捕獲組件。

您不應該這樣做del capture。正確的方法是通過:

cvReleaseCapture(capture) 
+0

謝謝!顯然,當Python對象「capture」被刪除時,底層C++結構不會被釋放。不知何故,我太笨了,找不到C++ API調用 - 再次感謝。 – Mathias 2012-03-19 13:51:02

+0

還有一件事: 而不是'import cv'我需要'從opencv.highgui導入cvReleaseCapture'在我的命名空間中擁有該方法。這就是爲什麼我一開始就找不到它的原因。 – Mathias 2012-03-20 10:16:00

+0

你有哪些opencv版本?我從cv2 import __version__中得到 '; print __version__; '$ Rev:4557 $''我不能使用'opencv.highgui.cvReleaseCapture',還有另一種釋放捕獲的方法嗎?只發現[this](http://stackoverflow.com/a/6974470/1094999) – auraham 2012-10-21 23:37:06

相關問題