from tensorflow.keras.preprocessing import image import numpy as np import cv2 #we are starting our web cam webcam=cv2.VideoCapture(0) cap = cv2.VideoCapture(0) # Category dictionary categories = {0: 'ZERO', 1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 5: 'FIVE'} s="" d={} p="" count=0 while True: _, frame = cap.read() # Simulating mirror image frame = cv2.flip(frame, 1) # Got this from collect-data.py # Coordinates of the ROI x1 = int(0.5*frame.shape[1]) y1 = 10 x2 = frame.shape[1]-10 y2 = int(0.5*frame.shape[1]) # Drawing the ROI # The increment/decrement by 1 is to compensate for the bounding box cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,0,0) ,1) # Extracting the ROI roi = frame[y1:y2, x1:x2] # Resizing the ROI so it can be fed to the model for prediction roi = cv2.resize(roi, (64, 64)) roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) _, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY) cv2.imshow("test", test_image) # Batch of 1 result = loaded_model.predict(test_image.reshape(1, 64, 64, 1)) prediction = {'FIVE': result[0][0], 'FOUR': result[0][1], 'ONE': result[0][2], 'THREE': result[0][3], 'TWO': result[0][4], 'ZERO': result[0][5]} max_key = max(prediction, key=prediction.get) cv2.putText(test_image,max_key,(x1,y1),cv2.FONT_HERSHEY_SIMPLEX,1,(255, 0, 0),2) print(max_key) cv2.imshow("Frame", frame) interrupt = cv2.waitKey(2) if interrupt & 0xFF == 27: # esc key break cap.release() cv2.destroyAllWindows() |