3sys_path = [
'/workspace/smlab',
'/workspace']
5 if path
not in sys.path:
12from smutils.utils_vis
import draw_single_bbox_and_label
13from smutils.utils_vis
import vis_pose_coco_skeleton
14from smutils.utils_os
import search_file, create_directory
15from smutils.utils_data
import load_labelmap
16from smutils.utils_video
import make_video
17from smutils.utils_data
import save_pkl_data
21det_cfg_path =
'/workspace/smlab/smdetect/configs/yolo/yolov8.py'
22track_cfg_path =
'/workspace/smlab/smtrack/configs/bytetrack/bytetrack_base.py'
23pose_cfg_path =
'/workspace/smlab/smpose/configs/mmpose/hrnet_trt.py'
24action_cfg_path =
'/workspace/InnoTest/models/posec3d_action.py'
25posture_cfg_path =
'/workspace/InnoTest/models/posec3d_pose.py'
29det_model = smrunner.build_model(det_cfg_path)
30track_model = smrunner.build_model(track_cfg_path)
31pose_model = smrunner.build_model(pose_cfg_path)
32action_model = smrunner.build_model(action_cfg_path).to(device)
33posture_model = smrunner.build_model(posture_cfg_path).to(device)
44labelmap[
'action_upper'] = load_labelmap(
'/workspace/smlab/smaction/datasets/safemotion_v22_upper_action.txt')
45labelmap[
'action_lower'] = load_labelmap(
'/workspace/smlab/smaction/datasets/safemotion_v22_lower_action.txt')
46labelmap[
'pose'] = load_labelmap(
'/workspace/smlab/smaction/datasets/safemotion_v22_pose.txt')
47labelmap[
'hand'] = load_labelmap(
'/workspace/smlab/smaction/datasets/safemotion_v22_hand.txt')
50cvt_lower_map = [0, 1, 2, 0, 4, 5, 0, 7, 8, 9, 0, 11]
51cvt_upper_map = [0, 0, 0, 3, 0, 0]
52cvt_pose_map = [0, 0, 0, 0, 4, 4, 6, 0]
53cvt_hand_map = [0, 0, 0, 3]
56action_vis_param = dict(
57 box_color = (0, 255, 0),
59 txt_color = (255, 255, 255),
66 box_color = (0, 255, 0),
68 txt_color = (255, 255, 255),
75video_folder =
'/media/safemotion/HDD5/pjm_test/ai_park_test_video'
76_, video_path_list = search_file(video_folder,
'.mp4')
79save_folder =
'/media/safemotion/HDD5/pjm_test/ai_park_test_fps20'
80create_directory(save_folder)
83tmp_save_folder =
'/media/safemotion/HDD5/pjm_test/tmp_clip_images_fps20'
84create_directory(tmp_save_folder)
85shutil.rmtree(tmp_save_folder)
88for video_idx, video_path
in enumerate(video_path_list):
91 create_directory(tmp_save_folder)
94 video_name = video_path.split(
'/')[-1]
95 save_video_path = os.path.join(save_folder, video_name)
98 cap = cv2.VideoCapture(video_path)
100 if not cap.isOpened():
101 print(f
"Error: Could not open video({video_path}).")
105 total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
106 fps = cap.get(cv2.CAP_PROP_FPS)
107 print(f
'{video_path} : {total_frames} frames, {fps} FPS')
110 action_k = int(fps+0.5)*2
111 pose_k = int(fps+0.5)//2
116 track_model.tracker.reset()
119 ret, image = cap.read()
123 print(
"Reached the end of the video or encountered an error.")
127 print(f
'{video_name} : {frame_id+1} / {total_frames}', end=
'\r')
128 img_shape = image.shape[:2]
131 vis_img = image.copy()
134 det_result = det_model.run_detector(image)
135 track_result = track_model.run_tracker(det_result[
'det_bboxes'], det_result[
'det_labels'], frame_id)
136 pose_result = pose_model.run_detector(image, track_result[
'track_bboxes'][0])
139 for pose
in pose_result:
140 track_id = int(pose[
'track_id'])
141 if track_id
not in track_data:
142 track_data[track_id] = []
143 pose[
'frame_id'] = frame_id
144 track_data[track_id].append(pose)
147 vis_img = vis_pose_coco_skeleton(vis_img, pose_result)
151 for track_id, pose_q
in track_data.items():
153 bbox = pose_q[-1][
'bbox']
155 last_frame = pose_q[-1][
'frame_id']
156 if last_frame == frame_id:
157 label = f
'{track_id:3d}'
158 vis_img = draw_single_bbox_and_label(vis_img, bbox, label, **vis_param)
162 if frame_id - last_frame > action_k*0.5:
163 delete_list.append(track_id)
168 if frame_id - last_frame > 15:
172 action_result = action_model.run_recognizer(pose_q, action_k, action_sample, device=device)
175 if action_result
is None:
179 posture_result = posture_model.run_recognizer(pose_q[-pose_k:], pose_k, pose_sample,
False, device=device)
183 if last_frame - pose_q[0][
'frame_id'] >= action_k:
187 pose_label = cvt_pose_map[posture_result[
'pred_pose']]
188 upper_label = cvt_upper_map[action_result[
'pred_action_upper']]
189 lower_label = cvt_lower_map[action_result[
'pred_action_lower']]
190 hand_label = cvt_hand_map[posture_result[
'pred_hand']]
193 label = f
"{track_id:3d}: {labelmap['pose'][pose_label]}"
196 label += f
"/{labelmap['hand'][hand_label]}"
198 label += f
"/{labelmap['action_upper'][upper_label]}"
200 label += f
"/{labelmap['action_lower'][lower_label]}"
202 cv2.putText(vis_img, label, (10, txt_pos_y), cv2.FONT_HERSHEY_DUPLEX, 1.5, (0, 0, 0), 10, 1)
203 cv2.putText(vis_img, label, (10, txt_pos_y), cv2.FONT_HERSHEY_DUPLEX, 1.5, (0, 255, 255), 2, 1)
206 vis_img = draw_single_bbox_and_label(vis_img, bbox, f
'{track_id:3d}', **action_vis_param)
209 for track_id
in delete_list:
210 del track_data[track_id]
216 name = f
'{frame_id:09d}.jpg'
217 save_path = os.path.join(tmp_save_folder, name)
218 cv2.imwrite(save_path, vis_img)
221 make_video(tmp_save_folder, save_video_path, fps=fps, half=
False)
222 shutil.rmtree(tmp_save_folder)