forked from Pevooo/SignLanguageToText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
77 lines (54 loc) · 1.82 KB
/
inference.py
File metadata and controls
77 lines (54 loc) · 1.82 KB
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
import cv2 as cv
import keras
import numpy as np
import mediapipe as mp
import tensorflow as tf
model = keras.models.load_model('SignLanguage9C991.h5')
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
cap = cv.VideoCapture(0)
CATEGORIES = ["hello", "yes", "no", "iloveyou", "callme", "goodjob", "highfive", "dislike", "peace"]
def get_landmark_array(landmarks):
arr = []
for l in landmarks:
arr.append([l.x, l.y, l.z])
return np.array(arr)
def get_rectangle(landmarks, h, w):
x_max, y_max, x_min, y_min = 0, 0, w, h
for lm in landmarks:
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
return x_min, y_min, x_max, y_max
def predict(img, draw_rectangle=True):
imgRGB = cv.cvtColor(img, cv.COLOR_BGR2RGB)
result = hands.process(imgRGB)
if result.multi_hand_landmarks:
for hand in result.multi_hand_landmarks:
x_min, y_min, x_max, y_max = None, None, None, None
# Drawing Rectangle
if draw_rectangle:
h, w, c = img.shape
x_min, y_min, x_max, y_max = get_rectangle(hand.landmark, h, w)
cv.rectangle(img, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
# Predicting
data = get_landmark_array(hand.landmark)
data = data.reshape((1, 21, 3))
result = model.predict(data)
if draw_rectangle:
cv.putText(img, f"{CATEGORIES[result.argmax()]}, {int(max(result[0]) * 100)}%" , (x_min, y_min), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
break # Delete Break if you want to detect more than one hand
return True
else:
return False
while True:
success, frame = cap.read()
predict(frame)
cv.imshow("Video", frame)
cv.waitKey(1)