forked from open-mmlab/mmskeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
236 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
processor_cfg: | ||
name: ".processor.skeleton_dataset.build" | ||
gpus: 1 | ||
worker_per_gpu: 2 | ||
video_dir: resource/data_example | ||
out_dir: "data/dataset_example" | ||
category_annotation: resource/category_annotation_example.json | ||
|
||
detection_cfg: | ||
model_cfg: configs/mmdet/cascade_rcnn_r50_fpn_1x.py | ||
checkpoint_file: mmskeleton://mmdet/cascade_rcnn_r50_fpn_20e | ||
bbox_thre: 0.8 | ||
estimation_cfg: | ||
model_cfg: configs/pose_estimation/hrnet/pose_hrnet_w32_256x192_test.yaml | ||
checkpoint_file: mmskeleton://pose_estimation/pose_hrnet_w32_256x192 | ||
data_cfg: | ||
image_size: | ||
- 192 | ||
- 256 | ||
pixel_std: 200 | ||
image_mean: | ||
- 0.485 | ||
- 0.456 | ||
- 0.406 | ||
image_std: | ||
- 0.229 | ||
- 0.224 | ||
- 0.225 | ||
post_process: true | ||
|
||
argparse_cfg: | ||
gpus: | ||
bind_to: processor_cfg.gpus | ||
help: number of gpus | ||
video_dir: | ||
bind_to: processor_cfg.video_dir | ||
help: folder for videos | ||
worker_per_gpu: | ||
bind_to: processor_cfg.worker_per_gpu | ||
help: number of workers for each gpu | ||
skeleton_model: | ||
bind_to: processor_cfg.estimation_cfg.model_cfg | ||
skeleton_checkpoint: | ||
bind_to: processor_cfg.estimation_cfg.checkpoint_file | ||
detection_model: | ||
bind_to: processor_cfg.detection_cfg.model_cfg | ||
detection_checkpoint: | ||
bind_to: processor_cfg.detection_cfg.checkpoint_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import os | ||
import json | ||
import mmcv | ||
import numpy as np | ||
import ntpath | ||
from mmskeleton.apis.estimation import init_pose_estimator, inference_pose_estimator | ||
from multiprocessing import current_process, Process, Manager | ||
from mmskeleton.utils import cache_checkpoint | ||
from mmskeleton.processor.apis import save_batch_image_with_joints | ||
from mmcv.utils import ProgressBar | ||
|
||
pose_estimators = dict() | ||
|
||
|
||
def worker(inputs, results, gpu, detection_cfg, estimation_cfg): | ||
worker_id = current_process()._identity[0] - 1 | ||
global pose_estimators | ||
if worker_id not in pose_estimators: | ||
pose_estimators[worker_id] = init_pose_estimator(detection_cfg, | ||
estimation_cfg, | ||
device=gpu) | ||
while True: | ||
idx, image = inputs.get() | ||
|
||
# end signal | ||
if image is None: | ||
return | ||
|
||
res = inference_pose_estimator(pose_estimators[worker_id], image) | ||
res['frame_index'] = idx | ||
results.put(res) | ||
|
||
|
||
def build(detection_cfg, | ||
estimation_cfg, | ||
video_dir, | ||
out_dir, | ||
gpus=1, | ||
worker_per_gpu=1, | ||
video_max_length=10000, | ||
category_annotation=None): | ||
|
||
cache_checkpoint(detection_cfg.checkpoint_file) | ||
cache_checkpoint(estimation_cfg.checkpoint_file) | ||
if not os.path.isdir(out_dir): | ||
os.makedirs(out_dir) | ||
|
||
if category_annotation is None: | ||
video_categories = dict() | ||
else: | ||
with open(category_annotation) as f: | ||
video_categories = json.load(f)['annotations'] | ||
|
||
inputs = Manager().Queue(video_max_length) | ||
results = Manager().Queue(video_max_length) | ||
|
||
num_worker = gpus * worker_per_gpu | ||
procs = [] | ||
for i in range(num_worker): | ||
p = Process(target=worker, | ||
args=(inputs, results, i % gpus, detection_cfg, | ||
estimation_cfg)) | ||
procs.append(p) | ||
p.start() | ||
|
||
video_file_list = os.listdir(video_dir) | ||
prog_bar = ProgressBar(len(video_file_list)) | ||
for video_file in video_file_list: | ||
|
||
reader = mmcv.VideoReader(os.path.join(video_dir, video_file)) | ||
video_frames = reader[:video_max_length] | ||
annotations = [] | ||
|
||
for i, image in enumerate(video_frames): | ||
inputs.put((i, image)) | ||
|
||
for i in range(len(video_frames)): | ||
t = results.get() | ||
if not t['has_return']: | ||
continue | ||
|
||
num_person = len(t['joint_preds']) | ||
assert len(t['person_bbox']) == num_person | ||
|
||
for j in range(num_person): | ||
keypoints = np.concatenate( | ||
(t['joint_preds'][j], t['joint_scores'][j]), 1) | ||
keypoints = keypoints.reshape(-1).round().astype(int).tolist() | ||
|
||
person_info = dict(person_bbox=t['person_bbox'] | ||
[j].round().astype(int).tolist(), | ||
frame_index=t['frame_index'], | ||
id=j, | ||
person_id=None, | ||
keypoints=keypoints, | ||
num_keypoints=t['joint_scores'].shape[1]) | ||
|
||
annotations.append(person_info) | ||
|
||
# output results | ||
annotations = sorted(annotations, key=lambda x: x['frame_index']) | ||
category_id = video_categories[video_file][ | ||
'category_id'] if video_file in video_categories else -1 | ||
info = dict(video_name=video_file, | ||
resolution=reader.resolution, | ||
version='1.0') | ||
video_info = dict(info=info, | ||
category_id=category_id, | ||
annotations=annotations) | ||
with open(os.path.join(out_dir, video_file + '.json'), 'w') as f: | ||
json.dump(video_info, f) | ||
|
||
prog_bar.update() | ||
|
||
# send end signals | ||
for p in procs: | ||
inputs.put((-1, None)) | ||
# wait to finish | ||
for p in procs: | ||
p.join() | ||
|
||
print('\nBuild skeleton dataset to {}.'.format(out_dir)) | ||
return video_info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"categories": [ | ||
"skateboarding", | ||
"clean_and_jerk", | ||
"ta_chi" | ||
], | ||
"annotations": { | ||
"clean_and_jerk.mp4": { | ||
"category_id": 1 | ||
}, | ||
"skateboarding.mp4": { | ||
"category_id": 0 | ||
}, | ||
"ta_chi.mp4": { | ||
"category_id": 2 | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.