11

I am working with a raspberry pi to capture the first 20 frames of a video. Now this is more of a concepts question but while going through the openCV documentation on videoCapture, they emphasize the importance of releasing capture in this code (as posted on their website):

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

What is the importance of cap.release()? Does ommmiting this line have any memory implication? If so what are they and why?

4 Answers 4

12

When you call cap.release(), then:

  1. release software resource
  2. release hardware resource

You can try to make another instance cap2 = cv2.VideoCapture(0) before you call cap.release().

cap = cv2.VideoCapture(0)
#cap.release() 

cap2 = cv2.VideoCapture(0)

Because you haven't release the camera device resource, then it will raise errors like Device or resource busy, leads to raise a OpenCV Exception.

libv4l2: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

libv4l2: error setting pixformat: Device or resource busy
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline
) in cvCaptureFromCAM_GStreamer, file /home/xxx/Programs/OpenCV/src/opencv-master/modules/videoio/src/cap_gstreamer.cpp, line 887
VIDEOIO(cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast<char *>(index))): raised OpenCV exception:

/home/xxx/Programs/OpenCV/src/opencv-master/modules/videoio/src/cap_gstreamer.cpp:887: error: (-2) GStreamer: unable to start pipeline
 in function cvCaptureFromCAM_GStreamer
5

It is unclear to me, but according to this offical documentation it both closes the IO device, as well as frees a pointer. So it could be assumed that it frees some amount of memory (however much that is). More importantly, I think it would release access of the device/file for other processes.

Closes video file or capturing device.

The methods are automatically called by subsequent VideoCapture::open and by VideoCapture destructor.

The C function also deallocates memory and clears *capture pointer.

2

According to my experience it's more useful if you are using real-time cameras on your laptop or another robot project with Raspberry Pi or else, in this case you really need to release the videocapture object, (and videowrite as well) To avoid any materials conflicts.

And you can see that's kind of problem on daily life using laptop when sometimes you need to close a software( from the task manager ) to be able to use an other one .

0

As Kinght mentioned above, it releases the hardware and software resources.

BUT, in the new version of openCV, it executes automatically after exiting the loop of frames.

According to the docstring of openCV using Python, they're saying this:

release() -> Closes video file or capturing device. The method is automatically called by subsequent VideoCapture::open and by VideoCapture destructor. The C function also deallocates memory and clears *capture pointer.

So, I've tried to allocate the resources twice sequentially without releasing, and it works fine. As in the following code:

import cv2 as cv
cap = cv.VideoCapture('Resources/test1.mp4')
cap2 = cv.VideoCapture('Resources/test2.mp4')
while True:
    isSuccess1, img1 = cap.read()
    isSuccess2, img2 = cap2.read()
    print('isSuccess1', isSuccess1)
    print('isSuccess2', isSuccess2)
    cv.imshow('Video1', img1)
    cv.imshow('Video2', img2)

    if cv.waitKey(1) & 0XFF == ord('q'):
        break

And the video opened well, also the output in my terminal was:

isSuccess True
1
  • 3
    You're not testing what you actually think you are testing; no destructor is called here, and you do not call open. Therefore, release is never called. Both capture worked, because this is a local file. If you try with a hardware device (like a webcam) you will surely have errors. Also, you are opening two different files, so there was no doubt this was going to work Jul 11, 2022 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.