Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of cv2.videoCapture.release()?

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?

like image 482
Veejay Avatar asked Jan 11 '18 18:01

Veejay


People also ask

What does cv2 VideoCapture do?

cv2. VideoCapture – Creates a video capture object, which would help stream or display the video.

What does cv2 VideoCapture 0 mean?

Next, we cay cap = cv2. VideoCapture(0) . This will return video from the first webcam on your computer.

What does cv2 destroyAllWindows () do?

0 – wait indefinitely cv2. destroyAllWindows() simply destroys all the windows we created. To destroy any specific window, use the function cv2. destroyWindow() where you pass the exact window name.

What is isOpened () in Python?

In a real-time video monitoring system, to ensure that the camera is opened and working properly we have isOpened() of OpenCV. The idea behind the article is to check whether the camera is connected and if the camera is found to be disconnected then a mail will be sent to the Admin or the concerned person.


2 Answers

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
like image 181
Kinght 金 Avatar answered Sep 27 '22 17:09

Kinght 金


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.

like image 25
ritlew Avatar answered Sep 27 '22 18:09

ritlew