-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathObjectDetectionApp.hpp
More file actions
109 lines (93 loc) · 3.32 KB
/
ObjectDetectionApp.hpp
File metadata and controls
109 lines (93 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ---------------------------------------------------------------------
// Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------------
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <optional>
#include <onnxruntime_cxx_api.h>
namespace App
{
enum Precision
{
Fp32 = 0,
Fp16 = 1
};
enum BackendOption
{
Cpu = 0,
Npu = 2,
};
class ObjectDetectionApp
{
private:
// model io pointing to loaded data
std::vector<Ort::Value> m_inputs;
std::vector<Ort::Value> m_outputs;
std::vector<const char*> m_input_names;
// Keep io names alive returned by session
std::vector<Ort::AllocatedStringPtr> m_io_data_ptr;
// model metadata
uint32_t m_model_input_ht, m_model_input_wt;
std::string m_model_path;
Ort::Env m_env;
std::unique_ptr<Ort::Session> m_session;
Ort::AllocatorWithDefaultOptions m_allocator;
void ClearInputsAndOutputs();
public:
ObjectDetectionApp(std::string model_path, uint32_t model_input_ht, uint32_t model_input_wt);
ObjectDetectionApp() = delete;
ObjectDetectionApp(const ObjectDetectionApp&) = delete;
ObjectDetectionApp(ObjectDetectionApp&&) = delete;
ObjectDetectionApp& operator=(const ObjectDetectionApp&) = delete;
ObjectDetectionApp& operator=(ObjectDetectionApp&&) = delete;
/**
* PrepareModelForInference: Prepares model for inference with ORT
* - initializes ORT Session
* - sets backend, precision and qnn_options for execution provider
*
* @param backend: backend for model execution.
* @param precision: preferred precision for model execution.
* @param qnn_options: QNN Execution provider options.
* refer to
* https://onnxruntime.ai/docs/execution-providers/QNN-ExecutionProvider.html#configuration-options
* for all supported options.
*
*/
void PrepareModelForInference(const App::BackendOption backend,
const App::Precision precision,
std::unordered_map<std::string, std::string> qnn_options);
/**
* LoadInputs: Load and prepare local image for model inference
* - Loads image with opencv
* - Converts image to Ort::OrtTensor to use during model inference
*
* @param image_path: local path to image to load.
*
*/
void LoadInputs(const std::string& image_path);
/**
* RunInference: Execute prepared model
* - Runs inference and cache output
*
* @throws if model is not prepared with PrepareModelForInference before
* @throws if inputs are not loaded with LoadInputs before
*
*/
void RunInference();
/**
* ProcessOutput: Process cached model output to show results
*
* @param input_image_path input image to add bounding boxes and labels.
* @param display_output_image if true, shows output image in window using
* opencv.
* @param output_image_path if provided, serializes output image locally.
*
*/
void ProcessOutput(const std::string& input_image_path,
std::optional<std::string> output_image_path = std::nullopt,
bool display_output_image = true);
};
} // namespace App