blob: 8bc2f0de381a06024d840e2bc42d08d7d3cd1629 [file] [log] [blame]
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "CvVideoFrameReader.hpp"
7#include "CvWindowOutput.hpp"
8#include "CvVideoFileWriter.hpp"
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +01009#include "ObjectDetectionPipeline.hpp"
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010010#include "CmdArgsParser.hpp"
11
12#include <fstream>
13#include <iostream>
14#include <map>
15#include <random>
16
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010017const std::string MODEL_NAME = "--model-name";
18const std::string VIDEO_FILE_PATH = "--video-file-path";
19const std::string MODEL_FILE_PATH = "--model-file-path";
20const std::string OUTPUT_VIDEO_FILE_PATH = "--output-video-file-path";
21const std::string LABEL_PATH = "--label-path";
22const std::string PREFERRED_BACKENDS = "--preferred-backends";
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +000023const std::string PROFILING_ENABLED = "--profiling_enabled";
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010024const std::string HELP = "--help";
25
26/*
27 * The accepted options for this Object detection executable
28 */
29static std::map<std::string, std::string> CMD_OPTIONS = {
30 {VIDEO_FILE_PATH, "[REQUIRED] Path to the video file to run object detection on"},
31 {MODEL_FILE_PATH, "[REQUIRED] Path to the Object Detection model to use"},
32 {LABEL_PATH, "[REQUIRED] Path to the label set for the provided model file. "
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +000033 "Label file should be an ordered list, separated by a new line."},
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010034 {MODEL_NAME, "[REQUIRED] The name of the model being used. Accepted options: YOLO_V3_TINY, SSD_MOBILE"},
35 {OUTPUT_VIDEO_FILE_PATH, "[OPTIONAL] Path to the output video file with detections added in. "
36 "If specified will save file to disk, else displays the output to screen"},
37 {PREFERRED_BACKENDS, "[OPTIONAL] Takes the preferred backends in preference order, separated by comma."
38 " For example: CpuAcc,GpuAcc,CpuRef. Accepted options: [CpuAcc, CpuRef, GpuAcc]."
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +000039 " Defaults to CpuAcc,CpuRef"},
40 {PROFILING_ENABLED, "[OPTIONAL] Enabling this option will print important ML related milestones timing"
41 "information in micro-seconds. By default, this option is disabled."
42 "Accepted options are true/false."}
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010043};
44
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010045/*
46 * Reads the user supplied backend preference, splits it by comma, and returns an ordered vector
47 */
48std::vector<armnn::BackendId> GetPreferredBackendList(const std::string& preferredBackends)
49{
50 std::vector<armnn::BackendId> backends;
51 std::stringstream ss(preferredBackends);
52
53 while(ss.good())
54 {
55 std::string backend;
56 std::getline( ss, backend, ',' );
57 backends.emplace_back(backend);
58 }
59 return backends;
60}
61
62/*
63 * Assigns a color to each label in the label set
64 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010065std::vector<std::tuple<std::string, common::BBoxColor>> AssignColourToLabel(const std::string& pathToLabelFile)
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010066{
67 std::ifstream in(pathToLabelFile);
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010068 std::vector<std::tuple<std::string, common::BBoxColor>> labels;
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010069
70 std::string str;
71 std::default_random_engine generator;
72 std::uniform_int_distribution<int> distribution(0,255);
73
74 while (std::getline(in, str))
75 {
76 if(!str.empty())
77 {
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010078 common::BBoxColor c{
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010079 .colorCode = std::make_tuple(distribution(generator),
80 distribution(generator),
81 distribution(generator))
82 };
83 auto bboxInfo = std::make_tuple (str, c);
84
85 labels.emplace_back(bboxInfo);
86 }
87 }
88 return labels;
89}
90
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010091std::tuple<std::unique_ptr<common::IFrameReader<cv::Mat>>,
92 std::unique_ptr<common::IFrameOutput<cv::Mat>>>
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010093 GetFrameSourceAndSink(const std::map<std::string, std::string>& options) {
94
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010095 std::unique_ptr<common::IFrameReader<cv::Mat>> readerPtr;
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010096
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010097 std::unique_ptr<common::CvVideoFrameReader> reader = std::make_unique<common::CvVideoFrameReader>();
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010098 reader->Init(GetSpecifiedOption(options, VIDEO_FILE_PATH));
99
100 auto enc = reader->GetSourceEncodingInt();
101 auto fps = reader->GetSourceFps();
102 auto w = reader->GetSourceWidth();
103 auto h = reader->GetSourceHeight();
104 if (!reader->ConvertToRGB())
105 {
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100106 readerPtr = std::move(std::make_unique<common::CvVideoFrameReaderRgbWrapper>(std::move(reader)));
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100107 }
108 else
109 {
110 readerPtr = std::move(reader);
111 }
112
113 if(CheckOptionSpecified(options, OUTPUT_VIDEO_FILE_PATH))
114 {
115 std::string outputVideo = GetSpecifiedOption(options, OUTPUT_VIDEO_FILE_PATH);
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100116 auto writer = std::make_unique<common::CvVideoFileWriter>();
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100117 writer->Init(outputVideo, enc, fps, w, h);
118
119 return std::make_tuple<>(std::move(readerPtr), std::move(writer));
120 }
121 else
122 {
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100123 auto writer = std::make_unique<common::CvWindowOutput>();
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100124 writer->Init("Processed Video");
125 return std::make_tuple<>(std::move(readerPtr), std::move(writer));
126 }
127}
128
129int main(int argc, char *argv[])
130{
131 std::map<std::string, std::string> options;
132
133 int result = ParseOptions(options, CMD_OPTIONS, argv, argc);
134 if (result != 0)
135 {
136 return result;
137 }
138
139 // Create the network options
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100140 common::PipelineOptions pipelineOptions;
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100141 pipelineOptions.m_ModelFilePath = GetSpecifiedOption(options, MODEL_FILE_PATH);
142 pipelineOptions.m_ModelName = GetSpecifiedOption(options, MODEL_NAME);
143
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +0000144 if (CheckOptionSpecified(options, PROFILING_ENABLED))
145 {
146 pipelineOptions.m_ProfilingEnabled = GetSpecifiedOption(options, PROFILING_ENABLED) == "true";
147 }
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100148 if(CheckOptionSpecified(options, PREFERRED_BACKENDS))
149 {
150 pipelineOptions.m_backends = GetPreferredBackendList((GetSpecifiedOption(options, PREFERRED_BACKENDS)));
151 }
152 else
153 {
154 pipelineOptions.m_backends = {"CpuAcc", "CpuRef"};
155 }
156
157 auto labels = AssignColourToLabel(GetSpecifiedOption(options, LABEL_PATH));
158
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +0000159 common::Profiling profiling(pipelineOptions.m_ProfilingEnabled);
160 profiling.ProfilingStart();
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100161 od::IPipelinePtr objectDetectionPipeline = od::CreatePipeline(pipelineOptions);
162
163 auto inputAndOutput = GetFrameSourceAndSink(options);
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100164 std::unique_ptr<common::IFrameReader<cv::Mat>> reader = std::move(std::get<0>(inputAndOutput));
165 std::unique_ptr<common::IFrameOutput<cv::Mat>> sink = std::move(std::get<1>(inputAndOutput));
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100166
167 if (!sink->IsReady())
168 {
169 std::cerr << "Failed to open video writer.";
170 return 1;
171 }
172
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100173 common::InferenceResults<float> results;
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100174
175 std::shared_ptr<cv::Mat> frame = reader->ReadFrame();
176
177 //pre-allocate frames
178 cv::Mat processed;
179
180 while(!reader->IsExhausted(frame))
181 {
182 objectDetectionPipeline->PreProcessing(*frame, processed);
183 objectDetectionPipeline->Inference(processed, results);
184 objectDetectionPipeline->PostProcessing(results,
185 [&frame, &labels](od::DetectedObjects detects) -> void {
186 AddInferenceOutputToFrame(detects, *frame, labels);
187 });
188
189 sink->WriteFrame(frame);
190 frame = reader->ReadFrame();
191 }
192 sink->Close();
Eanna O Cathain2f0ddb62022-03-03 15:58:10 +0000193 profiling.ProfilingStopAndPrintUs("Overall compute time");
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100194 return 0;
195}