-
Notifications
You must be signed in to change notification settings - Fork 3
/
inference.py
214 lines (189 loc) · 7.62 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
from keras.models import load_model
import cv2
import mediapipe as mp
import numpy as np
import pandas as pd
import math
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", type=str, required=True,
help="path to saved .h5 model, eg: dir/model.h5")
ap.add_argument("-c", "--conf", type=float, required=True,
help="min prediction conf to detect pose class (0<conf<1)")
ap.add_argument("-i", "--source", type=str, required=True,
help="path to sample image")
ap.add_argument("--save", action='store_true',
help="Save video")
args = vars(ap.parse_args())
source = args["source"]
path_saved_model = args["model"]
threshold = args["conf"]
save = args['save']
##############
torso_size_multiplier = 2.5
n_landmarks = 33
n_dimensions = 3
landmark_names = [
'nose',
'left_eye_inner', 'left_eye', 'left_eye_outer',
'right_eye_inner', 'right_eye', 'right_eye_outer',
'left_ear', 'right_ear',
'mouth_left', 'mouth_right',
'left_shoulder', 'right_shoulder',
'left_elbow', 'right_elbow',
'left_wrist', 'right_wrist',
'left_pinky_1', 'right_pinky_1',
'left_index_1', 'right_index_1',
'left_thumb_2', 'right_thumb_2',
'left_hip', 'right_hip',
'left_knee', 'right_knee',
'left_ankle', 'right_ankle',
'left_heel', 'right_heel',
'left_foot_index', 'right_foot_index',
]
class_names = [
'Chair', 'Cobra', 'Dog',
'Tree', 'Warrior'
]
##############
mp_pose = mp.solutions.pose
pose = mp_pose.Pose()
col_names = []
for i in range(n_landmarks):
name = mp_pose.PoseLandmark(i).name
name_x = name + '_X'
name_y = name + '_Y'
name_z = name + '_Z'
name_v = name + '_V'
col_names.append(name_x)
col_names.append(name_y)
col_names.append(name_z)
col_names.append(name_v)
# Load saved model
model = load_model(path_saved_model, compile=True)
if source.endswith(('.jpg', '.jpeg', '.png')):
path_to_img = source
# Load sample Image
img = cv2.imread(path_to_img)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = pose.process(img_rgb)
if result.pose_landmarks:
lm_list = []
for landmarks in result.pose_landmarks.landmark:
# Preprocessing
max_distance = 0
lm_list.append(landmarks)
center_x = (lm_list[landmark_names.index('right_hip')].x +
lm_list[landmark_names.index('left_hip')].x)*0.5
center_y = (lm_list[landmark_names.index('right_hip')].y +
lm_list[landmark_names.index('left_hip')].y)*0.5
shoulders_x = (lm_list[landmark_names.index('right_shoulder')].x +
lm_list[landmark_names.index('left_shoulder')].x)*0.5
shoulders_y = (lm_list[landmark_names.index('right_shoulder')].y +
lm_list[landmark_names.index('left_shoulder')].y)*0.5
for lm in lm_list:
distance = math.sqrt((lm.x - center_x)**2 + (lm.y - center_y)**2)
if(distance > max_distance):
max_distance = distance
torso_size = math.sqrt((shoulders_x - center_x) **
2 + (shoulders_y - center_y)**2)
max_distance = max(torso_size*torso_size_multiplier, max_distance)
pre_lm = list(np.array([[(landmark.x-center_x)/max_distance, (landmark.y-center_y)/max_distance,
landmark.z/max_distance, landmark.visibility] for landmark in lm_list]).flatten())
data = pd.DataFrame([pre_lm], columns=col_names)
predict = model.predict(data)[0]
if max(predict) > threshold:
pose_class = class_names[predict.argmax()]
print('predictions: ', predict)
print('predicted Pose Class: ', pose_class)
else:
pose_class = 'Unknown Pose'
print('[INFO] Predictions is below given Confidence!!')
# Show Result
img = cv2.putText(
img, f'{class_names[predict.argmax()]}',
(40, 50), cv2.FONT_HERSHEY_PLAIN,
2, (255, 0, 255), 2
)
if save:
os.makedirs('ImageOutput', exist_ok=True)
img_full_name = os.path.split(path_to_img)[1]
img_name = os.path.splitext(img_full_name)[0]
path_to_save_img = f'ImageOutput/{img_name}.jpg'
cv2.imwrite(f'{path_to_save_img}', img)
print(f'[INFO] Output Image Saved in {path_to_save_img}')
cv2.imshow('Output Image', img)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
print('[INFO] Inference on Test Image is Ended...')
else:
# Web-cam
if source.isnumeric():
source = int(source)
cap = cv2.VideoCapture(source)
source_width = int(cap.get(3))
source_height = int(cap.get(4))
# Write Video
if save:
out_video = cv2.VideoWriter('output.avi',
cv2.VideoWriter_fourcc(*'MJPG'),
10, (source_width, source_height))
while True:
success, img = cap.read()
if not success:
print('[ERROR] Failed to Read Video feed')
break
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = pose.process(img_rgb)
if result.pose_landmarks:
lm_list = []
for landmarks in result.pose_landmarks.landmark:
# Preprocessing
max_distance = 0
lm_list.append(landmarks)
center_x = (lm_list[landmark_names.index('right_hip')].x +
lm_list[landmark_names.index('left_hip')].x)*0.5
center_y = (lm_list[landmark_names.index('right_hip')].y +
lm_list[landmark_names.index('left_hip')].y)*0.5
shoulders_x = (lm_list[landmark_names.index('right_shoulder')].x +
lm_list[landmark_names.index('left_shoulder')].x)*0.5
shoulders_y = (lm_list[landmark_names.index('right_shoulder')].y +
lm_list[landmark_names.index('left_shoulder')].y)*0.5
for lm in lm_list:
distance = math.sqrt((lm.x - center_x) **
2 + (lm.y - center_y)**2)
if(distance > max_distance):
max_distance = distance
torso_size = math.sqrt((shoulders_x - center_x) **
2 + (shoulders_y - center_y)**2)
max_distance = max(torso_size*torso_size_multiplier, max_distance)
pre_lm = list(np.array([[(landmark.x-center_x)/max_distance, (landmark.y-center_y)/max_distance,
landmark.z/max_distance, landmark.visibility] for landmark in lm_list]).flatten())
data = pd.DataFrame([pre_lm], columns=col_names)
predict = model.predict(data)[0]
if max(predict) > threshold:
pose_class = class_names[predict.argmax()]
print('predictions: ', predict)
print('predicted Pose Class: ', pose_class)
else:
pose_class = 'Unknown Pose'
print('[INFO] Predictions is below given Confidence!!')
# Show Result
img = cv2.putText(
img, f'{pose_class}',
(40, 50), cv2.FONT_HERSHEY_PLAIN,
2, (255, 0, 255), 2
)
# Write Video
if save:
out_video.write(img)
cv2.imshow('Output Image', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
if save:
out_video.release()
print("[INFO] Out video Saved as 'output.avi'")
cv2.destroyAllWindows()
print('[INFO] Inference on Videostream is Ended...')