image_demo.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. """Image Demo.
  3. This script adopts a new infenence class, currently supports image path,
  4. np.array and folder input formats, and will support video and webcam
  5. in the future.
  6. Example:
  7. Save visualizations and predictions results::
  8. python demo/image_demo.py demo/demo.jpg rtmdet-s
  9. python demo/image_demo.py demo/demo.jpg \
  10. configs/rtmdet/rtmdet_s_8xb32-300e_coco.py \
  11. --weights rtmdet_s_8xb32-300e_coco_20220905_161602-387a891e.pth
  12. Visualize prediction results::
  13. python demo/image_demo.py demo/demo.jpg rtmdet-ins-s --show
  14. python demo/image_demo.py demo/demo.jpg rtmdet-ins_s_8xb32-300e_coco \
  15. --show
  16. """
  17. from argparse import ArgumentParser
  18. from mmengine.logging import print_log
  19. from mmdet.apis import DetInferencer
  20. def parse_args():
  21. parser = ArgumentParser()
  22. parser.add_argument(
  23. 'inputs', type=str, help='Input image file or folder path.')
  24. parser.add_argument(
  25. 'model',
  26. type=str,
  27. help='Config or checkpoint .pth file or the model name '
  28. 'and alias defined in metafile. The model configuration '
  29. 'file will try to read from .pth if the parameter is '
  30. 'a .pth weights file.')
  31. parser.add_argument('--weights', default=None, help='Checkpoint file')
  32. parser.add_argument(
  33. '--out-dir',
  34. type=str,
  35. default='outputs',
  36. help='Output directory of images or prediction results.')
  37. parser.add_argument(
  38. '--device', default='cuda:0', help='Device used for inference')
  39. parser.add_argument(
  40. '--pred-score-thr',
  41. type=float,
  42. default=0.3,
  43. help='bbox score threshold')
  44. parser.add_argument(
  45. '--batch-size', type=int, default=1, help='Inference batch size.')
  46. parser.add_argument(
  47. '--show',
  48. action='store_true',
  49. help='Display the image in a popup window.')
  50. parser.add_argument(
  51. '--no-save-vis',
  52. action='store_true',
  53. help='Do not save detection vis results')
  54. parser.add_argument(
  55. '--no-save-pred',
  56. action='store_true',
  57. help='Do not save detection json results')
  58. parser.add_argument(
  59. '--print-result',
  60. action='store_true',
  61. help='Whether to print the results.')
  62. parser.add_argument(
  63. '--palette',
  64. default='none',
  65. choices=['coco', 'voc', 'citys', 'random', 'none'],
  66. help='Color palette used for visualization')
  67. call_args = vars(parser.parse_args())
  68. if call_args['no_save_vis'] and call_args['no_save_pred']:
  69. call_args['out_dir'] = ''
  70. if call_args['model'].endswith('.pth'):
  71. print_log('The model is a weight file, automatically '
  72. 'assign the model to --weights')
  73. call_args['weights'] = call_args['model']
  74. call_args['model'] = None
  75. init_kws = ['model', 'weights', 'device', 'palette']
  76. init_args = {}
  77. for init_kw in init_kws:
  78. init_args[init_kw] = call_args.pop(init_kw)
  79. return init_args, call_args
  80. def main():
  81. init_args, call_args = parse_args()
  82. # TODO: Video and Webcam are currently not supported and
  83. # may consume too much memory if your input folder has a lot of images.
  84. # We will be optimized later.
  85. inferencer = DetInferencer(**init_args)
  86. inferencer(**call_args)
  87. if call_args['out_dir'] != '' and not (call_args['no_save_vis']
  88. and call_args['no_save_pred']):
  89. print_log(f'results have been saved at {call_args["out_dir"]}')
  90. if __name__ == '__main__':
  91. main()