forked from hailo-ai/tappas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhailo_objects.hpp
1165 lines (1063 loc) · 35.4 KB
/
hailo_objects.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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)
**/
/**
* @file hailo_objects.hpp
* @authors Hailo
**/
#pragma once
#include "hailo_tensors.hpp"
#include <map>
#include <algorithm>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include <mutex>
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define CLIP(x) (CLAMP(x, 0, 255))
#define NULL_CLASS_ID (-1)
typedef enum
{
HAILO_ROI,
HAILO_CLASSIFICATION,
HAILO_DETECTION,
HAILO_LANDMARKS,
HAILO_TILE,
HAILO_UNIQUE_ID,
HAILO_MATRIX,
HAILO_DEPTH_MASK,
HAILO_CLASS_MASK,
HAILO_CONF_CLASS_MASK,
HAILO_USER_META
} hailo_object_t;
static std::map<std::string, hailo_object_t> hailo_object_map = {
{"hailo_roi", HAILO_ROI},
{"hailo_classification", HAILO_CLASSIFICATION},
{"hailo_detection", HAILO_DETECTION},
{"hailo_landmarks", HAILO_LANDMARKS},
{"hailo_tile", HAILO_TILE},
{"hailo_unique_id", HAILO_UNIQUE_ID},
{"hailo_matrix", HAILO_MATRIX},
{"hailo_depth_mask", HAILO_DEPTH_MASK},
{"hailo_class_mask", HAILO_CLASS_MASK},
{"hailo_conf_class_mask", HAILO_CONF_CLASS_MASK},
{"hailo_user_meta", HAILO_USER_META},
};
inline hailo_object_t hailo_object_type_from_string(const std::string &lower_case_str)
{
if (hailo_object_map.find(lower_case_str) != hailo_object_map.end())
{
return hailo_object_map[lower_case_str];
}
else
{
throw std::invalid_argument("Invalid hailo object type: " + lower_case_str + ".");
}
}
inline std::string hailo_object_type_to_string(const hailo_object_t obj)
{
for (auto it = hailo_object_map.begin(); it != hailo_object_map.end(); ++it)
{
if (it->second == obj)
{
return it->first;
}
}
throw std::invalid_argument("Invalid hailo object type.");
}
typedef enum
{
SINGLE_SCALE,
MULTI_SCALE,
} hailo_tiling_mode_t;
typedef enum
{
TRACKING_ID,
GLOBAL_ID,
} hailo_unique_id_mode_t;
static float assure_normal(float num)
{
if ((num > 1.0f) || (num < 0.0))
{
throw std::invalid_argument("Number should be between 0.0 to 1.0.");
}
return num;
}
/**
* @brief HailoPoint - Represents a detected point (Landmarks)
*
*/
struct HailoPoint
{
protected:
const float m_x;
const float m_y;
const float m_confidence;
public:
/**
* @brief Construct a new Hailo Point object
*
* @param x normalized x position (float)
* @param y normalized y position (float)
* @param confidence The confidence in the point's accuracy, float between 0.0 to 1.0 - default is 1.0.
*/
HailoPoint(float x, float y, float confidence = 1.0f) : m_x(x), m_y(y), m_confidence(assure_normal(confidence)){};
const float x() const { return m_x; }
const float y() const { return m_y; }
const float confidence() const { return m_confidence; }
};
/**
* @brief HailoBBox - Represents a bounding box.
* Takes 4 float arguments representing the bounding box (normalized).
* The first 2 arguments are the x and y of the minimum point (Top Left corner).
* The other 2 arguments are the width and height of the box respectivly.
* All arguments are normalized to the picture they relate to.
*/
struct HailoBBox
{
protected:
float m_xmin;
float m_ymin;
float m_width;
float m_height;
public:
/**
* @brief Construct a new Hailo BBox object
*
* @param xmin normalized xmin position
* @param ymin normalized ymin position
* @param width normalized width of bounding box
* @param height normalized height of bounding box
*/
HailoBBox(float xmin, float ymin, float width, float height) : m_xmin(xmin), m_ymin(ymin), m_width(width), m_height(height){};
const float xmin() const { return m_xmin; }
const float ymin() const { return m_ymin; }
const float width() const { return m_width; }
const float height() const { return m_height; }
const float xmax() const { return m_xmin + m_width; }
const float ymax() const { return m_ymin + m_height; }
};
/**
* @brief Represents an object that is a usable output after postprocessing.
* An abstract class for all objects to inherit from.
*/
class HailoObject
{
protected:
std::shared_ptr<std::mutex> mutex;
public:
// Constructor
HailoObject()
{
mutex = std::make_shared<std::mutex>();
};
// Destructor
virtual ~HailoObject() = default;
HailoObject &operator=(const HailoObject &other) = default;
HailoObject &operator=(HailoObject &&other) noexcept = default;
HailoObject(HailoObject &&other) noexcept = default;
HailoObject(const HailoObject &other) = default;
/**
* @brief Get the type object
*
* @return hailo_object_t - The type of the object.
*/
virtual hailo_object_t get_type() = 0;
};
using HailoObjectPtr = std::shared_ptr<HailoObject>;
/**
* @brief Represents a HailoObject that can hold other objects.
* for example a face detection can hold landmarks or age classification, gender classification etc...
*/
class HailoMainObject : public HailoObject, public std::enable_shared_from_this<HailoMainObject>
{
protected:
std::vector<HailoObjectPtr> m_sub_objects;
std::map<std::string, HailoTensorPtr> m_tensors;
public:
HailoMainObject()
{
mutex = std::make_shared<std::mutex>();
};
virtual ~HailoMainObject() = default;
HailoMainObject(HailoMainObject &&other) noexcept : HailoObject(other), m_sub_objects(std::move(other.m_sub_objects)){};
HailoMainObject(const HailoMainObject &other) : HailoObject(other), m_sub_objects(other.m_sub_objects){};
HailoMainObject &operator=(const HailoMainObject &other) = default;
HailoMainObject &operator=(HailoMainObject &&other) noexcept = default;
/**
* @brief Add an object to the main object.
*
* @param obj Object to add.
*/
void add_object(HailoObjectPtr obj)
{
std::lock_guard<std::mutex> lock(*mutex);
m_sub_objects.emplace_back(obj);
};
/**
* @brief Add a tensor to the main object.
*
* @param tensor Tensor to add.
*/
void add_tensor(HailoTensorPtr tensor)
{
std::lock_guard<std::mutex> lock(*mutex);
m_tensors.emplace(tensor->name(), tensor);
};
/**
* @brief Remove a HailoObject from the MainObject
*
* @param obj - HailoObjectPtr
* The object to remove
*/
void remove_object(HailoObjectPtr obj)
{
std::lock_guard<std::mutex> lock(*mutex);
m_sub_objects.erase(std::remove(m_sub_objects.begin(), m_sub_objects.end(), obj), m_sub_objects.end());
};
/**
* @brief Remove a HailoObject from the MainObject
*
* @param index - uint
* The index of the object to remove
*/
void remove_object(uint index)
{
std::lock_guard<std::mutex> lock(*mutex);
m_sub_objects.erase(m_sub_objects.begin() + index);
};
/**
* @brief Get a tensor from this main object.
*
* @param name Tensor's name to get,
* @return HailoTensorPtr - A tensor.
*/
HailoTensorPtr get_tensor(std::string name)
{
std::lock_guard<std::mutex> lock(*mutex);
auto itr = m_tensors.find(name);
if (itr == m_tensors.end())
{
throw std::invalid_argument("No tensor with name " + name);
}
return itr->second;
};
/**
* @brief Checks whether there are tensors attached to this main object
*
* @return true when there are tensors in this main object.
* @return false when there are no tensors in this main object.
*/
bool has_tensors()
{
return !m_tensors.empty();
};
/**
* @brief Get a vector of the tensors attached to this main object.
*
* @return std::vector<HailoTensorPtr>
*/
std::vector<HailoTensorPtr> get_tensors()
{
std::lock_guard<std::mutex> lock(*mutex);
std::vector<HailoTensorPtr> _tensors;
_tensors.reserve(m_tensors.size());
for (auto &tensor_pair : m_tensors)
{
_tensors.emplace_back(tensor_pair.second);
}
return _tensors;
};
std::map<std::string, HailoTensorPtr> get_tensors_by_name()
{
std::map<std::string, HailoTensorPtr> tensors_by_name;
auto tensors = get_tensors();
for (auto tensor : tensors)
{
tensors_by_name[tensor->name()] = tensor;
}
return tensors_by_name;
}
/**
* @brief Clear all tensors attached to this main object.
*
*/
void clear_tensors()
{
std::lock_guard<std::mutex> lock(*mutex);
m_tensors.clear();
}
/**
* @brief Get the objects attached to this main object
*
* @return std::vector<HailoObjectPtr>
*/
std::vector<HailoObjectPtr> get_objects()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_sub_objects;
}
/**
* @brief Get the objects of a given type, attached to this main object.
*
* @param type The type of object to get.
* @return std::vector<HailoObjectPtr>
*/
std::vector<HailoObjectPtr> get_objects_typed(hailo_object_t type)
{
std::lock_guard<std::mutex> lock(*mutex);
std::vector<HailoObjectPtr> filtered_subobjects;
for (auto &obj : m_sub_objects)
{
if (obj->get_type() == type)
{
filtered_subobjects.emplace_back(obj);
}
}
return filtered_subobjects;
}
/**
* @brief Removes all the objects of a given type, attached to this main object.
*
* @param type The type of object to get.
*/
void remove_objects_typed(hailo_object_t type)
{
for (auto obj : this->get_objects_typed(type))
{
this->remove_object(obj);
}
}
};
using HailoMainObjectPtr = std::shared_ptr<HailoMainObject>;
/**
* @brief Represents an ROI (Region Of Interest).
* Part of an image, can hold other objects. Mostly inherited by other objects but isn't abstract.
* Can represents the whole image by giving the right HailoBBox.
*/
class HailoROI : public HailoMainObject
{
protected:
HailoBBox m_bbox; // A bounding box - the normalized position of this region of interest.
HailoBBox m_scaling_bbox; // A bounding box to scale by - x offset, y offset, width factor, height factor
std::string m_stream_id; // A string representing the stream ID that is related to this ROI.
public:
HailoROI(HailoBBox bbox, std::string stream_id = "") : m_bbox(bbox), m_scaling_bbox(HailoBBox(0.0, 0.0, 1.0, 1.0)), m_stream_id(stream_id){};
virtual ~HailoROI() = default;
HailoROI(HailoROI &&other) noexcept : HailoMainObject(other), m_bbox(std::move(other.m_bbox)), m_scaling_bbox(std::move(other.m_scaling_bbox)), m_stream_id(std::move(other.m_stream_id)){};
HailoROI(const HailoROI &other) : HailoMainObject(other), m_bbox(other.m_bbox), m_scaling_bbox(std::move(other.m_scaling_bbox)), m_stream_id(std::move(other.m_stream_id)){};
HailoROI &operator=(const HailoROI &other) = default;
HailoROI &operator=(HailoROI &&other) noexcept = default;
std::shared_ptr<HailoROI> shared_from_this()
{
return std::dynamic_pointer_cast<HailoROI>(HailoMainObject::shared_from_this());
}
virtual hailo_object_t get_type()
{
return HAILO_ROI;
}
/**
* @brief Add an object to the main object.
*
* @param obj Object to add.
*/
void add_object(HailoObjectPtr obj)
{
std::shared_ptr<HailoROI> possible_roi = std::dynamic_pointer_cast<HailoROI>(obj);
if (nullptr != possible_roi)
{
possible_roi->set_scaling_bbox(this->get_bbox());
possible_roi->set_stream_id(this->get_stream_id());
}
HailoMainObject::add_object(obj);
};
/**
* @brief Add an object to the main object.
* Ignore possible scaling of rois
*
* @param obj Object to add.
*/
void add_unscaled_object(HailoObjectPtr obj)
{
HailoMainObject::add_object(obj);
};
/**
* @brief Get the bbox of this ROI
*
* @return HailoBBox&
*/
HailoBBox &get_bbox()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_bbox;
}
/**
* @brief Set the bbox of this ROI
*
* @param bbox
*/
void set_bbox(HailoBBox bbox)
{
std::lock_guard<std::mutex> lock(*mutex);
m_bbox = std::move(bbox);
}
/**
* @brief Get the scaling bbox of this ROI
*
* @return HailoBBox&
*/
HailoBBox &get_scaling_bbox()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_scaling_bbox;
}
/**
* @brief Set the scaling bbox of this ROI
*
* @param bbox
*/
void set_scaling_bbox(HailoBBox bbox)
{
std::lock_guard<std::mutex> lock(*mutex);
float new_xmin = (m_scaling_bbox.xmin() * bbox.width()) + bbox.xmin();
float new_ymin = (m_scaling_bbox.ymin() * bbox.height()) + bbox.ymin();
float new_width = m_scaling_bbox.width() * bbox.width();
float new_height = m_scaling_bbox.height() * bbox.height();
HailoBBox new_scale = HailoBBox(new_xmin, new_ymin, new_width, new_height);
m_scaling_bbox = std::move(new_scale);
}
/**
* @brief Clear the scaling bbox of this ROI
*
*/
void clear_scaling_bbox()
{
std::lock_guard<std::mutex> lock(*mutex);
m_scaling_bbox = HailoBBox(0.0, 0.0, 1.0, 1.0);
}
/**
* @brief Get the stream ID of this ROI
*
* @return std::string
*/
std::string get_stream_id()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_stream_id;
}
/**
* @brief Set the stream ID of this ROI
*
* @param stream_id
*/
void set_stream_id(std::string stream_id)
{
std::lock_guard<std::mutex> lock(*mutex);
m_stream_id = std::move(stream_id);
}
};
using HailoROIPtr = std::shared_ptr<HailoROI>;
class HailoTileROI : public HailoROI
{
protected:
uint m_index;
float m_overlap_x_axis;
float m_overlap_y_axis;
uint m_layer;
hailo_tiling_mode_t m_mode;
public:
HailoTileROI(HailoBBox bbox, uint index, float overlap_x_axis, float overlap_y_axis, uint layer, hailo_tiling_mode_t mode) : HailoROI(bbox), m_index(index), m_overlap_x_axis(overlap_x_axis), m_overlap_y_axis(overlap_y_axis), m_layer(layer), m_mode(mode){};
// Move constructor
HailoTileROI(HailoTileROI &&other) noexcept : HailoROI(other),
m_index(other.m_index),
m_overlap_x_axis(other.m_overlap_x_axis),
m_overlap_y_axis(other.m_overlap_y_axis),
m_layer(other.m_layer),
m_mode(other.m_mode){};
// Copy constructor
HailoTileROI(const HailoTileROI &other) : HailoROI(other),
m_index(other.m_index),
m_overlap_x_axis(other.m_overlap_x_axis),
m_overlap_y_axis(other.m_overlap_y_axis),
m_layer(other.m_layer),
m_mode(other.m_mode){};
// Move assignment
HailoTileROI &operator=(HailoTileROI &&other) noexcept
{
if (this != &other)
{
m_bbox = std::move(other.m_bbox);
m_sub_objects = std::move(other.m_sub_objects);
m_index = other.m_index;
m_overlap_x_axis = other.m_overlap_x_axis;
m_overlap_y_axis = other.m_overlap_y_axis;
m_layer = other.m_layer;
m_mode = other.m_mode;
}
return *this;
};
// Copy assignment
HailoTileROI &operator=(const HailoTileROI &other)
{
if (this != &other)
{
m_bbox = other.m_bbox;
m_sub_objects = other.m_sub_objects;
m_index = other.m_index;
m_overlap_x_axis = other.m_overlap_x_axis;
m_overlap_y_axis = other.m_overlap_y_axis;
m_layer = other.m_layer;
m_mode = other.m_mode;
}
return *this;
};
virtual ~HailoTileROI() = default;
virtual hailo_object_t get_type()
{
return HAILO_TILE;
}
float get_overlap_x_axis() { return m_overlap_x_axis; }
float get_overlap_y_axis() { return m_overlap_y_axis; }
uint get_index() { return m_index; }
uint get_layer() { return m_layer; }
uint get_mode() { return m_mode; }
};
using HailoTileROIPtr = std::shared_ptr<HailoTileROI>;
/**
* @brief Represents a detection in a ROI. Inherits from HailoROI.
*
*/
class HailoDetection : public HailoROI
{
protected:
float m_confidence; // Confidence of the detection.
std::string m_label; // The label of detection, e.g. "Horse", "Monkey", "Tiger" for type "Animals".
int m_class_id; // Class id, initialized to -1 if missing.
public:
/**
* @brief Construct a new New Hailo Detection object
*
* @param bbox HailoBBox - a bounding box representing the region of interest in the frame.
* @param label std::string what the detection is.
* @param confidence The confidence of the detection.
* @note class id is set to -1.
*/
HailoDetection(HailoBBox bbox, const std::string &label, float confidence) : HailoROI(bbox), m_confidence(assure_normal(confidence)), m_label(label), m_class_id(NULL_CLASS_ID){};
/**
* @brief Construct a new New Hailo Detection object
*
* @param bbox HailoBBox - a bounding box representing the region of interest in the frame.
* @param class_id The detection's class id, if theres any.
* @param label std::string what the detection is.
* @param confidence The confidence of the detection.
*/
HailoDetection(HailoBBox bbox, int class_id, const std::string &label, float confidence) : HailoROI(bbox), m_confidence(assure_normal(confidence)), m_label(label), m_class_id(class_id){};
// Move constructor
HailoDetection(HailoDetection &&other) noexcept : HailoROI(other),
m_confidence(assure_normal(other.m_confidence)),
m_label(std::move(other.m_label)),
m_class_id(other.m_class_id){};
// Copy constructor
HailoDetection(const HailoDetection &other) : HailoROI(other),
m_confidence(assure_normal(other.m_confidence)),
m_label(other.m_label),
m_class_id(other.m_class_id){};
virtual ~HailoDetection() = default;
// Move assignment
HailoDetection &operator=(HailoDetection &&other) noexcept
{
if (this != &other)
{
HailoROI::operator=(other);
m_confidence = assure_normal(other.m_confidence);
m_class_id = other.m_class_id;
m_label = std::move(other.m_label);
}
return *this;
};
// Copy assignment
HailoDetection &operator=(const HailoDetection &other)
{
if (this != &other)
{
HailoROI::operator=(other);
m_confidence = assure_normal(other.m_confidence);
m_class_id = other.m_class_id;
m_label = other.m_label;
}
return *this;
};
// Overload comparison operators
bool operator<(const HailoDetection &other) const
{
return this->m_confidence < other.m_confidence;
}
bool operator>(const HailoDetection &other) const
{
return this->m_confidence > other.m_confidence;
}
virtual hailo_object_t get_type()
{
std::lock_guard<std::mutex> lock(*mutex);
return HAILO_DETECTION;
}
std::shared_ptr<HailoObject> clone()
{
std::lock_guard<std::mutex> lock(*mutex);
return std::make_shared<HailoDetection>(*this);
}
// Getters of DetectionObject.
float get_confidence()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_confidence;
}
void set_confidence(float conf)
{
std::lock_guard<std::mutex> lock(*mutex);
m_confidence = conf;
}
std::string get_label()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_label;
}
void set_label(std::string label)
{
std::lock_guard<std::mutex> lock(*mutex);
m_label = label;
}
int get_class_id()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_class_id;
}
};
using HailoDetectionPtr = std::shared_ptr<HailoDetection>;
/**
* @brief Represents a Classification of an ROI.
*
*/
class HailoClassification : public HailoObject
{
protected:
float m_confidence; // Confidence of the classification.
std::string m_classification_type; // Type of labeling, e.g. "age", "gender", "color", etc...
std::string m_label; // The label of classification, e.g. "Horse", "Monkey", "Tiger" for type "Animals".
int m_class_id; // Class id, initialized to -1 if missing.
public:
/**
* @brief Construct a new Hailo Classification object
*
* @param classification_type The type of classification.
* @param label classification result.
* @param confidence confidence of classification result.
*/
HailoClassification(const std::string &classification_type,
const std::string &label,
float confidence) : m_confidence(assure_normal(confidence)), m_classification_type(classification_type), m_label(label), m_class_id(NULL_CLASS_ID){};
/**
* @brief Construct a new Hailo Classification object
*
* @param classification_type The type of classification.
* @param label classification result.
*/
HailoClassification(const std::string &classification_type,
const std::string &label) : m_confidence(assure_normal(1.0f)), m_classification_type(classification_type), m_label(label), m_class_id(NULL_CLASS_ID){};
/**
* @brief Construct a new Hailo Classification object
*
* @param classification_type The type of classification.
* @param class_id Class ID of the classification result.
* @param label classification result.
* @param confidence confidence of classification result.
*/
HailoClassification(const std::string &classification_type,
int class_id,
std::string label,
float confidence) : m_confidence(assure_normal(confidence)), m_classification_type(classification_type), m_label(label), m_class_id(class_id){};
// Move Constructor
HailoClassification(HailoClassification &&other) : m_confidence(assure_normal(other.m_confidence)),
m_classification_type(std::move(other.m_classification_type)),
m_label(std::move(other.m_label)), m_class_id(other.m_class_id){};
// Copy Constructor
HailoClassification(const HailoClassification &other) : m_confidence(assure_normal(other.m_confidence)),
m_classification_type(other.m_classification_type),
m_label(other.m_label), m_class_id(other.m_class_id){};
virtual ~HailoClassification() = default;
// Move assignment
HailoClassification &operator=(HailoClassification &&other) noexcept
{
if (this != &other)
{
HailoObject::operator=(other);
m_confidence = assure_normal(other.m_confidence);
m_classification_type = std::move(other.m_classification_type);
m_label = std::move(other.m_label);
m_class_id = other.m_class_id;
}
return *this;
};
// Copy assignment
HailoClassification &operator=(const HailoClassification &other)
{
if (this != &other)
{
HailoObject::operator=(other);
m_confidence = assure_normal(other.m_confidence);
m_classification_type = other.m_classification_type;
m_label = other.m_label;
m_class_id = other.m_class_id;
}
return *this;
};
std::shared_ptr<HailoObject> clone()
{
std::lock_guard<std::mutex> lock(*mutex);
return std::make_shared<HailoClassification>(*this);
}
virtual hailo_object_t get_type()
{
std::lock_guard<std::mutex> lock(*mutex);
return HAILO_CLASSIFICATION;
}
// Getters to HailoClassification
float get_confidence()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_confidence;
}
std::string get_label()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_label;
}
std::string get_classification_type()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_classification_type;
}
int get_class_id()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_class_id;
}
};
using HailoClassificationPtr = std::shared_ptr<HailoClassification>;
/**
* @brief Represents a set of landmarks on a given ROI.
*
*/
class HailoLandmarks : public HailoObject
{
protected:
std::string m_landmarks_type; // Type of labeling, e.g. "pose", "facial landmarking", etc...
std::vector<HailoPoint> m_points; // Vector of points.
float m_threshold; // Threshold of landmark network.
const std::vector<std::pair<int, int>> m_pairs; // pairs of landmarks that should be connected in the overlay
public:
/**
* @brief Construct a new Hailo Landmarks object
*
* @param landmarks_name landmarks_name Type of landmarks.
* @param threshold threshold Minimum threshold of points to decide whether they are valid.
* @param pairs vector of pairs of joints that should be connected in overlay
*/
HailoLandmarks(std::string landmarks_name, float threshold = 0.0f, const std::vector<std::pair<int, int>> pairs = {}) : m_landmarks_type(landmarks_name), m_threshold(threshold), m_pairs(pairs){};
/**
* @brief Construct a new Hailo Landmarks object
*
* @param landmarks_name Type of landmarks.
* @param points Set of landmarks represented as std::vector<HailoPoint>.
* @param threshold Minimum threshold of points to decide whether they are valid.
*/
HailoLandmarks(std::string landmarks_name,
std::vector<HailoPoint> points,
float threshold = 0.0f,
const std::vector<std::pair<int, int>> pairs = {}) : m_landmarks_type(landmarks_name),
m_points(std::move(points)),
m_threshold(threshold),
m_pairs(pairs){};
virtual ~HailoLandmarks() = default;
// Move constructor
HailoLandmarks(HailoLandmarks &&other) : m_landmarks_type(std::move(other.m_landmarks_type)), m_points(std::move(other.m_points)), m_threshold(other.m_threshold), m_pairs(other.m_pairs){};
// Copy constructor
HailoLandmarks(const HailoLandmarks &other) : m_landmarks_type(other.m_landmarks_type), m_points(other.m_points), m_threshold(other.m_threshold), m_pairs(other.m_pairs){};
HailoLandmarks &operator=(const HailoLandmarks &other) = default;
HailoLandmarks &operator=(HailoLandmarks &&other) noexcept = default;
virtual hailo_object_t get_type()
{
return HAILO_LANDMARKS;
}
/**
* @brief Add a point to this Landmarks object.
*
* @param point HailoPoint to add.
*/
void add_point(HailoPoint point)
{
std::lock_guard<std::mutex> lock(*mutex);
m_points.emplace_back(point);
};
/**
* @brief Set a new vector of points to this Landmarks object.
*
* @param points std::vector<HailoPoint> to set.
*/
void set_points(std::vector<HailoPoint> points)
{
std::lock_guard<std::mutex> lock(*mutex);
m_points.clear();
m_points = std::move(points);
};
std::shared_ptr<HailoObject> clone()
{
std::lock_guard<std::mutex> lock(*mutex);
return std::make_shared<HailoLandmarks>(*this);
}
// Getters for HailoLandmarks object.
std::vector<HailoPoint> get_points()
{
std::lock_guard<std::mutex> lock(*mutex);
return m_points;
}
float get_threshold()
{
return m_threshold;
}
std::string get_landmarks_type()
{
return m_landmarks_type;
}
std::vector<std::pair<int, int>> get_pairs()
{
return m_pairs;
}
};
using HailoLandmarksPtr = std::shared_ptr<HailoLandmarks>;
/**
* @brief Represents a unique id of a ROI.
*
*/
class HailoUniqueID : public HailoObject
{
protected:
int m_unique_id; // Unique id, initialized to -1 if missing.
hailo_unique_id_mode_t m_mode; // Mode of unique id.
public:
/**
* @brief Construct a new Hailo Unique ID object
*
* @param unique_id - int
* A unique id
*/
HailoUniqueID(int unique_id, hailo_unique_id_mode_t mode = TRACKING_ID) : m_unique_id(unique_id), m_mode(mode){};
virtual ~HailoUniqueID() = default;
int get_id()
{
return m_unique_id;
}
int get_mode()
{
return m_mode;
}
std::shared_ptr<HailoObject> clone()
{
std::lock_guard<std::mutex> lock(*mutex);
return std::make_shared<HailoUniqueID>(*this);
}
virtual hailo_object_t get_type()
{
return HAILO_UNIQUE_ID;
}
};
using HailoUniqueIDPtr = std::shared_ptr<HailoUniqueID>;
class HailoMask : public HailoObject
{
protected:
int m_mask_width;
int m_mask_height;
float m_transparency;
public:
HailoMask(int mask_width, int mask_height, float transparency) : m_mask_width(mask_width), m_mask_height(mask_height), m_transparency(transparency){};
// Move Constructor
HailoMask(HailoMask &&other) = default;
// Copy Constructor
HailoMask(const HailoMask &other) = default;
virtual ~HailoMask() = default;
// Move assignment
HailoMask &operator=(HailoMask &&other) noexcept = default;
// Copy assignment
HailoMask &operator=(const HailoMask &other) = default;
int get_width()
{
return m_mask_width;
}
int get_height()
{
return m_mask_height;
}
float get_transparency()
{
return m_transparency;
}
};
using HailoMaskPtr = std::shared_ptr<HailoMask>;
class HailoDepthMask : public HailoMask
{
protected:
std::vector<float> m_data;
public:
HailoDepthMask(std::vector<float> &&data_vec, int mask_width, int mask_height, float transparency) : HailoMask(mask_width, mask_height, transparency), m_data(std::move(data_vec)){};
virtual hailo_object_t get_type()
{
return HAILO_DEPTH_MASK;
}