-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (137 loc) · 6.28 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/6/15 15:06
# @Author : shen.p
# @Site :
# @File : main.py.py
# @Software: PyCharm
import time
import cv2
import numpy as np
from mediapipe.python.solutions import pose as mp_pose
from mediapipe.python.solutions import drawing_utils as mp_drawing
import os
import tqdm
import yaml
import datetime
# 指定视频路径和输出名称
from bodyclassfy import PoseClassifier
from count import RepetitionCounter
from data_process import show_image
from posture import FullBodyPoseEmbedder, EMADictSmoothing
from visual import PoseClassificationVisualizer
with open("./config.yml", "r", encoding='utf-8') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader)
video_path = cfg["input_path"]
class_name = cfg["specify_class"]
if video_path == 0:
strtime = time.strftime('%Y-%m-%d')
out_video_path = cfg["output_path"] + strtime + ".mp4"
else:
outname = video_path.split("/")[-1].split(".")
out_video_path = cfg["output_path"] + outname[0] + "_out." + outname[1]
video_cap = cv2.VideoCapture(video_path)
# Get some video parameters to generate output video with classificaiton.
video_n_frames = video_cap.get(cv2.CAP_PROP_FRAME_COUNT)
video_fps = video_cap.get(cv2.CAP_PROP_FPS)
video_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
video_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if __name__ == '__main__':
# Folder with pose class CSVs. That should be the same folder you using while
# building classifier to output CSVs.
pose_samples_folder = cfg["pose_samples_folder"]
# Initialize tracker.
pose_tracker = mp_pose.Pose(upper_body_only=False)
# Initialize embedder.
pose_embedder = FullBodyPoseEmbedder()
# Initialize classifier.
# Ceck that you are using the same parameters as during bootstrapping.
pose_classifier = PoseClassifier(
pose_samples_folder=pose_samples_folder,
pose_embedder=pose_embedder,
top_n_by_max_distance=30,
top_n_by_mean_distance=10)
# # Uncomment to validate target poses used by classifier and find outliers.
# outliers = pose_classifier.find_pose_sample_outliers()
# print('Number of pose sample outliers (consider removing them): ', len(outliers))
# Initialize EMA smoothing.
pose_classification_filter = EMADictSmoothing(
window_size=10,
alpha=0.2)
# 指定动作的两个阈值
repetition_counter = RepetitionCounter(
class_name=class_name,
enter_threshold=cfg["enter_thread"],
exit_threshold=cfg["exit_thread"])
# Initialize renderer.
pose_classification_visualizer = PoseClassificationVisualizer(
class_name=class_name,
plot_x_max=video_n_frames,
# Graphic looks nicer if it's the same as `top_n_by_mean_distance`.
plot_y_max=10)
# Open output video.
out_video = cv2.VideoWriter(out_video_path, cv2.VideoWriter_fourcc(*'mp4v'), video_fps, (video_width, video_height))
frame_idx = 0
output_frame = None
with tqdm.tqdm(total=video_n_frames, position=0, leave=True) as pbar:
while True:
# Get next frame of the video.
success, input_frame = video_cap.read()
if not success:
break
# Run pose tracker.
input_frame = cv2.cvtColor(input_frame, cv2.COLOR_BGR2RGB)
result = pose_tracker.process(image=input_frame)
pose_landmarks = result.pose_landmarks
# Draw pose prediction.
output_frame = input_frame.copy()
if pose_landmarks is not None:
mp_drawing.draw_landmarks(
image=output_frame,
landmark_list=pose_landmarks,
connections=mp_pose.POSE_CONNECTIONS)
if pose_landmarks is not None:
# Get landmarks.
frame_height, frame_width = output_frame.shape[0], output_frame.shape[1]
pose_landmarks = np.array([[lmk.x * frame_width, lmk.y * frame_height, lmk.z * frame_width]
for lmk in pose_landmarks.landmark], dtype=np.float32)
assert pose_landmarks.shape == (33, 3), 'Unexpected landmarks shape: {}'.format(pose_landmarks.shape)
# Classify the pose on the current frame.
pose_classification = pose_classifier(pose_landmarks)
# Smooth classification using EMA.
pose_classification_filtered = pose_classification_filter(pose_classification)
# Count repetitions.
repetitions_count = repetition_counter(pose_classification_filtered)
else:
# No pose => no classification on current frame.
pose_classification = None
# Still add empty classification to the filter to maintaing correct
# smoothing for future frames.
pose_classification_filtered = pose_classification_filter(dict())
pose_classification_filtered = None
# Don't update the counter presuming that person is 'frozen'. Just
# take the latest repetitions count.
repetitions_count = repetition_counter.n_repeats
# Draw classification plot and repetition counter.
output_frame = pose_classification_visualizer(
frame=output_frame,
pose_classification=pose_classification,
pose_classification_filtered=pose_classification_filtered,
repetitions_count=repetitions_count)
# Save the output frame.
out_video.write(cv2.cvtColor(np.array(output_frame), cv2.COLOR_RGB2BGR))
# Show intermediate frames of the video to track progress.
if frame_idx % 50 == 0:
show_image(output_frame)
frame_idx += 1
pbar.update()
cv2.imshow('press q to break', cv2.cvtColor(np.array(output_frame), cv2.COLOR_RGB2BGR))
if cv2.waitKey(1) in [ord('q'), 27]: # 按键盘上的q或esc退出(在英文输入法下)
break
# Close output video.
out_video.release()
# Release MediaPipe resources.
pose_tracker.close()
# Show the last frame of the video.
if output_frame is not None:
show_image(output_frame)