forked from JimmyIITR/Project-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
74 lines (58 loc) · 2.08 KB
/
inference.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
import cv2
import mediapipe as mp
import numpy as np
from keras.models import load_model
import time
from keras.layers import LeakyReLU
def inference():
labels = np.load('labels.npy')
model = load_model('model.h5')
cap = cv2.VideoCapture(0)
holis = mp.solutions.holistic
hands = mp.solutions.hands
drawing = mp.solutions.drawing_utils
holisO = holis.Holistic(static_image_mode=False)
while True:
x = []
stime = time.time()
_, frame = cap.read()
frame = cv2.flip(frame, 1)
res = holisO.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
drawing.draw_landmarks(frame, res.left_hand_landmarks, hands.HAND_CONNECTIONS)
drawing.draw_landmarks(frame, res.right_hand_landmarks, hands.HAND_CONNECTIONS)
drawing.draw_landmarks(frame, res.face_landmarks, holis.FACEMESH_TESSELATION)
if res.face_landmarks:
if not(res.left_hand_landmarks):
for i in range(42):
x.append(0.0)
else:
lox, loy = res.left_hand_landmarks.landmark[8].x, res.left_hand_landmarks.landmark[8].y
for i in res.left_hand_landmarks.landmark:
x.append(i.x - lox)
x.append(i.y - loy)
if not(res.right_hand_landmarks):
for i in range(42):
x.append(0.0)
else:
rox, roy = res.right_hand_landmarks.landmark[8].x, res.right_hand_landmarks.landmark[8].y
for i in res.right_hand_landmarks.landmark:
x.append(i.x - rox)
x.append(i.y - roy)
for i in res.face_landmarks.landmark:
x.append(i.x - res.face_landmarks.landmark[1].x)
x.append(i.y - res.face_landmarks.landmark[1].y)
pred = model.predict(np.array([x]))
cv2.putText(frame, labels[np.argmax(pred)], (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
etime = time.time()
cv2.putText(frame, f"{int(1/(etime-stime))}", (50,340), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
try:
img = cv2.imread(f"emojis/{labels[np.argmax(pred)]}.png")
img = cv2.resize(img, (100,100))
frame[100:200, :100] = cv2.addWeighted(frame[100:200, :100], 0, img, 1, 0)
except:
print("no")
cv2.imshow("window", frame)
if cv2.waitKey(1) == 27:
cap.release()
cv2.destroyAllWindows()
break