blob: fc3e2147214b688516eea48b0e8cc9acfb143215 [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 file, runs inference on each frame producing
6bounding boxes and labels around detected objects, and saves the processed video.
7"""
8
9import os
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000010import sys
11script_dir = os.path.dirname(__file__)
12sys.path.insert(1, os.path.join(script_dir, '..', 'common'))
13
Jakub Sujak433a5952020-06-17 15:35:03 +010014import cv2
Jakub Sujak433a5952020-06-17 15:35:03 +010015from tqdm import tqdm
16from 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_file_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':
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000039 labels = os.path.join(script_dir, 'ssd_labels.txt')
Jakub Sujak433a5952020-06-17 15:35:03 +010040 return labels, ssd_processing, ssd_resize_factor(video)
41 elif model_name == 'yolo_v3_tiny':
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000042 labels = os.path.join(script_dir, 'yolo_labels.txt')
Jakub Sujak433a5952020-06-17 15:35:03 +010043 return labels, yolo_processing, yolo_resize_factor(video, input_binding_info)
44 else:
45 raise ValueError(f'{model_name} is not a valid model name')
46
47
48def main(args):
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000049 video, video_writer, frame_count = init_video_file_capture(args.video_file_path, args.output_video_file_path)
50
51 executor = ArmnnNetworkExecutor(args.model_file_path, args.preferred_backends)
52 labels, process_output, resize_factor = get_model_processing(args.model_name, video, executor.input_binding_info)
53 labels = dict_labels(labels if args.label_path is None else args.label_path, include_rgb=True)
Jakub Sujak433a5952020-06-17 15:35:03 +010054
55 for _ in tqdm(frame_count, desc='Processing frames'):
56 frame_present, frame = video.read()
57 if not frame_present:
58 continue
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000059 input_tensors = preprocess(frame, executor.input_binding_info)
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 video_writer.write(frame)
64 print('Finished processing frames')
65 video.release(), video_writer.release()
66
67
68if __name__ == '__main__':
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000069 parser = ArgumentParser()
70 parser.add_argument('--video_file_path', required=True, type=str,
71 help='Path to the video file to run object detection on')
72 parser.add_argument('--model_file_path', required=True, type=str,
73 help='Path to the Object Detection model to use')
74 parser.add_argument('--model_name', required=True, type=str,
75 help='The name of the model being used. Accepted options: ssd_mobilenet_v1, yolo_v3_tiny')
76 parser.add_argument('--label_path', type=str,
77 help='Path to the labelset for the provided model file')
78 parser.add_argument('--output_video_file_path', type=str,
79 help='Path to the output video file with detections added in')
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)