首页 > 解决方案 > 如何在仍然显示摄像机记录的同时检查训练模型的图像?

问题描述

我有这个 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 中的线程并不熟悉。有谁知道如何做到这一点?

标签: pythonmultithreadingkerascv2

解决方案


我通常为此使用框架 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

推荐阅读