-
Notifications
You must be signed in to change notification settings - Fork 0
/
jycm.py
121 lines (109 loc) · 4.25 KB
/
jycm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
import cv2
import mediapipe as mp
from keras.models import load_model
import numpy as np
import time
import pandas as pd
model = load_model(r"C:\Users\prash\OneDrive\Desktop\jay_projecf\jay_projecf\jayanthModel.h5")
mphands = mp.solutions.hands
hands = mphands.Hands()
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
_, frame = cap.read()
h, w, c = frame.shape
img_counter = 0
analysisframe = ''
letterpred = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
print('lets start the process')
while True:
_, frame = cap.read()
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
analysisframe = frame
showframe = analysisframe
cv2.imshow("Frame", showframe)
framergbanalysis = cv2.cvtColor(analysisframe, cv2.COLOR_BGR2RGB)
resultanalysis = hands.process(framergbanalysis)
hand_landmarksanalysis = resultanalysis.multi_hand_landmarks
if hand_landmarksanalysis:
for handLMsanalysis in hand_landmarksanalysis:
x_max = 0
y_max = 0
x_min = w
y_min = h
for lmanalysis in handLMsanalysis.landmark:
x, y = int(lmanalysis.x * w), int(lmanalysis.y * h)
if x > x_max:
x_max = x
if x < x_min:
x_min = x
if y > y_max:
y_max = y
if y < y_min:
y_min = y
y_min -= 20
y_max += 20
x_min -= 20
x_max += 20
# Extract the region of interest (hand) for prediction
analysisframe_roi = cv2.cvtColor(analysisframe, cv2.COLOR_BGR2GRAY)
analysisframe_roi = analysisframe_roi[y_min:y_max, x_min:x_max]
analysisframe_roi = cv2.resize(analysisframe_roi, (28, 28))
# Preprocess the image for prediction
analysisframe_roi = analysisframe_roi / 255.0
analysisframe_roi = np.expand_dims(analysisframe_roi, axis=0)
analysisframe_roi = np.expand_dims(analysisframe_roi, axis=-1)
# Make prediction
prediction = model.predict(analysisframe_roi)
predarray = np.array(prediction[0])
letter_prediction_dict = {letterpred[i]: predarray[i] for i in range(len(letterpred))}
predarrayordered = sorted(predarray, reverse=True)
high1 = predarrayordered[0]
high2 = predarrayordered[1]
high3 = predarrayordered[2]
for key,value in letter_prediction_dict.items():
if value==high1:
print("Predicted Character 1: ", key)
print('Confidence 1: ', 100*value)
elif value==high2:
print("Predicted Character 2: ", key)
print('Confidence 2: ', 100*value)
elif value==high3:
print("Predicted Character 3: ", key)
print('Confidence 3: ', 100*value)
time.sleep(5)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(framergb)
hand_landmarks = result.multi_hand_landmarks
if hand_landmarks:
for handLMs in hand_landmarks:
x_max = 0
y_max = 0
x_min = w
y_min = h
for lm in handLMs.landmark:
x, y = int(lm.x * w), int(lm.y * h)
if x > x_max:
x_max = x
if x < x_min:
x_min = x
if y > y_max:
y_max = y
if y < y_min:
y_min = y
y_min -= 20
y_max += 20
x_min -= 20
x_max += 20
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
cv2.imshow("Frame", frame)
cap.release()
cv2.destroyAllWindows()