-
Notifications
You must be signed in to change notification settings - Fork 95
/
opendr_utils.cpp
332 lines (298 loc) · 9.34 KB
/
opendr_utils.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// Copyright 2020-2024 OpenDR European Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opendr_utils.h"
#include "data.h"
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <document.h>
#include <stringbuffer.h>
#include <writer.h>
const char *jsonGetStringFromKey(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember(key))) {
return "";
}
const rapidjson::Value &value = doc[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return "";
}
if (!value[index].IsString()) {
return "";
}
return value[index].GetString();
}
if (!value.IsString()) {
return "";
}
return value.GetString();
}
float jsonGetFloatFromKey(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember(key))) {
return 0.0f;
}
const rapidjson::Value &value = doc[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return 0.0f;
}
if (!value[index].IsFloat()) {
return 0.0f;
}
return value[index].IsFloat();
}
if (!value.IsFloat()) {
return 0.0f;
}
return value.GetFloat();
}
int jsonGetBoolFromKey(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember(key))) {
return -1;
}
const rapidjson::Value &value = doc[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return -1;
}
if (!value[index].IsBool()) {
return -1;
}
return (value[index].GetBool() ? 0 : 1);
}
if (!value.IsBool()) {
return -1;
}
return (value.GetBool() ? 0 : 1);
}
const char *jsonGetStringFromKeyInInferenceParams(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember("inference_params"))) {
return "";
}
const rapidjson::Value &inferenceParams = doc["inference_params"];
if ((!inferenceParams.IsObject()) || (!inferenceParams.HasMember(key))) {
return "";
}
const rapidjson::Value &value = inferenceParams[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return "";
}
if (!value[index].IsString()) {
return "";
}
return value[index].GetString();
}
if (!value.IsString()) {
return "";
}
return value.GetString();
}
float jsonGetFloatFromKeyInInferenceParams(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember("inference_params"))) {
return 0.0f;
}
const rapidjson::Value &inferenceParams = doc["inference_params"];
if ((!inferenceParams.IsObject()) || (!inferenceParams.HasMember(key))) {
return 0.0f;
}
const rapidjson::Value &value = inferenceParams[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return 0.0f;
}
if (!value[index].IsFloat()) {
return 0.0f;
}
return value[index].GetFloat();
}
if (!value.IsFloat()) {
return 0.0f;
}
return value.GetFloat();
}
int jsonGetBoolFromKeyInInferenceParams(const char *json, const char *key, const int index) {
rapidjson::Document doc;
doc.Parse(json);
if ((!doc.IsObject()) || (!doc.HasMember("inference_params"))) {
return -1;
}
const rapidjson::Value &inferenceParams = doc["inference_params"];
if ((!inferenceParams.IsObject()) || (!inferenceParams.HasMember(key))) {
return -1;
}
const rapidjson::Value &value = inferenceParams[key];
if (value.IsArray()) {
if (value.Size() <= index) {
return -1;
}
if (!value[index].IsBool()) {
return -1;
}
return (value[index].GetBool() ? 0 : 1);
}
if (!value.IsBool()) {
return -1;
}
return (value.GetBool() ? 0 : 1);
}
void loadImage(const char *path, OpenDRImageT *image) {
cv::Mat opencvImage = cv::imread(path, cv::IMREAD_COLOR);
if (opencvImage.empty()) {
image->data = NULL;
} else {
image->data = new cv::Mat(opencvImage);
}
}
void freeImage(OpenDRImageT *image) {
if (image->data) {
cv::Mat *opencvImage = static_cast<cv::Mat *>(image->data);
delete opencvImage;
}
}
void initDetectionsVector(OpenDRDetectionVectorTargetT *vector) {
vector->startingPointer = NULL;
std::vector<OpenDRDetectionTarget> detections;
OpenDRDetectionTargetT detection;
detection.name = -1;
detection.left = 0.0;
detection.top = 0.0;
detection.width = 0.0;
detection.height = 0.0;
detection.score = 0.0;
detections.push_back(detection);
loadDetectionsVector(vector, detections.data(), static_cast<int>(detections.size()));
}
void loadDetectionsVector(OpenDRDetectionVectorTargetT *vector, OpenDRDetectionTargetT *detectionPtr, int vectorSize) {
freeDetectionsVector(vector);
vector->size = vectorSize;
int sizeOfOutput = (vectorSize) * sizeof(OpenDRDetectionTargetT);
vector->startingPointer = static_cast<OpenDRDetectionTargetT *>(malloc(sizeOfOutput));
std::memcpy(vector->startingPointer, detectionPtr, sizeOfOutput);
}
void freeDetectionsVector(OpenDRDetectionVectorTargetT *vector) {
if (vector->startingPointer != NULL) {
free(vector->startingPointer);
vector->startingPointer = NULL;
}
}
void initTensor(OpenDRTensorT *tensor) {
tensor->batchSize = 0;
tensor->frames = 0;
tensor->channels = 0;
tensor->width = 0;
tensor->height = 0;
tensor->data = NULL;
}
void loadTensor(OpenDRTensorT *tensor, void *tensorData, int batchSize, int frames, int channels, int width, int height) {
freeTensor(tensor);
tensor->batchSize = batchSize;
tensor->frames = frames;
tensor->channels = channels;
tensor->width = width;
tensor->height = height;
int sizeOfData = (batchSize * frames * channels * width * height) * sizeof(float);
tensor->data = static_cast<float *>(malloc(sizeOfData));
std::memcpy(tensor->data, tensorData, sizeOfData);
}
void freeTensor(OpenDRTensorT *tensor) {
if (tensor->data != NULL) {
free(tensor->data);
tensor->data = NULL;
}
}
void initTensorVector(OpenDRTensorVectorT *vector) {
vector->nTensors = 0;
vector->batchSizes = NULL;
vector->frames = NULL;
vector->channels = NULL;
vector->widths = NULL;
vector->heights = NULL;
vector->datas = NULL;
}
void loadTensorVector(OpenDRTensorVectorT *vector, OpenDRTensorT *tensorPtr, int nTensors) {
freeTensorVector(vector);
vector->nTensors = nTensors;
int sizeOfDataShape = nTensors * sizeof(int);
/* initialize arrays to hold size values for each tensor */
vector->batchSizes = static_cast<int *>(malloc(sizeOfDataShape));
vector->frames = static_cast<int *>(malloc(sizeOfDataShape));
vector->channels = static_cast<int *>(malloc(sizeOfDataShape));
vector->widths = static_cast<int *>(malloc(sizeOfDataShape));
vector->heights = static_cast<int *>(malloc(sizeOfDataShape));
/* initialize array to hold data values for all tensors */
vector->datas = static_cast<float **>(malloc(nTensors * sizeof(float *)));
/* copy size values */
for (int i = 0; i < nTensors; i++) {
(vector->batchSizes)[i] = tensorPtr[i].batchSize;
(vector->frames)[i] = tensorPtr[i].frames;
(vector->channels)[i] = tensorPtr[i].channels;
(vector->widths)[i] = tensorPtr[i].width;
(vector->heights)[i] = tensorPtr[i].height;
/* copy data values by,
* initialize a data pointer into a tensor,
* copy the values,
* set tensor data pointer to watch the memory pointer*/
int sizeOfData = ((tensorPtr[i].batchSize) * (tensorPtr[i].frames) * (tensorPtr[i].channels) * (tensorPtr[i].width) *
(tensorPtr[i].height) * sizeof(float));
float *memoryOfDataTensor = static_cast<float *>(malloc(sizeOfData));
std::memcpy(memoryOfDataTensor, tensorPtr[i].data, sizeOfData);
(vector->datas)[i] = memoryOfDataTensor;
}
}
void freeTensorVector(OpenDRTensorVectorT *vector) {
// free vector pointers
if (vector->batchSizes != NULL) {
free(vector->batchSizes);
vector->batchSizes = NULL;
}
if (vector->frames != NULL) {
free(vector->frames);
vector->frames = NULL;
}
if (vector->channels != NULL) {
free(vector->channels);
vector->channels = NULL;
}
if (vector->widths != NULL) {
free(vector->widths);
vector->widths = NULL;
}
if (vector->heights != NULL) {
free(vector->heights);
vector->heights = NULL;
}
// free tensors data and vector memory
if (vector->datas != NULL) {
free(vector->datas);
vector->datas = NULL;
}
// reset tensor vector values
vector->nTensors = 0;
}
void iterTensorVector(OpenDRTensorT *tensor, OpenDRTensorVectorT *vector, int index) {
loadTensor(tensor, static_cast<void *>((vector->datas)[index]), (vector->batchSizes)[index], (vector->frames)[index],
(vector->channels)[index], (vector->widths)[index], (vector->heights)[index]);
}