-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathObjectDetector.cpp
142 lines (119 loc) · 4.67 KB
/
ObjectDetector.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "ObjectDetector.h"
#include <opencv2/imgproc.hpp>
using namespace cv;
ObjectDetector::ObjectDetector(const char* tfliteModelPath, bool quantized, bool useXnn) {
m_modelQuantized = quantized;
initDetectionModel(tfliteModelPath, useXnn);
}
ObjectDetector::~ObjectDetector() {
if (m_model != nullptr)
TfLiteModelDelete(m_model);
}
void ObjectDetector::initDetectionModel(const char* tfliteModelPath, bool useXnn) {
m_model = TfLiteModelCreateFromFile(tfliteModelPath);
if (m_model == nullptr) {
printf("Failed to load model");
return;
}
// Build the interpreter
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsSetNumThreads(options, 1);
if (useXnn) {
TfLiteXNNPackDelegateOptions xnnOpts = TfLiteXNNPackDelegateOptionsDefault();
m_xnnpack_delegate = TfLiteXNNPackDelegateCreate(&xnnOpts);
TfLiteInterpreterOptionsAddDelegate(options, m_xnnpack_delegate);
}
// Create the interpreter.
m_interpreter = TfLiteInterpreterCreate(m_model, options);
if (m_interpreter == nullptr) {
printf("Failed to create interpreter");
return;
}
// Allocate tensor buffers.
if (TfLiteInterpreterAllocateTensors(m_interpreter) != kTfLiteOk) {
printf("Failed to allocate tensors!");
return;
}
// Find input tensors.
if (TfLiteInterpreterGetInputTensorCount(m_interpreter) != 1) {
printf("Detection model graph needs to have 1 and only 1 input!");
return;
}
m_input_tensor = TfLiteInterpreterGetInputTensor(m_interpreter, 0);
if (m_modelQuantized && m_input_tensor->type != kTfLiteUInt8) {
printf("Detection model input should be kTfLiteUInt8!");
return;
}
if (!m_modelQuantized && m_input_tensor->type != kTfLiteFloat32) {
printf("Detection model input should be kTfLiteFloat32!");
return;
}
if (m_input_tensor->dims->data[0] != 1 ||
m_input_tensor->dims->data[1] != DETECTION_MODEL_SIZE ||
m_input_tensor->dims->data[2] != DETECTION_MODEL_SIZE ||
m_input_tensor->dims->data[3] != DETECTION_MODEL_CNLS) {
printf("Detection model must have input dims of 1x%ix%ix%i", DETECTION_MODEL_SIZE,
DETECTION_MODEL_SIZE, DETECTION_MODEL_CNLS);
return;
}
// Find output tensors.
if (TfLiteInterpreterGetOutputTensorCount(m_interpreter) != 4) {
printf("Detection model graph needs to have 4 and only 4 outputs!");
return;
}
m_output_locations = TfLiteInterpreterGetOutputTensor(m_interpreter, 0);
m_output_classes = TfLiteInterpreterGetOutputTensor(m_interpreter, 1);
m_output_scores = TfLiteInterpreterGetOutputTensor(m_interpreter, 2);
m_num_detections = TfLiteInterpreterGetOutputTensor(m_interpreter, 3);
}
DetectResult* ObjectDetector::detect(Mat src) {
DetectResult* res = new DetectResult[DETECT_NUM];
if (m_model == nullptr) {
return res;
}
Mat image;
resize(src, image, Size(DETECTION_MODEL_SIZE, DETECTION_MODEL_SIZE), 0, 0, INTER_AREA);
int cnls = image.type();
if (cnls == CV_8UC1) {
cvtColor(image, image, COLOR_GRAY2RGB);
}
else if (cnls == CV_8UC3) {
cvtColor(image, image, COLOR_BGR2RGB);
}
else if (cnls == CV_8UC4) {
cvtColor(image, image, COLOR_BGRA2RGB);
}
if (m_modelQuantized) {
// Copy image into input tensor
uchar* dst = m_input_tensor->data.uint8;
memcpy(dst, image.data,
sizeof(uchar) * DETECTION_MODEL_SIZE * DETECTION_MODEL_SIZE * DETECTION_MODEL_CNLS);
}
else {
// Normalize the image based on std and mean (p' = (p-mean)/std)
Mat fimage;
image.convertTo(fimage, CV_32FC3, 1 / IMAGE_STD, -IMAGE_MEAN / IMAGE_STD);
// Copy image into input tensor
float* dst = m_input_tensor->data.f;
memcpy(dst, fimage.data,
sizeof(float) * DETECTION_MODEL_SIZE * DETECTION_MODEL_SIZE * DETECTION_MODEL_CNLS);
}
if (TfLiteInterpreterInvoke(m_interpreter) != kTfLiteOk) {
printf("Error invoking detection model");
return res;
}
const float* detection_locations = m_output_locations->data.f;
const float* detection_classes = m_output_classes->data.f;
const float* detection_scores = m_output_scores->data.f;
const int num_detections = (int)* m_num_detections->data.f;
for (int i = 0; i < num_detections && i < DETECT_NUM; ++i) {
res[i].score = detection_scores[i];
res[i].label = (int)detection_classes[i];
// Get the bbox, make sure its not out of the image bounds, and scale up to src image size
res[i].ymin = std::fmax(0.0f, detection_locations[4 * i] * src.rows);
res[i].xmin = std::fmax(0.0f, detection_locations[4 * i + 1] * src.cols);
res[i].ymax = std::fmin(float(src.rows - 1), detection_locations[4 * i + 2] * src.rows);
res[i].xmax = std::fmin(float(src.cols - 1), detection_locations[4 * i + 3] * src.cols);
}
return res;
}