webcam_api_demo.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import logging
  3. import warnings
  4. from argparse import ArgumentParser
  5. from mmengine import Config, DictAction
  6. from mmpose.apis.webcam import WebcamExecutor
  7. from mmpose.apis.webcam.nodes import model_nodes
  8. def parse_args():
  9. parser = ArgumentParser('Webcam executor configs')
  10. parser.add_argument(
  11. '--config', type=str, default='demo/webcam_cfg/human_pose.py')
  12. parser.add_argument(
  13. '--cfg-options',
  14. nargs='+',
  15. action=DictAction,
  16. default={},
  17. help='Override settings in the config. The key-value pair '
  18. 'in xxx=yyy format will be merged into config file. For example, '
  19. "'--cfg-options executor_cfg.camera_id=1'")
  20. parser.add_argument(
  21. '--debug', action='store_true', help='Show debug information.')
  22. parser.add_argument(
  23. '--cpu', action='store_true', help='Use CPU for model inference.')
  24. parser.add_argument(
  25. '--cuda', action='store_true', help='Use GPU for model inference.')
  26. return parser.parse_args()
  27. def set_device(cfg: Config, device: str):
  28. """Set model device in config.
  29. Args:
  30. cfg (Config): Webcam config
  31. device (str): device indicator like "cpu" or "cuda:0"
  32. """
  33. device = device.lower()
  34. assert device == 'cpu' or device.startswith('cuda:')
  35. for node_cfg in cfg.executor_cfg.nodes:
  36. if node_cfg.type in model_nodes.__all__:
  37. node_cfg.update(device=device)
  38. return cfg
  39. def run():
  40. warnings.warn('The Webcam API will be deprecated in future. ',
  41. DeprecationWarning)
  42. args = parse_args()
  43. cfg = Config.fromfile(args.config)
  44. cfg.merge_from_dict(args.cfg_options)
  45. if args.debug:
  46. logging.basicConfig(level=logging.DEBUG)
  47. if args.cpu:
  48. cfg = set_device(cfg, 'cpu')
  49. if args.cuda:
  50. cfg = set_device(cfg, 'cuda:0')
  51. webcam_exe = WebcamExecutor(**cfg.executor_cfg)
  52. webcam_exe.run()
  53. if __name__ == '__main__':
  54. run()