Home What's the meaning of cv2.videoCapture.release()?
Reply: 2

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

Veejay
1#
Veejay Published in 2018-01-11 18:16:27Z

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?

ritlew
2#
ritlew Reply to 2018-01-11 18:26:28Z

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.

Silencer
3#
Silencer Reply to 2018-01-12 02:24:41Z

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331306 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO