-
Notifications
You must be signed in to change notification settings - Fork 44
/
hailo_common.hpp
307 lines (269 loc) · 10.7 KB
/
hailo_common.hpp
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
/**
* Copyright (c) 2021-2022 Hailo Technologies Ltd. All rights reserved.
* Distributed under the LGPL license (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt)
**/
/* __BEGIN_DECLS should be used at the beginning of your declarations,
so that C++ compilers don't mangle their names. Use __END_DECLS at
the end of C declarations. */
#pragma once
#include "hailo_objects.hpp"
// #include <stdlib.h>
// #include <string>
// #include <cstring>
#undef __BEGIN_DECLS
#undef __END_DECLS
#ifdef __cplusplus
#define __BEGIN_DECLS \
extern "C" \
{
#define __END_DECLS }
#else
#define __BEGIN_DECLS /* empty */
#define __END_DECLS /* empty */
#endif
namespace hailo_common
{
inline void add_object(HailoROIPtr roi, HailoObjectPtr obj)
{
roi->add_object(obj);
}
inline void add_objects(HailoROIPtr roi, std::vector<HailoObjectPtr> objects)
{
for (HailoObjectPtr obj : objects)
{
add_object(roi, obj);
}
}
inline void add_classification(HailoROIPtr roi, std::string type, std::string label, float confidence, int class_id = NULL_CLASS_ID)
{
add_object(roi,
std::make_shared<HailoClassification>(type, class_id, label, confidence));
}
inline HailoDetectionPtr add_detection(HailoROIPtr roi, HailoBBox bbox, std::string label, float confidence, int class_id = NULL_CLASS_ID)
{
HailoDetectionPtr detection = std::make_shared<HailoDetection>(bbox, class_id, label, confidence);
detection->set_scaling_bbox(roi->get_bbox());
add_object(roi, detection);
return detection;
}
inline void add_detections(HailoROIPtr roi, std::vector<HailoDetection> detections)
{
for (auto det : detections)
{
add_object(roi, std::make_shared<HailoDetection>(det));
}
}
inline void add_detection_pointers(HailoROIPtr roi, std::vector<HailoDetectionPtr> detections)
{
for (auto det : detections)
{
det->set_scaling_bbox(roi->get_bbox());
roi->add_object(det);
}
}
inline void remove_objects(HailoROIPtr roi, std::vector<HailoObjectPtr> objects)
{
for (HailoObjectPtr obj : objects)
{
roi->remove_object(obj);
}
}
inline void remove_detections(HailoROIPtr roi, std::vector<HailoDetectionPtr> objects)
{
for (HailoObjectPtr obj : objects)
{
roi->remove_object(obj);
}
}
inline bool has_classifications(HailoROIPtr roi, std::string classification_type)
{
for (auto obj : roi->get_objects_typed(HAILO_CLASSIFICATION))
{
HailoClassificationPtr classification = std::dynamic_pointer_cast<HailoClassification>(obj);
if (classification_type.compare(classification->get_classification_type()) == 0)
{
return true;
}
}
return false;
}
inline void remove_classifications(HailoROIPtr roi, std::string classification_type)
{
std::vector<HailoObjectPtr> classifications;
for (auto obj : roi->get_objects_typed(HAILO_CLASSIFICATION))
{
HailoClassificationPtr classification = std::dynamic_pointer_cast<HailoClassification>(obj);
if (classification_type.compare(classification->get_classification_type()) == 0)
{
classifications.push_back(classification);
}
}
remove_objects(roi, classifications);
}
inline std::vector<HailoDetectionPtr> get_hailo_detections(HailoROIPtr roi)
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_DETECTION);
std::vector<HailoDetectionPtr> detections;
for (auto obj : objects)
{
detections.emplace_back(std::dynamic_pointer_cast<HailoDetection>(obj));
}
return detections;
}
inline std::vector<HailoTileROIPtr> get_hailo_tiles(HailoROIPtr roi)
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_TILE);
std::vector<HailoTileROIPtr> tiles;
for (auto obj : objects)
{
tiles.emplace_back(std::dynamic_pointer_cast<HailoTileROI>(obj));
}
return tiles;
}
inline std::vector<HailoClassificationPtr> get_hailo_classifications(HailoROIPtr roi, std::string classification_type = "")
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_CLASSIFICATION);
std::vector<HailoClassificationPtr> classifications;
for (auto obj : objects)
{
HailoClassificationPtr classification = std::dynamic_pointer_cast<HailoClassification>(obj);
if (classification_type.empty() || classification_type.compare(classification->get_classification_type()) == 0)
{
classifications.emplace_back(std::dynamic_pointer_cast<HailoClassification>(obj));
}
}
return classifications;
}
inline std::vector<HailoUniqueIDPtr> get_hailo_unique_id(HailoROIPtr roi)
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_UNIQUE_ID);
std::vector<HailoUniqueIDPtr> unique_ids;
for (auto obj : objects)
{
unique_ids.emplace_back(std::dynamic_pointer_cast<HailoUniqueID>(obj));
}
return unique_ids;
}
inline std::vector<HailoUniqueIDPtr> get_hailo_unique_id_by_mode(HailoROIPtr roi, hailo_unique_id_mode_t mode)
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_UNIQUE_ID);
std::vector<HailoUniqueIDPtr> unique_ids;
for (auto obj : objects)
{
HailoUniqueIDPtr unique_id = std::dynamic_pointer_cast<HailoUniqueID>(obj);
if(unique_id->get_mode() == mode)
{
unique_ids.emplace_back(unique_id);
}
}
return unique_ids;
}
inline std::vector<HailoUniqueIDPtr> get_hailo_track_id(HailoROIPtr roi)
{
return get_hailo_unique_id_by_mode(roi, TRACKING_ID);
}
inline std::vector<HailoUniqueIDPtr> get_hailo_global_id(HailoROIPtr roi)
{
return get_hailo_unique_id_by_mode(roi, GLOBAL_ID);
}
inline std::vector<HailoLandmarksPtr> get_hailo_landmarks(HailoROIPtr roi)
{
std::vector<HailoObjectPtr> objects = roi->get_objects_typed(HAILO_LANDMARKS);
std::vector<HailoLandmarksPtr> landmarks;
for (auto obj : objects)
{
landmarks.emplace_back(std::dynamic_pointer_cast<HailoLandmarks>(obj));
}
return landmarks;
}
inline std::vector<HailoROIPtr> get_hailo_roi_instances(HailoROIPtr roi)
{
std::vector<HailoROIPtr> hailo_rois;
for (auto obj : roi->get_objects())
{
HailoROIPtr hailo_roi = std::dynamic_pointer_cast<HailoROI>(obj);
if (hailo_roi)
hailo_rois.emplace_back(hailo_roi);
}
return hailo_rois;
}
/**
* Flatten HailoBBox with parent HailoBBox.
* re scales each bbox values (x,y,width,height min/max values) to match the parent scale.
*
* @param[in] bbox HailoBBox, target bbox to flatten.
* @param[in] parent_bbox HailoBBox, parent bbox - to scale to.
* @return void.
*/
inline HailoBBox create_flattened_bbox(const HailoBBox &bbox, const HailoBBox &parent_bbox)
{
float xmin = parent_bbox.xmin() + bbox.xmin() * parent_bbox.width();
float ymin = parent_bbox.ymin() + bbox.ymin() * parent_bbox.height();
float width = bbox.width() * parent_bbox.width();
float height = bbox.height() * parent_bbox.height();
return HailoBBox(xmin, ymin, width, height);
}
/**
* Flatten HailoROI subobjects and move them under parent HailoROI.
* re scales each suboject bbox values to match the parent and move the under the ownership of the parent.
*
* @param[in] roi HailoROIPtr, target roi to flatten.
* @param[in] parent_roi HailoROIPtr, parent roi - will take ownership of the subobjects of the target.
* @param[in] filter_type hailo_object_t, specific subobject type to flatten.
* @return void.
*/
inline void flatten_hailo_roi(HailoROIPtr roi, HailoROIPtr parent_roi, hailo_object_t filter_type)
{
std::vector<HailoObjectPtr> objects = roi->get_objects();
for (uint index = 0; index < objects.size(); index++)
{
if (objects[index]->get_type() == filter_type)
{
HailoROIPtr sub_obj_roi = std::dynamic_pointer_cast<HailoROI>(objects[index]);
sub_obj_roi->set_bbox(std::move(create_flattened_bbox(sub_obj_roi->get_bbox(), roi->get_bbox())));
parent_roi->add_object(sub_obj_roi);
roi->remove_object(index);
objects.erase(objects.begin() + index);
index--;
}
}
}
/**
* Fixate Point(x,y) to the values of a given HailoBBox.
* @param[in] hailo_roi HailoROIPtr, target roi.
* @param[in] point HailoPoint, target point object.
* @param[in] fixate_bbox HailoBBox, target bbox - to fixate the point to.
* @return New HailoPoint.
*/
inline HailoPoint fixate_point_with_bbox(HailoROIPtr hailo_roi, HailoPoint &point, const HailoBBox &fixate_bbox)
{
HailoBBox current_bbox = hailo_roi->get_bbox();
float xmin = current_bbox.xmin() + point.x() * current_bbox.width();
float ymin = current_bbox.ymin() + point.y() * current_bbox.height();
return HailoPoint((xmin-fixate_bbox.xmin())/fixate_bbox.width(), (ymin-fixate_bbox.ymin())/fixate_bbox.height());
}
/**
* Fixate HailoROI landmarks subobject to the values of a given HailoBBox.
* re place every landmark point to match new bbox, by changing the x and y values.
*
* @param[in] hailo_roi HailoROIPtr, target roi.
* @param[in] fixate_bbox HailoBBox, target bbox - to fixate the landmarks to.
* @return void.
*/
inline void fixate_landmarks_with_bbox(HailoROIPtr hailo_roi, HailoBBox fixate_bbox)
{
std::vector<HailoLandmarksPtr> landmarks_ptrs = get_hailo_landmarks(hailo_roi);
if (landmarks_ptrs.size() > 0)
{
HailoLandmarksPtr landmarks_obj = std::dynamic_pointer_cast<HailoLandmarks>(landmarks_ptrs[0]);
std::vector<HailoPoint> decoded_points = landmarks_obj->get_points();
std::vector<HailoPoint> new_points;
new_points.reserve(decoded_points.size());
for (size_t i = 0; i < decoded_points.size(); i++)
{
new_points.emplace_back(fixate_point_with_bbox(hailo_roi, decoded_points[i], fixate_bbox));
}
landmarks_obj->set_points(new_points);
}
}
}