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

时间:2018-07-01 17:46:21

标签: python-3.x opencv

我正在尝试通过笔记本电脑的内置网络摄像头使用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()

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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

cap = cv2.VideoCapture(0)
framerate = cap.get(5)
相关问题