cv2.videoCapture.release() 是什么意思? [英] What's the meaning of cv2.videoCapture.release()?
问题描述
我正在使用树莓派来捕捉视频的前 20 帧.现在这更像是一个概念问题,但在浏览关于 videoCapture 的 openCV 文档时,他们强调了在此代码中发布捕获的重要性(如其网站上发布的那样):
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()
cap.release()
的重要性是什么?省略这一行是否有任何记忆含义?如果是这样,它们是什么以及为什么?
What is the importance of cap.release()
? Does ommmiting this line have any memory implication? If so what are they and why?
推荐答案
当你调用 cap.release()
时:
- 发布软件资源
- 释放硬件资源
<小时>
您可以在调用 cap.release()
之前尝试创建另一个实例 cap2 = cv2.VideoCapture(0)
.
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)
因为你还没有释放相机设备资源,那么它会引发Device or resource busy
等错误,导致引发OpenCV Exception.
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
这篇关于cv2.videoCapture.release() 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!