-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (58 loc) · 2.71 KB
/
app.py
File metadata and controls
69 lines (58 loc) · 2.71 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
import cv2
from utils import *
import mediapipe as mp
from configs import *
from landmark_classification import HandLandmarkClassificator
def init_hand_landmarks_model():
mp_hands = mp.solutions.hands
mp_hands_landmark_detector = mp_hands.Hands(
static_image_mode= True,
max_num_hands= num_hands,
min_detection_confidence= min_detection_confidence,
min_tracking_confidence= min_tracking_confidence,
)
return mp_hands_landmark_detector
def process_landmarks(frame, landmarks, classificator):
for hand_landmark, _ in zip(landmarks.multi_hand_landmarks, landmarks.multi_handedness):
hand_bounding_box = bounding_box_from_landmarks(frame, hand_landmark)
list_of_landmarks = convert_landmarks_to_list(frame, hand_landmark)
label_predict = classificator.classify(list_of_landmarks)
frame = draw_landmarks(frame, list_of_landmarks)
frame = draw_bounding_box(frame, hand_bounding_box, label_predict)
cv2.imshow('frame', frame)
def save_landmarks_mode(frame, landmarks, key):
for hand_landmark, _ in zip(landmarks.multi_hand_landmarks, landmarks.multi_handedness):
hand_bounding_box = bounding_box_from_landmarks(frame, hand_landmark)
frame = draw_bounding_box(frame, hand_bounding_box)
list_of_landmarks = convert_landmarks_to_list(frame, hand_landmark)
frame = draw_landmarks(frame, list_of_landmarks)
if is_number_key(key):
save_land_mark(list_of_landmarks, int(chr(key & 0xFF)), landmark_csv_path)
cv2.imshow('frame', frame)
def capture_webcam(cap_width, cap_height):
webcam = init_video_capture_device(cap_height=cap_height, cap_width=cap_width)
cvFpsCalc = CvFpsCalc(buffer_len=10)
mp_hands_landmark_detector = init_hand_landmarks_model()
landmark_classificator= HandLandmarkClassificator()
global mode
while(True):
_, frame = webcam.read()
input_key = cv2.waitKey(10)
frame = preprocess_frame(frame, cvFpsCalc, mode)
landmarks = mp_hands_landmark_detector.process(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
if exist_landmarks(landmarks) and mode == "predict":
process_landmarks(frame, landmarks, landmark_classificator)
elif exist_landmarks(landmarks) and mode == "save":
save_landmarks_mode(frame, landmarks, input_key)
else:
cv2.imshow('frame', frame)
if pressed_key(input_key, 'q'):
break
if pressed_key(input_key, 's'):
mode = "save"
if pressed_key(input_key, 'p'):
mode= "predict"
webcam.release()
cv2.destroyAllWindows()
capture_webcam(frame_width, frame_height)