|
| 1 | +import argparse |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import torch |
| 5 | + |
| 6 | +from mmdet.apis import inference_detector, init_detector, show_result |
| 7 | + |
| 8 | + |
| 9 | +def parse_args(): |
| 10 | + parser = argparse.ArgumentParser(description='MMDetection webcam demo') |
| 11 | + parser.add_argument('config', help='test config file path') |
| 12 | + parser.add_argument('checkpoint', help='checkpoint file') |
| 13 | + parser.add_argument('--device', type=int, default=0, help='CUDA device id') |
| 14 | + parser.add_argument( |
| 15 | + '--camera-id', type=int, default=0, help='camera device id') |
| 16 | + parser.add_argument( |
| 17 | + '--score-thr', type=float, default=0.5, help='bbox score threshold') |
| 18 | + args = parser.parse_args() |
| 19 | + return args |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + args = parse_args() |
| 24 | + |
| 25 | + model = init_detector( |
| 26 | + args.config, args.checkpoint, device=torch.device('cuda', args.device)) |
| 27 | + |
| 28 | + camera = cv2.VideoCapture(args.camera_id) |
| 29 | + |
| 30 | + print('Press "Esc", "q" or "Q" to exit.') |
| 31 | + while True: |
| 32 | + ret_val, img = camera.read() |
| 33 | + result = inference_detector(model, img) |
| 34 | + |
| 35 | + ch = cv2.waitKey(1) |
| 36 | + if ch == 27 or ch == ord('q') or ch == ord('Q'): |
| 37 | + break |
| 38 | + |
| 39 | + show_result( |
| 40 | + img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1) |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == '__main__': |
| 44 | + main() |
0 commit comments