我试图使用 cv2.VideoCapture()
每 3 秒从相机获取一帧,所以我使用 time.sleep()
暂停执行,代码在这里:
import cv2
import time
cnt = 0
cap = cv2.VideoCapture(0)
while (True):
time.sleep(3)
ret, frame = cap.read()
cv2.imwrite('{}.png'.format(cnt),frame)
cnt+=1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我在相机前面有一个秒表,每 3 秒保存一帧,但我发现在第一帧之后还有其他 4 帧被错误地保存,所以保存的图像时间是:1.0、1.1、1.1、 1.2、1.3、2、3、4、5、6 等。那到底是怎么回事?
最佳答案
问题似乎是当您调用 read()
时 opencv 正在缓冲多个帧。从其他几个问题(Get most recent frame from webcam、http://answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/)看来,缓冲了 5 张图片,因此您可能需要在继续之前grab()
额外的 4 张。
关于python - 为什么 time.sleep() 在开头以 cv2.VideoCapture() 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047861/