无法使用 OpenCV 每 5 秒捕获一次图像



我试图通过笔记本电脑的内置网络摄像头使用opencv每5秒捕获一次图像。我正在使用 time.sleep(5( 进行所需的暂停。在每次运行中,第一个图像似乎都正确保存,但在那之后,所有图像都被保存为不受支持的格式(当我打开它们时(。此外,我无法通过按"q"来中断循环。 下面是我的代码。

import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
framerate = cap.get(5)
x=1
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cap.release()
# Our operations on the frame come here

filename = 'C:/Users/shjohari/Desktop/Retail_AI/MySection/to_predict/image' +  str(int(x)) + ".png"
x=x+1
cv2.imwrite(filename, frame)
time.sleep(5)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

任何帮助,不胜感激。

只是一个小小的改变,解决了我的问题。 我在循环:P中包含以下代码

cap = cv2.VideoCapture(0)
framerate = cap.get(5)

最新更新