python - 如何在仍然显示摄像机记录的同时检查训练模型的图像?
问题描述
我有这个 python 程序,它可以在帮助下OpenCV (cv2)
打开一个相机会话。我已经设置了一个感兴趣的区域,从摄像机记录中提取图像并在经过训练的keras
模型中作为参数传递以获得预测。我的问题是我怎样才能每 10 秒检查一次模型?我尝试使用time.sleep(10)
which 将整个窗口(因为它发生在 while 循环内)冻结 10 秒。这意味着整个录制每 10 秒停止一次,而我希望能够持续录制并每隔 10 秒检查一次模型。到目前为止,这是我的代码:
import cv2
import numpy as np
from tensorflow import keras
import time
#import playsound
# wait for the sound to finish playing?
blocking = True
model = keras.models.load_model("model2.h5")
cam = cv2.VideoCapture(0)
####region of interest dimensions
startX = 800
startY = 0
finishX = 1200
finishY = 400
while(1):
ret, frame = cam.read()
if ret:
### displays video recording and region of interest
frame = cv2.flip(frame,1)
display = cv2.rectangle(frame.copy(),(startX,startY),(finishX,finishY),(0,0,255),2)
cv2.imshow('Total Input',display)
ROI = frame[startY:finishY, startX:finishX].copy()
cv2.imshow('Region of Interest', ROI)
#pauses for 10 seconds
time.sleep(10)
img = cv2.resize(display, (128, 128)) #R
img = img.reshape(1, 128, 128, 3)
predictions = model.predict(img) # Make predictions towards the test set
predicted_label = np.argmax(predictions) # Get index of the predicted label from prediction
print(predicted_label)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
我在想我是否应该使用线程,但我对 python 中的线程并不熟悉。有谁知道如何做到这一点?
解决方案
我通常为此使用框架 ID。基本上,您的模型只会在 n 帧之后进行预测。这是如何使用它的代码。您可以编辑要跳过的帧数:
frame_id =0
while(1):
frame_id +=1
ret, frame = cam.read()
if ret:
### displays video recording and region of interest
frame = cv2.flip(frame,1)
display = cv2.rectangle(frame.copy(),(startX,startY),(finishX,finishY),(0,0,255),2)
cv2.imshow('Total Input',display)
ROI = frame[startY:finishY, startX:finishX].copy()
cv2.imshow('Region of Interest', ROI)
#pauses for 10 seconds
time.sleep(10)
img = cv2.resize(display, (128, 128)) #R
img = img.reshape(1, 128, 128, 3)
if fram_id % 10 == 0:
predictions = model.predict(img) # Make predictions towards the test set
predicted_label = np.argmax(predictions) # Get index of the predicted label from prediction
print(predicted_label)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
推荐阅读
- python - 使用 Web Scraping 方法(Python BS4)从 Google Play 商店中提取 Android 应用程序的评论 - 索引超出范围
- vbscript - 从替换中跳过一个字符串,如果它有任何后缀或后缀
- android - 在服务内部使用广播接收器
- angular - Angular 8:NG8002:不能绑定到“ontime”,因为它不是“div”的已知属性
- node.js - 在win10中,我执行yarn add protobufjs,但是报错:error An unexpected error occurred: "EPERM: operation not allowed, unlink
- python - 模型很容易收敛但预测结果很差
- java - 尽管没有任何错误,但 JDBC 无法更新
- regex - 正则表达式:匹配带有下划线和 id 的单词
- perl - 如何将多个 HMTL 文件中的内容合并到一个文件中?
- http - 为什么不总是为 CORS 请求发送 Origin HTTP 标头?