diff --git a/Task 4/prediction.py b/Task 4/prediction.py new file mode 100644 index 0000000..5151d08 --- /dev/null +++ b/Task 4/prediction.py @@ -0,0 +1,54 @@ +from keras.models import model_from_json +import operator +import cv2 + +json_file = open("model-bw.json", "r") +model_json = json_file.read() +json_file.close() +loaded_model = model_from_json(model_json) +loaded_model.load_weights("model-bw.h5") +print("Loaded model from disk") + +cap = cv2.VideoCapture(0) + +categories = {0: 'ZERO', 1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 5: 'FIVE'} + +while True: + _, frame = cap.read() + frame = cv2.flip(frame, 1) + + x1 = int(0.5*frame.shape[1]) + y1 = 10 + x2 = frame.shape[1]-10 + y2 = int(0.5*frame.shape[1]) + + cv2.putText(frame, "Expressando - TDOC 2021", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,0), 3) + cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,255,255) ,3) + roi = frame[y1:y2, x1:x2] + + roi = cv2.resize(roi, (64, 64)) + roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) + cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3) + + _, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY) + cv2.imshow("ROI", test_image) + + result = loaded_model.predict(test_image.reshape(1, 64, 64, 1)) + prediction = {'ZERO': result[0][0], + 'ONE': result[0][1], + 'TWO': result[0][2], + 'THREE': result[0][3], + 'FOUR': result[0][4], + 'FIVE': result[0][5]} + prediction = sorted(prediction.items(), key=operator.itemgetter(1), reverse=True) #(0.9 = FIVE, 0.7, 0.6, 0.5, 0.4) + cv2.putText(frame, "PREDICTION:", (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2) + cv2.putText(frame, prediction[0][0], (80, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2) + cv2.imshow("Frame", frame) + + interrupt = cv2.waitKey(10) + if interrupt & 0xFF == 27: + break + + +cap.release() +cv2.destroyAllWindows()