python - 每 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程序。请告诉我答案
解决方案
您可以使用 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
推荐阅读
- android - RecyclerView 不想在使用 MotionLayout 时显示项目
- javascript - 我如何从 Nodje.js / Javascript 中的回调中获取值,以访问对查询的响应的值,使用 mysql
- rabbitmq - 用于 Windows 每周日志轮换的 RabbitMQ 3.8.2 不适用于每晚重新启动
- javascript - HTML - 多个解析器错误:StartTag:无效元素名称
- macos - 有没有办法在 objc 或 swift (macos Big Sur) 中验证代码签名
- django - Django 的“执行系统检查”需要很长时间 - Dash Plotly
- javascript - 处理在 React 中按时间间隔从 API 调用获取数据的组件的记忆?
- spring - xml 中的模式验证
- c# - xamarin 形式:每次调用函数时动态过滤具有不同列表值的模型列表
- wikipedia-api - 使用 Wikipedia API 时如何提取整个类别的信息