Skip to content Skip to sidebar Skip to footer

Python Opencv - Videocapture.release() Won't Work In Linux

I'm using OpenCV 2.4.9 and Python 2.7.11. I've written a small program that shows the camera output, and when pressing 'q', closes the camera but doesn't exit the application (for

Solution 1:

The way to free the camera (without exiting) is indeed release(). I've tested your code in a Linux Mint 18 (64bit) environment running both OpenCV 2.4.13 and also OpenCV 3.1 with Python 2.7.12. There were no issues.

Here is a way for you to see what is going on in your code:

import cv2
import sys

#print "Before cv2.VideoCapture(0)"#print cap.grab()
cap = cv2.VideoCapture(0)

print"After cv2.VideoCapture(0): cap.grab() --> " + str(cap.grab()) + "\n"whileTrue:
    ret, frame = cap.read()
    if frame isNone:
        print"BYE"break

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        breakprint"After breaking, but before cap.release(): cap.grab() --> " + str(cap.grab()) + "\n"

cap.release()

print"After breaking, and after cap.release(): cap.grab() --> " + str(cap.grab()) + "\n"

cap.open(0)
print"After reopening cap with cap.open(0): cap.grab() --> " + str(cap.grab()) + "\n"

cv2.destroyAllWindows()

whileTrue:
    cv2.waitKey(1)

You may want to think about reinstalling OpenCV on your system. I recommend checking out the awesome guides on PyImageSearch --> http://www.pyimagesearch.com/opencv-tutorials-resources-guides/

Let me know if this helps!

Solution 2:

I had the same problem. By default, my OpenCV build was using Gstreamer as a backend for VideoCapture(). If I forced it to use V4L2 instead, e.g.

cap = VideoCapture(0,cv2.CAP_V4L2)

cap.release() worked.

The Gstreamer backend SHOULD be able to close any pipelines it opens (see source code here: https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_gstreamer.cpp), but for my backend-agnostic application it was easier to avoid than fix that issue.

Post a Comment for "Python Opencv - Videocapture.release() Won't Work In Linux"