blob: 8635a40a9e9520e5fc4c381f7e95936a8bbcb9d3 [file] [log] [blame]
Jakub Sujak433a5952020-06-17 15:35:03 +01001# Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
2# SPDX-License-Identifier: MIT
3
4"""
5Object detection demo that takes a video stream from a device, runs inference
6on each frame producing bounding boxes and labels around detected objects,
7and displays a window with the latest processed frame.
8"""
9
10import os
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000011import sys
12script_dir = os.path.dirname(__file__)
13sys.path.insert(1, os.path.join(script_dir, '..', 'common'))
14
Jakub Sujak433a5952020-06-17 15:35:03 +010015import cv2
Jakub Sujak433a5952020-06-17 15:35:03 +010016from argparse import ArgumentParser
17
18from ssd import ssd_processing, ssd_resize_factor
19from yolo import yolo_processing, yolo_resize_factor
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000020from utils import dict_labels
21from cv_utils import init_video_stream_capture, preprocess, draw_bounding_boxes
22from network_executor import ArmnnNetworkExecutor
Jakub Sujak433a5952020-06-17 15:35:03 +010023
24
25def get_model_processing(model_name: str, video: cv2.VideoCapture, input_binding_info: tuple):
26 """
27 Gets model-specific information such as model labels and decoding and processing functions.
28 The user can include their own network and functions by adding another statement.
29
30 Args:
31 model_name: Name of type of supported model.
32 video: Video capture object, contains information about data source.
33 input_binding_info: Contains shape of model input layer, used for scaling bounding boxes.
34
35 Returns:
36 Model labels, decoding and processing functions.
37 """
38 if model_name == 'ssd_mobilenet_v1':
Jakub Sujak885cf8c2020-11-24 16:39:21 +000039 return ssd_processing, ssd_resize_factor(video)
Jakub Sujak433a5952020-06-17 15:35:03 +010040 elif model_name == 'yolo_v3_tiny':
Jakub Sujak885cf8c2020-11-24 16:39:21 +000041 return yolo_processing, yolo_resize_factor(video, input_binding_info)
Jakub Sujak433a5952020-06-17 15:35:03 +010042 else:
43 raise ValueError(f'{model_name} is not a valid model name')
44
45
46def main(args):
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000047 video = init_video_stream_capture(args.video_source)
48 executor = ArmnnNetworkExecutor(args.model_file_path, args.preferred_backends)
49
Jakub Sujak885cf8c2020-11-24 16:39:21 +000050 process_output, resize_factor = get_model_processing(args.model_name, video, executor.input_binding_info)
51 labels = dict_labels(args.label_path, include_rgb=True)
Jakub Sujak433a5952020-06-17 15:35:03 +010052
53 while True:
54 frame_present, frame = video.read()
55 frame = cv2.flip(frame, 1) # Horizontally flip the frame
56 if not frame_present:
57 raise RuntimeError('Error reading frame from video stream')
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000058 input_tensors = preprocess(frame, executor.input_binding_info)
59 print("Running inference...")
60 output_result = executor.run(input_tensors)
61 detections = process_output(output_result)
Jakub Sujak433a5952020-06-17 15:35:03 +010062 draw_bounding_boxes(frame, detections, resize_factor, labels)
63 cv2.imshow('PyArmNN Object Detection Demo', frame)
64 if cv2.waitKey(1) == 27:
65 print('\nExit key activated. Closing video...')
66 break
67 video.release(), cv2.destroyAllWindows()
68
69
70if __name__ == '__main__':
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000071 parser = ArgumentParser()
72 parser.add_argument('--video_source', type=int, default=0,
73 help='Device index to access video stream. Defaults to primary device camera at index 0')
74 parser.add_argument('--model_file_path', required=True, type=str,
75 help='Path to the Object Detection model to use')
76 parser.add_argument('--model_name', required=True, type=str,
77 help='The name of the model being used. Accepted options: ssd_mobilenet_v1, yolo_v3_tiny')
Jakub Sujak885cf8c2020-11-24 16:39:21 +000078 parser.add_argument('--label_path', required=True, type=str,
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000079 help='Path to the labelset for the provided model file')
80 parser.add_argument('--preferred_backends', type=str, nargs='+', default=['CpuAcc', 'CpuRef'],
81 help='Takes the preferred backends in preference order, separated by whitespace, '
82 'for example: CpuAcc GpuAcc CpuRef. Accepted options: [CpuAcc, CpuRef, GpuAcc]. '
83 'Defaults to [CpuAcc, CpuRef]')
84 args = parser.parse_args()
Jakub Sujak433a5952020-06-17 15:35:03 +010085 main(args)