gpt4 book ai didi

python - cv2.videoCapture.release() 是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:16 24 4
gpt4 key购买 nike

我正在使用树莓派捕捉视频的前 20 帧。现在这更像是一个概念问题,但是在浏览 videoCapture 上的 openCV 文档时,他们强调了在此代码中发布捕获的重要性(如他们网站上发布的那样):

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() 的重要性是什么?忽略这一行是否对内存有影响?如果是,它们是什么,为什么?

最佳答案

当您调用 cap.release() 时,则:

  1. 发布软件资源
  2. 释放硬件资源

在调用 cap.release() 之前,您可以尝试创建另一个实例 cap2 = cv2.VideoCapture(0)

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

cap2 = cv2.VideoCapture(0)

因为你还没有释放相机设备资源,那么它会引发错误,如Device or resource busy,导致引发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

关于python - cv2.videoCapture.release() 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com