【发布时间】:2020-01-12 14:19:56
【问题描述】:
我编写了一个代码,可以使用 OpenCV 从网络摄像头馈送中捕获图像。但是,每当我按下键捕获我的帧时,都会出现输入延迟。当我使用它退出时没有延迟,但是当我使用捕获时有一个明显的延迟。我通过在两种情况下都打印一条语句来衡量这一点,按下c 时,该语句在打印前需要延迟。在我看来,这个问题类似于......相机资源正在被使用并且没有及时释放以供下一次按键或类似的事情......但不确定。
import cv2 as cv
import numpy as np
import glob
import matplotlib.pyplot as plt
cap = cv.VideoCapture(1)
img_counter = 0
while True:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# cv.imshow('frame',frame)
cv.imshow('gray', gray)
if not ret:
break
if cv.waitKey(1) & 0xFF == ord('q'):
print('helloq')
break
elif cv.waitKey(1) & 0xFF == ord('c'):
print('hello{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
我正在使用外部网络摄像头,并且
cv2.__version__ = 3.4.2`
【问题讨论】: