john000z commited on
Commit
e14f5fb
·
1 Parent(s): c1a78eb
Files changed (1) hide show
  1. model.py +165 -0
model.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import os
4
+ import shutil
5
+ from datetime import datetime
6
+ from timeit import default_timer as timer
7
+ from utools import load_relevant_data_subset,mark_pred
8
+ from utools import softmax
9
+ import mediapipe as mp
10
+ import cv2
11
+ import json
12
+ N=3
13
+
14
+ ROWS_PER_FRAME=543
15
+ with open('sign_to_prediction_index_map_cn.json', 'r') as f:
16
+ person_dict = json.load(f)
17
+ inverse_dict=dict([val,key] for key,val in person_dict.items())
18
+
19
+
20
+ def r_holistic(video_path):
21
+ mp_drawing = mp.solutions.drawing_utils
22
+ mp_drawing_styles = mp.solutions.drawing_styles
23
+ mp_holistic = mp.solutions.holistic
24
+ frame_number = 0
25
+ frame = []
26
+ type_ = []
27
+ index = []
28
+ x = []
29
+ y = []
30
+ z = []
31
+ cap=cv2.VideoCapture(video_path)
32
+ frame_width = int(cap.get(3))
33
+ frame_height = int(cap.get(4))
34
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
35
+ frame_size = (frame_width, frame_height)
36
+ fourcc = cv2.VideoWriter_fourcc(*"VP80") #cv2.VideoWriter_fourcc('H.264')
37
+ output_video = "output_recorded_holistic.webm"
38
+ out = cv2.VideoWriter(output_video, fourcc, int(fps/N), frame_size)
39
+ with mp_holistic.Holistic(min_detection_confidence=0.5,min_tracking_confidence=0.5) as holistic:
40
+ n=0
41
+ while cap.isOpened():
42
+ frame_number+=1
43
+ n+=1
44
+ ret, image = cap.read()
45
+ if not ret:
46
+ break
47
+ if n%N==0:
48
+ image.flags.writeable = False
49
+ image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
50
+ #mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=RGB_frame)
51
+ results = holistic.process(image)
52
+
53
+ # Draw landmark annotation on the image.
54
+ image.flags.writeable = True
55
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
56
+ mp_drawing.draw_landmarks(
57
+ image,
58
+ results.face_landmarks,
59
+ mp_holistic.FACEMESH_CONTOURS,
60
+ landmark_drawing_spec=None,
61
+ connection_drawing_spec=mp_drawing_styles
62
+ .get_default_face_mesh_contours_style())
63
+ mp_drawing.draw_landmarks(
64
+ image,
65
+ results.pose_landmarks,
66
+ mp_holistic.POSE_CONNECTIONS,
67
+ landmark_drawing_spec=mp_drawing_styles
68
+ .get_default_pose_landmarks_style())
69
+ # Flip the image horizontally for a selfie-view display.
70
+ #if cv2.waitKey(5) & 0xFF == 27:
71
+ out.write(image)
72
+
73
+ if(results.face_landmarks is None):
74
+ for i in range(468):
75
+ frame.append(frame_number)
76
+ type_.append("face")
77
+ index.append(ind)
78
+ x.append(None)
79
+ y.append(None)
80
+ z.append(None)
81
+ else:
82
+ for ind,val in enumerate(results.face_landmarks.landmark):
83
+ frame.append(frame_number)
84
+ type_.append("face")
85
+ index.append(ind)
86
+ x.append(val.x)
87
+ y.append(val.y)
88
+ z.append(val.z)
89
+ #left hand
90
+ if(results.left_hand_landmarks is None):
91
+ for i in range(21):
92
+ frame.append(frame_number)
93
+ type_.append("left_hand")
94
+ index.append(ind)
95
+ x.append(None)
96
+ y.append(None)
97
+ z.append(None)
98
+ else:
99
+ for ind,val in enumerate(results.left_hand_landmarks.landmark):
100
+ frame.append(frame_number)
101
+ type_.append("left_hand")
102
+ index.append(ind)
103
+ x.append(val.x)
104
+ y.append(val.y)
105
+ z.append(val.z)
106
+ #pose
107
+ if(results.pose_landmarks is None):
108
+ for i in range(33):
109
+ frame.append(frame_number)
110
+ type_.append("pose")
111
+ index.append(ind)
112
+ x.append(None)
113
+ y.append(None)
114
+ z.append(None)
115
+ else:
116
+ for ind,val in enumerate(results.pose_landmarks.landmark):
117
+ frame.append(frame_number)
118
+ type_.append("pose")
119
+ index.append(ind)
120
+ x.append(val.x)
121
+ y.append(val.y)
122
+ z.append(val.z)
123
+ #right hand
124
+ if(results.right_hand_landmarks is None):
125
+ for i in range(21):
126
+ frame.append(frame_number)
127
+ type_.append("right_hand")
128
+ index.append(ind)
129
+ x.append(None)
130
+ y.append(None)
131
+ z.append(None)
132
+ else:
133
+ for ind,val in enumerate(results.right_hand_landmarks.landmark):
134
+ frame.append(frame_number)
135
+ type_.append("right_hand")
136
+ index.append(ind)
137
+ x.append(val.x)
138
+ y.append(val.y)
139
+ z.append(val.z)
140
+ #break
141
+ cap.release()
142
+ out.release()
143
+ cv2.destroyAllWindows()
144
+ df1 = pd.DataFrame({
145
+ "frame" : frame,
146
+ "type" : type_,
147
+ "landmark_index" : index,
148
+ "x" : x,
149
+ "y" : y,
150
+ "z" : z
151
+ })
152
+ aa=load_relevant_data_subset(df1)
153
+ model_path_1='model_1.tflite'
154
+ model_path_2='model_2.tflite'
155
+ model_path_3='model_3.tflite'
156
+ #interpreter = tflite.Interpreter(model_path_1)
157
+ #found_signatures = list(interpreter.get_signature_list().keys())
158
+ #prediction_fn = interpreter.get_signature_runner("serving_default")
159
+ output_1 = mark_pred(model_path_1,aa)
160
+ output_2 = mark_pred(model_path_2,aa)
161
+ output_3 = mark_pred(model_path_3,aa)
162
+ output=softmax(output_1['outputs'])+softmax(output_2['outputs'])+softmax(output_3['outputs'])
163
+ sign = output.argmax()
164
+ lb = inverse_dict.get(sign)
165
+ yield output_video,lb