python - 如何在仍然显示摄像机记录的同时根据训练有素的模型检查图像?

标签 python multithreading keras cv2

我有这个python程序,它在OpenCV (cv2)的帮助下打开了摄像头 session 。我已经设置了一个感兴趣的区域,在该区域中,将从摄像机记录中提取图像,并在经过训练的keras模型中将其作为参数进行预测。我的问题是,我怎样才能每10秒检查一次模型?我尝试使用time.sleep(10)将整个窗口冻结(因为它在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 - 如何在仍然显示摄像机记录的同时根据训练有素的模型检查图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65347405/

相关文章:

python - Airflow DAG 预定日期晚了一周

python - 在 Python 中计算决定系数

java - 循环障碍Java,如何验证?

tensorflow - 平面一维数据上的一维卷积(即无时间序列)

python - 如何在 Keras 中使用 LSTM 的多个输入?

python - 打印列表时保持Python在换行后不留空格

python - 发电机停止 Keras fit_generator

c++ - 多线程效率不高 : Debugging False Sharing?

java - 对并行 Java 程序进行单元测试

python - 制作 Keras 模型时将数据拆分为训练、测试和评估