DaoAI World C++ SDK INDUSTRIAL 2025.3.0
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1// Copyright (c) 2024, DaoAI Robotics. All rights reserved.
2// Version: 2025.3.0
3
4#pragma once
5#include "API_EXPORT.h"
6#include <iostream>
7#include <vector>
8#include "config.h"
9#include <memory>
10#include <variant>
11#include <cmath>
12#include <map>
13namespace DaoAI
14{
15 namespace DeepLearning
16 {
17 enum class DeviceType
18 {
19 CPU, GPU
20 };
21
22 namespace Vision
23 {
24 enum class DetectionLevel
25 {
27 };
28 };
29
30 class Point
31 {
32 public:
33 DAOAI_API Point(const float& x, const float& y, const float& confidence = 0, const std::string& label = "");
34 DAOAI_API Point(const float& x, const float& y, const std::string& label);
37
38 // overload operator<< to print out polygon object
39 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Point& point);
40
41 DAOAI_API Point operator+(const Point& point) const;
42 DAOAI_API Point operator-(const Point& point) const;
43
44 float x = 0, y = 0, confidence = 0;
45 std::string label = "";
46 };
47
48 class Polygon
49 {
50 public:
51 DAOAI_API Polygon(const std::vector<Point>& points);
54
55 // overload operator<< to print out polygon object
56 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Polygon& polygon);
57
58 // overload operator<< to print out polygon object
59 friend DAOAI_API std::stringstream& operator<<(std::stringstream& ss, const Polygon& polygon);
60
61 std::vector<Point> points;
62 };
63
64 class Box
65 {
66 public:
67
68 enum class Type
69 {
70 XYXY, XYWH
71 };
72
77
83 DAOAI_API Box(const Point& p1, const Point& p2, const float& angle = NAN);
84
92 DAOAI_API Box(const float& a1, const float& a2, const float& a3, const float& a4, const float& angle = NAN, const Type& type = Type::XYXY);
93
95
96#ifndef BUILD_INFERNCE_CLIENT
101#endif // !BUILD_INFERNCE_CLIENT
102
103 DAOAI_API std::string toString() const;
104
105 // overload operator<< to print out polygon object
106 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Box& box);
107
108 // overload operator<< to print out polygon object
109 friend DAOAI_API std::stringstream& operator<<(std::stringstream& ss, const Box& box);
110
114 DAOAI_API float h() const;
115
119 DAOAI_API float w() const;
120
124 DAOAI_API float x1() const;
125
129 DAOAI_API float y1() const;
130
134 DAOAI_API float x2() const;
135
139 DAOAI_API float y2() const;
140
144 DAOAI_API float angle() const;
145
149 DAOAI_API float area() const;
150
151#ifndef BUILD_INFERENCE_CLIENT
155 DAOAI_API float iou(const Box& box) const;
156
160 DAOAI_API Box intersect(const Box& box) const;
161
165 DAOAI_API Box merge(const Box& box) const;
166
167
173 DAOAI_API bool contains(const Box& box) const;
174
175#endif // !BUILD_INFERENCE_CLIENT
176
181 DAOAI_API Box toType(const Type& type) const;
182
183 std::vector<float> data;
185 };
186
187#ifndef BUILD_INFERENCE_CLIENT
188 class Image
189 {
190 public:
191 enum class Type
192 {
194 };
195
201
209 DAOAI_API Image(const int& image_height, const int& image_width, const Image::Type& type, void* data);
210
218 DAOAI_API Image(const int& image_height, const int& image_width, const Image::Type& type, std::shared_ptr<uint8_t[]> data_ptr);
219
220
225 DAOAI_API Image(const std::string& file_path);
226
227 DAOAI_API virtual ~Image();
228
235
240 DAOAI_API void save(const std::string& file_path) const;
241
247
252 DAOAI_API uint8_t* getData() const;
253
258 DAOAI_API bool empty() const;
259
262 private:
263 uint8_t *data;
264 std::shared_ptr<uint8_t[]> data_ptr;
265
266 };
267
268 class Mask
269 {
270 public:
272
273 DAOAI_API Mask(const Image& image);
274
275 Mask(const Image& image, const Box& location, const int& image_height, const int& image_width);
276
278
284 DAOAI_API Mask merge(const Mask& mask) const;
285
290 DAOAI_API std::vector<Polygon> toPolygons() const;
291
296 DAOAI_API long long area() const;
297
303
308 DAOAI_API bool empty() const;
309
315
320 std::vector<Image>& getImageData();
321
326 std::vector<Box>& getLocations();
327
329 private:
330 std::vector<Image> image_data;
331 std::vector<Box> locations;
332 };
333#else
334 class Mask
335 {
336 public:
337 Mask();
338 Mask(const std::string& base64_image_data);
339 Mask(const std::vector<Polygon> polygon_data);
340
341 // either polygon_data or base64_image_data should be non-null
342 std::unique_ptr<std::vector<Polygon>> polygon_data;
343 std::unique_ptr<std::string> base64_image_data;
344 };
345#endif // !BUILD_INFERENCE_CLIENT
346 }
347}
#define DAOAI_API
Definition API_EXPORT.h:13
Definition common.h:65
DAOAI_API Polygon toPolygon() const
Convert to polygon type considering box angle.
Type type
Definition common.h:184
DAOAI_API std::string toString() const
DAOAI_API float h() const
Bounding box height.
std::vector< float > data
Definition common.h:183
DAOAI_API float x1() const
The x coordinate of top-left corner.
Type
Definition common.h:69
DAOAI_API bool contains(const Box &box) const
Check if this box contains another box.
DAOAI_API float w() const
Bounding box width.
DAOAI_API Box intersect(const Box &box) const
Calculate insection box.
DAOAI_API float area() const
The area of the box.
DAOAI_API float iou(const Box &box) const
Calculate iou with a box.
DAOAI_API Box()
Construct a empty box.
DAOAI_API Box toType(const Type &type) const
Convert to different box type.
friend DAOAI_API std::ostream & operator<<(std::ostream &ss, const Box &box)
DAOAI_API float x2() const
The x coordinate of bottom-right corner.
DAOAI_API Box merge(const Box &box) const
Calculate union box.
DAOAI_API float angle() const
The rotate angle of he bounding box.
DAOAI_API Box(const Point &p1, const Point &p2, const float &angle=NAN)
Construct a box given the top-left and right-bottom points.
DAOAI_API float y2() const
The y coordinate of bottom-right corner.
DAOAI_API Box(const float &a1, const float &a2, const float &a3, const float &a4, const float &angle=NAN, const Type &type=Type::XYXY)
Construct a box given the coordinates of top-left and bottom-right points.
friend DAOAI_API std::stringstream & operator<<(std::stringstream &ss, const Box &box)
DAOAI_API float y1() const
The y coordinate of top-left corner.
Definition common.h:189
DAOAI_API uint8_t * getData() const
Get the data buffer of the image.
DAOAI_API Image()
Default constructor for Image object.
DAOAI_API Image clone() const
Clone the image.
Type type
Definition common.h:261
DAOAI_API Image(const int &image_height, const int &image_width, const Image::Type &type, void *data)
Construct image from data buffer.
Type
Definition common.h:192
DAOAI_API bool empty() const
Check if the image is empty.
DAOAI_API Image toType(const Image::Type &type) const
Convert image to different type.
virtual DAOAI_API ~Image()
DAOAI_API Image(const std::string &file_path)
Construct image from file path.
int height
Definition common.h:260
DAOAI_API void save(const std::string &file_path) const
Save the image to a file.
int width
Definition common.h:260
DAOAI_API Image(const int &image_height, const int &image_width, const Image::Type &type, std::shared_ptr< uint8_t[]> data_ptr)
Construct image from data buffer.
Definition common.h:269
DAOAI_API Mask(const Image &image)
Mask getMergedMask() const
Construct merged mask, merge() method only store the partial mask data but not create a full mask,...
DAOAI_API long long area() const
Get the area of the mask.
std::vector< Box > & getLocations()
Get the mask locations.
Mask(const Image &image, const Box &location, const int &image_height, const int &image_width)
int height
Definition common.h:328
DAOAI_API Mask merge(const Mask &mask) const
merging two masks, masks must have the same width and height.
DAOAI_API std::vector< Polygon > toPolygons() const
Constructing polygons from mask data.
DAOAI_API Image toImage() const
Constructing image from mask data.
std::vector< Image > & getImageData()
Get the mask data.
int width
Definition common.h:328
DAOAI_API bool empty() const
Check if the mask is empty.
Definition common.h:31
DAOAI_API Point operator-(const Point &point) const
friend DAOAI_API std::ostream & operator<<(std::ostream &ss, const Point &point)
DAOAI_API Point(const float &x, const float &y, const std::string &label)
std::string label
Definition common.h:45
DAOAI_API Point operator+(const Point &point) const
DAOAI_API Point(const float &x, const float &y, const float &confidence=0, const std::string &label="")
float x
Definition common.h:44
float y
Definition common.h:44
float confidence
Definition common.h:44
Definition common.h:49
DAOAI_API Polygon(const std::vector< Point > &points)
friend DAOAI_API std::stringstream & operator<<(std::stringstream &ss, const Polygon &polygon)
std::vector< Point > points
Definition common.h:61
friend DAOAI_API std::ostream & operator<<(std::ostream &ss, const Polygon &polygon)
DetectionLevel
Definition common.h:25
DeviceType
Definition common.h:18
Definition common.h:14