forked from open-mmlab/mmpose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam_demo.py
72 lines (51 loc) · 1.86 KB
/
webcam_demo.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
# Copyright (c) OpenMMLab. All rights reserved.
import logging
from argparse import ArgumentParser
from mmcv import Config, DictAction
from mmpose.apis.webcam import WebcamExecutor
from mmpose.apis.webcam.nodes import model_nodes
def parse_args():
parser = ArgumentParser('Webcam executor configs')
parser.add_argument(
'--config', type=str, default='demo/webcam_cfg/pose_estimation.py')
parser.add_argument(
'--cfg-options',
nargs='+',
action=DictAction,
default={},
help='Override settings in the config. The key-value pair '
'in xxx=yyy format will be merged into config file. For example, '
"'--cfg-options executor_cfg.camera_id=1'")
parser.add_argument(
'--debug', action='store_true', help='Show debug information.')
parser.add_argument(
'--cpu', action='store_true', help='Use CPU for model inference.')
parser.add_argument(
'--cuda', action='store_true', help='Use GPU for model inference.')
return parser.parse_args()
def set_device(cfg: Config, device: str):
"""Set model device in config.
Args:
cfg (Config): Webcam config
device (str): device indicator like "cpu" or "cuda:0"
"""
device = device.lower()
assert device == 'cpu' or device.startswith('cuda:')
for node_cfg in cfg.executor_cfg.nodes:
if node_cfg.type in model_nodes.__all__:
node_cfg.update(device=device)
return cfg
def run():
args = parse_args()
cfg = Config.fromfile(args.config)
cfg.merge_from_dict(args.cfg_options)
if args.debug:
logging.basicConfig(level=logging.DEBUG)
if args.cpu:
cfg = set_device(cfg, 'cpu')
if args.cuda:
cfg = set_device(cfg, 'cuda:0')
webcam_exe = WebcamExecutor(**cfg.executor_cfg)
webcam_exe.run()
if __name__ == '__main__':
run()