-
Notifications
You must be signed in to change notification settings - Fork 0
/
hands_gesture_recog.py
104 lines (77 loc) · 2.19 KB
/
hands_gesture_recog.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
import cv2
import mediapipe as mp
import pandas as pd
import os
import numpy as np
def image_processed(hand_img):
# Image processing
# 1. Convert BGR to RGB
img_rgb = cv2.cvtColor(hand_img, cv2.COLOR_BGR2RGB)
# 2. Flip the img in Y-axis
img_flip = cv2.flip(img_rgb, 1)
# accessing MediaPipe solutions
mp_hands = mp.solutions.hands
# Initialize Hands
hands = mp_hands.Hands(static_image_mode=True,
max_num_hands=1, min_detection_confidence=0.7)
# Results
output = hands.process(img_flip)
hands.close()
try:
data = output.multi_hand_landmarks[0]
#print(data)
data = str(data)
data = data.strip().split('\n')
garbage = ['landmark {', ' visibility: 0.0', ' presence: 0.0', '}']
without_garbage = []
for i in data:
if i not in garbage:
without_garbage.append(i)
clean = []
for i in without_garbage:
i = i.strip()
clean.append(i[2:])
for i in range(0, len(clean)):
clean[i] = float(clean[i])
return(clean)
except:
return(np.zeros([1,63], dtype=int)[0])
import pickle
# load model
with open('model.pkl', 'rb') as f:
svm = pickle.load(f)
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
i = 0
while True:
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# frame = cv.flip(frame,1)
data = image_processed(frame)
# print(data.shape)
data = np.array(data)
y_pred = svm.predict(data.reshape(-1,63))
print(y_pred)
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (50, 100)
# fontScale
fontScale = 3
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 5
# Using cv2.putText() method
frame = cv2.putText(frame, str(y_pred[0]), org, font,
fontScale, color, thickness, cv2.LINE_AA)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()