首页 > 解决方案 > 每 5 秒从视频中捕获图像,并使用 Python 和 OpenCV 保存图片

问题描述

制作ocr程序的人。我想每 5 秒捕获一次视频图像并实时检查。

cap = cv2.VieoCapture(0)

while(True)
    ret,frame = cap.read()

    cv2.imwrite('Test.png'),frame,params=[cv2.IMWRITE_PNG_COMPRESSION,2]
    ocrcheck()    #Function to read 'Test.png' and check with ocr program

我想每5秒实时获取一帧,保存并将保存的图像打印到ocr程序。请告诉我答案

标签: pythonopencvocr

解决方案


您可以使用 cv2.waitKey(delay_in_ms) 或 time.sleep(delay_in_seconds) 休眠给定的时间。还要考虑以下几点:您的 ocrcheck() 函数和 cv2.imwrite 将花费一些时间,因此最好不要睡 5 秒,而是睡 (5 - time_spent),其中 time_spent 是调用这两个函数所花费的时间。例子:

while(True)
    start_time = time.time()

    ret,frame = cap.read()

    cv2.imwrite('Test.png'),frame,params=[cv2.IMWRITE_PNG_COMPRESSION,2]
    ocrcheck()    #Function to read 'Test.png' and check with ocr program

    spent = time.time() - start_time # how much you spent on calling functions above

    time.sleep(5 - spent) # sleep for a rest of time

推荐阅读