[英]OpenCV input delay in capturing frames
I have written a code to enable capturing images from webcam feed using OpenCV.我编写了一个代码来启用使用 OpenCV 从网络摄像头提要捕获图像。 However there is an input delay whenever I press the key to capture my frame.
但是,每当我按下该键来捕获我的帧时,都会出现输入延迟。 There is no delay when I use it to quit, but there a significant delay when I use capture.
当我使用它退出时没有延迟,但是当我使用捕获时有明显的延迟。 I measured this by printing a statement inside both the cases, on pressing
c
the statement takes a delay before printing.我通过在两种情况下打印一条语句来衡量这一点,按
c
语句在打印前需要延迟。 The problem seems to me something like...the camera resources are being used and not freed up in time for the next key press or something like that....but not sure.在我看来,这个问题类似于......相机资源正在被使用,并且没有及时释放以供下一次按键或类似的事情使用......但不确定。
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
I am using an external web camera and cv2.__version__
= 3.4.2`我正在使用外部网络摄像头和
cv2.__version__
= 3.4.2`
Solved your issue, it seems like its caused by your key check.解决了您的问题,它似乎是由您的密钥检查引起的。
You should not call waitKey(1) more than once.您不应多次调用 waitKey(1)。 It causes lag.
它会导致滞后。
Try this solution:试试这个解决方案:
cap = cv.VideoCapture(0)
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
key = cv.waitKey(1)
if key==ord('c'):
print('img{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
print("Succesfully saved!")
if key==ord('q'):
print('Closing cam...')
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.