-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
338 lines (283 loc) · 15.7 KB
/
demo.py
File metadata and controls
338 lines (283 loc) · 15.7 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
"""
Copyright (C) 2020-2023 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging as log
import sys
from argparse import ArgumentParser, SUPPRESS
from pathlib import Path
from time import perf_counter
import cv2
import numpy as np
import math
import winsound as sd
sys.path.append(str(Path(__file__).resolve().parents[0] / 'common/python'))
sys.path.append(str(Path(__file__).resolve().parents[0] / 'common/python/openvino/model_zoo'))
from model_api.models import ImageModel, OutputTransform
from model_api.performance_metrics import PerformanceMetrics
from model_api.pipelines import get_user_config, AsyncPipeline
from model_api.adapters import create_core, OpenvinoAdapter
import monitors
from images_capture import open_images_capture
from helpers import resolution, log_latency_per_stage
log.basicConfig(format='[ %(levelname)s ] %(message)s', level=log.DEBUG, stream=sys.stdout)
ARCHITECTURES = {
'ae': 'HPE-assosiative-embedding',
'higherhrnet': 'HPE-assosiative-embedding',
'openpose': 'openpose'
}
def build_argparser():
parser = ArgumentParser(add_help=False)
args = parser.add_argument_group('Options')
args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
args.add_argument('-m', '--model', help='Required. Path to an .xml file with a trained model.',
required=True, type=Path)
args.add_argument('-at', '--architecture_type', help='Required. Specify model\' architecture type.',
type=str, required=True, choices=('ae', 'higherhrnet', 'openpose'))
args.add_argument('-i', '--input', required=True,
help='Required. An input to process. The input must be a single image, '
'a folder of images, video file or camera id.')
args.add_argument('--loop', default=False, action='store_true',
help='Optional. Enable reading the input in a loop.')
args.add_argument('-o', '--output', required=False,
help='Optional. Name of the output file(s) to save.')
args.add_argument('-limit', '--output_limit', required=False, default=1000, type=int,
help='Optional. Number of frames to store in output. '
'If 0 is set, all frames are stored.')
args.add_argument('-d', '--device', default='CPU', type=str,
help='Optional. Specify the target device to infer on; CPU or GPU is '
'acceptable. The demo will look for a suitable plugin for device specified. '
'Default value is CPU.')
common_model_args = parser.add_argument_group('Common model options')
common_model_args.add_argument('-t', '--prob_threshold', default=0.1, type=float,
help='Optional. Probability threshold for poses filtering.')
common_model_args.add_argument('--tsize', default=None, type=int,
help='Optional. Target input size. This demo implements image pre-processing '
'pipeline that is common to human pose estimation approaches. Image is first '
'resized to some target size and then the network is reshaped to fit the input '
'image shape. By default target image size is determined based on the input '
'shape from IR. Alternatively it can be manually set via this parameter. Note '
'that for OpenPose-like nets image is resized to a predefined height, which is '
'the target size in this case. For Associative Embedding-like nets target size '
'is the length of a short first image side.')
common_model_args.add_argument('--layout', type=str, default=None,
help='Optional. Model inputs layouts. '
'Ex. NCHW or input0:NCHW,input1:NC in case of more than one input.')
infer_args = parser.add_argument_group('Inference options')
infer_args.add_argument('-nireq', '--num_infer_requests', help='Optional. Number of infer requests',
default=0, type=int)
infer_args.add_argument('-nstreams', '--num_streams',
help='Optional. Number of streams to use for inference on the CPU or/and GPU in throughput '
'mode (for HETERO and MULTI device cases use format '
'<device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>).',
default='', type=str)
infer_args.add_argument('-nthreads', '--num_threads', default=None, type=int,
help='Optional. Number of threads to use for inference on CPU (including HETERO cases).')
io_args = parser.add_argument_group('Input/output options')
io_args.add_argument('-no_show', '--no_show', help="Optional. Don't show output.", action='store_true')
io_args.add_argument('--output_resolution', default=None, type=resolution,
help='Optional. Specify the maximum output window resolution '
'in (width x height) format. Example: 1280x720. '
'Input frame size used by default.')
io_args.add_argument('-u', '--utilization_monitors', default='', type=str,
help='Optional. List of monitors to show initially.')
debug_args = parser.add_argument_group('Debug options')
debug_args.add_argument('-r', '--raw_output_message', help='Optional. Output inference results raw values showing.',
default=False, action='store_true')
return parser
def beepsound():
fr = 2000 # range : 37 ~ 32767
du = 1000 # 1000 ms ==1second
sd.Beep(fr, du) # winsound.Beep(frequency, duration)
# new_code
def detect_head_pose(poses, frame):
"""
머리의 움직임을 추적하여 고개 숙임 여부를 감지하는 함수
"""
# 머리의 포즈에서 필요한 관절 인덱스를 지정
for pose in poses:
eyes = pose[1:2].mean()
ears = pose[3:4].mean()
# 머리의 방향 벡터 계산
head_vector = eyes - ears
# 머리의 기울기 각도 계산
# cv2.putText(frame, str(head_vector), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# 숙임 여부에 따라 경고 출력
if head_vector > -15: # 숙임을 나타내는 임계값 설정
cv2.putText(frame, 'Wrong Poses', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
beepsound()
default_skeleton = ((15, 13), (13, 11), (16, 14), (14, 12), (11, 12), (5, 11), (6, 12), (5, 6),
(5, 7), (6, 8), (7, 9), (8, 10), (1, 2), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6))
colors = (
(255, 0, 0), (255, 0, 255), (170, 0, 255), (255, 0, 85),
(255, 0, 170), (85, 255, 0), (255, 170, 0), (0, 255, 0),
(255, 255, 0), (0, 255, 85), (170, 255, 0), (0, 85, 255),
(0, 255, 170), (0, 0, 255), (0, 255, 255), (85, 0, 255),
(0, 170, 255))
def draw_poses(img, poses, point_score_threshold, output_transform, skeleton=default_skeleton, draw_ellipses=False):
img = output_transform.resize(img)
if poses.size == 0:
return img
stick_width = 4
img_limbs = np.copy(img)
for pose in poses:
points = pose[:, :2].astype(np.int32)
points = output_transform.scale(points)
points_scores = pose[:, 2]
# Draw joints.
for i, (p, v) in enumerate(zip(points, points_scores)):
if v > point_score_threshold:
cv2.circle(img, tuple(p), 1, colors[i], 2)
cv2.putText(img, str(i), tuple(p), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Draw limbs.
for i, j in skeleton:
if points_scores[i] > point_score_threshold and points_scores[j] > point_score_threshold:
if draw_ellipses:
middle = (points[i] + points[j]) // 2
vec = points[i] - points[j]
length = np.sqrt((vec * vec).sum())
angle = int(np.arctan2(vec[1], vec[0]) * 180 / np.pi)
polygon = cv2.ellipse2Poly(tuple(middle), (int(length / 2), min(int(length / 50), stick_width)),
angle, 0, 360, 1)
cv2.fillConvexPoly(img_limbs, polygon, colors[j])
else:
cv2.line(img_limbs, tuple(points[i]), tuple(points[j]), color=colors[j], thickness=stick_width)
cv2.addWeighted(img, 0.4, img_limbs, 0.6, 0, dst=img)
return img
def print_raw_results(poses, scores, frame_id):
log.debug(' ------------------- Frame # {} ------------------ '.format(frame_id))
for pose, pose_score in zip(poses, scores):
pose_str = ' '.join('({:.2f}, {:.2f}, {:.2f})'.format(p[0], p[1], p[2]) for p in pose)
log.debug('{} | {:.2f}'.format(pose_str, pose_score))
def main():
args = build_argparser().parse_args()
cap = open_images_capture(args.input, args.loop)
next_frame_id = 1
next_frame_id_to_show = 0
metrics = PerformanceMetrics()
render_metrics = PerformanceMetrics()
video_writer = cv2.VideoWriter()
plugin_config = get_user_config(args.device, args.num_streams, args.num_threads)
model_adapter = OpenvinoAdapter(create_core(), args.model, device=args.device, plugin_config=plugin_config,
max_num_requests=args.num_infer_requests, model_parameters = {'input_layouts': args.layout})
start_time = perf_counter()
frame = cap.read()
if frame is None:
raise RuntimeError("Can't read an image from the input")
config = {
'target_size': args.tsize,
'aspect_ratio': frame.shape[1] / frame.shape[0],
'confidence_threshold': args.prob_threshold,
'padding_mode': 'center' if args.architecture_type == 'higherhrnet' else None, # the 'higherhrnet' and 'ae' specific
'delta': 0.5 if args.architecture_type == 'higherhrnet' else None, # the 'higherhrnet' and 'ae' specific
}
model = ImageModel.create_model(ARCHITECTURES[args.architecture_type], model_adapter, config)
model.log_layers_info()
hpe_pipeline = AsyncPipeline(model)
hpe_pipeline.submit_data(frame, 0, {'frame': frame, 'start_time': start_time})
output_transform = OutputTransform(frame.shape[:2], args.output_resolution)
if args.output_resolution:
output_resolution = output_transform.new_resolution
else:
output_resolution = (frame.shape[1], frame.shape[0])
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(output_resolution[0] / 4), round(output_resolution[1] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'), cap.fps(),
output_resolution):
raise RuntimeError("Can't open video writer")
while True:
if hpe_pipeline.callback_exceptions:
raise hpe_pipeline.callback_exceptions[0]
# Process all completed requests
results = hpe_pipeline.get_result(next_frame_id_to_show)
if results:
(poses, scores), frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if len(poses) and args.raw_output_message:
print_raw_results(poses, scores, next_frame_id_to_show)
# # Check body posture and display warning if necessary new_code
# if detect_body_posture(poses):
# cv2.putText(frame, 'Wrong Poses', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
presenter.drawGraphs(frame)
rendering_start_time = perf_counter()
frame = draw_poses(frame, poses[:2], args.prob_threshold, output_transform)
render_metrics.update(rendering_start_time)
metrics.update(start_time, frame)
# Check if specific poses are detected and display warnings
# for pose, score in zip(poses, scores):
# if is_pose_detected(pose, pose_keypoints['arms_crossed']):
# cv2.putText(frame, 'Warning: Arms Crossed', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# if is_pose_detected(pose, pose_keypoints['hands_on_hips']):
# cv2.putText(frame, 'Warning: Hands on Hips', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# 고개 숙임 감지 함수 호출
detect_head_pose(poses, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
next_frame_id_to_show += 1
if not args.no_show:
cv2.imshow('Pose estimation results', frame)
key = cv2.waitKey(1)
ESC_KEY = 27
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
continue
if hpe_pipeline.is_ready():
# Get new image/frame
start_time = perf_counter()
frame = cap.read()
if frame is None:
break
# Submit for inference
hpe_pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
next_frame_id += 1
else:
# Wait for empty request
hpe_pipeline.await_any()
hpe_pipeline.await_all()
if hpe_pipeline.callback_exceptions:
raise hpe_pipeline.callback_exceptions[0]
# Process completed requests
for next_frame_id_to_show in range(next_frame_id_to_show, next_frame_id):
results = hpe_pipeline.get_result(next_frame_id_to_show)
(poses, scores), frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if len(poses) and args.raw_output_message:
print_raw_results(poses, scores, next_frame_id_to_show)
presenter.drawGraphs(frame)
rendering_start_time = perf_counter()
frame = draw_poses(frame, poses, args.prob_threshold, output_transform)
render_metrics.update(rendering_start_time)
metrics.update(start_time, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
if not args.no_show:
cv2.imshow('Pose estimation results', frame)
key = cv2.waitKey(1)
ESC_KEY = 27
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
metrics.log_total()
log_latency_per_stage(cap.reader_metrics.get_latency(),
hpe_pipeline.preprocess_metrics.get_latency(),
hpe_pipeline.inference_metrics.get_latency(),
hpe_pipeline.postprocess_metrics.get_latency(),
render_metrics.get_latency())
for rep in presenter.reportMeans():
log.info(rep)
if __name__ == '__main__':
sys.exit(main() or 0)