0

I'm making a program for predicting sign language. The problem is that I want to wait 3 seconds and put a timer under the ROI rectangle using cv2.putText and don't predict anything till those 3 seconds are passed but I don't know how, here's my code:

import numpy as np
from keras.models import model_from_json
import cv2

# Loading the model
json_file = open("model-bw.json", "r")
model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(model_json)
# load weights into new model
loaded_model.load_weights("model-bw.h5")
print("Loaded model from disk")

cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)

# Category dictionary
categories = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 
'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z', 26: 'DEL', 27: 'SPACE', 28: 'NOTHING'}

def mapper(val):
    return categories[val]

msg = ""
while True:
    _, frame = cap.read()
    # Simulating mirror image
    frame = cv2.flip(frame, 1)
    
    # Region of interest
    cv2.rectangle(frame, (800, 100), (1230, 500), (255, 255, 255), 2)
    roi = frame[100:500, 800:1230]
    
    # Resizing the ROI so it can be fed to the model for prediction
    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    _, roi = cv2.threshold(roi, 120, 255, cv2.THRESH_OTSU)
    test_image = cv2.resize(roi, (128, 128)) 
    cv2.imshow("test", test_image)

    # # Sorting based on top prediction
    pred = loaded_model.predict(np.array([test_image]))
    move_code = np.argmax(pred[0])
    prediction = mapper(move_code)

    cv2.putText(frame, "Predicted: "+prediction, (912, 532), cv2.FONT_HERSHEY_PLAIN, 2, (0,255,255), 1)

    msg += prediction
    # Displaying the sentence 
    cv2.putText(frame, "Sentence:"+msg, (50, 700), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 255), 1) 
    cv2.imshow("Frame", frame)
   
    
    interrupt = cv2.waitKey(1)
    if interrupt & 0xFF == 27: # esc key
        break
        
 
cap.release()
cv2.destroyAllWindows()
marky004
  • 67
  • 8

1 Answers1

0

Just like @Christoph Rackwitz suggested, time elapsed would be my approach as well.

import time

timer_seconds = 3
prediction_start_time = time.time()
predictions_number_done = 0  # just for breaking my loop

for i in range(10 ** 10):
    time_elapsed_seconds = time.time() - prediction_start_time
    if i == 0 or time_elapsed_seconds >= timer_seconds:
        if i == 0:
            print('--- first iteration: not waiting {} seconds ---'.format(timer_seconds))
        else:
            print("--- {:.5f} seconds passed ---".format(time_elapsed_seconds))
        # Let's say here is your prediction
        prediction_start_time = time.time()  # capture time of this prediction
        predictions_number_done += 1
    if predictions_number_done >= 5:
        break
gilad eini
  • 360
  • 2
  • 6