DaoAI World C++ SDK INDUSTRIAL 2024.8.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: 0.0.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 enum class DetectionLevel
23 {
25 };
26
27 class Point
28 {
29 public:
30 DAOAI_API Point(const float& x, const float& y, const float& confidence = 0, const std::string& label = "");
31 DAOAI_API Point(const float& x, const float& y, const std::string& label);
34
35 // overload operator<< to print out polygon object
36 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Point& point);
37
38 DAOAI_API Point operator+(const Point& point) const;
39 DAOAI_API Point operator-(const Point& point) const;
40
41 float x = 0, y = 0, confidence = 0;
42 std::string label = "";
43 };
44
45 class Polygon
46 {
47 public:
48 DAOAI_API Polygon(const std::vector<Point>& points);
51
52 // overload operator<< to print out polygon object
53 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Polygon& polygon);
54
55 // overload operator<< to print out polygon object
56 friend DAOAI_API std::stringstream& operator<<(std::stringstream& ss, const Polygon& polygon);
57
58 std::vector<Point> points;
59 };
60
61 class Box
62 {
63 public:
64
65 enum class Type
66 {
67 XYXY, XYWH
68 };
69
74
80 DAOAI_API Box(const Point& p1, const Point& p2, const float& angle = NAN);
81
89 DAOAI_API Box(const float& a1, const float& a2, const float& a3, const float& a4, const float& angle = NAN, const Type& type = Type::XYXY);
90
92
93#ifndef BUILD_INFERNCE_CLIENT
98#endif // !BUILD_INFERNCE_CLIENT
99
100 DAOAI_API std::string toString() const;
101
102 // overload operator<< to print out polygon object
103 friend DAOAI_API std::ostream& operator<<(std::ostream& ss, const Box& box);
104
105 // overload operator<< to print out polygon object
106 friend DAOAI_API std::stringstream& operator<<(std::stringstream& ss, const Box& box);
107
111 DAOAI_API float h() const;
112
116 DAOAI_API float w() const;
117
121 DAOAI_API float x1() const;
122
126 DAOAI_API float y1() const;
127
131 DAOAI_API float x2() const;
132
136 DAOAI_API float y2() const;
137
141 DAOAI_API float angle() const;
142
146 DAOAI_API float area() const;
147
148#ifndef BUILD_INFERENCE_CLIENT
152 DAOAI_API float iou(const Box& box) const;
153
157 DAOAI_API Box intersect(const Box& box) const;
158
162 DAOAI_API Box merge(const Box& box) const;
163
164
170 DAOAI_API bool contains(const Box& box) const;
171
172#endif // !BUILD_INFERENCE_CLIENT
173
178 DAOAI_API Box toType(const Type& type) const;
179
180 std::vector<float> data;
182 };
183
184#ifndef BUILD_INFERENCE_CLIENT
185 class Image
186 {
187 public:
188 enum class Type
189 {
191 };
192
198
206 DAOAI_API Image(const int& image_height, const int& image_width, const Image::Type& type, void* data);
207
215 DAOAI_API Image(const int& image_height, const int& image_width, const Image::Type& type, std::shared_ptr<uint8_t[]> data_ptr);
216
217
222 DAOAI_API Image(const std::string& file_path);
223
224 DAOAI_API virtual ~Image();
225
232
237 DAOAI_API void save(const std::string& file_path) const;
238
244
249 DAOAI_API uint8_t* getData() const;
250
255 DAOAI_API bool empty() const;
256
259 private:
260 uint8_t *data;
261 std::shared_ptr<uint8_t[]> data_ptr;
262
263 };
264
265 class Mask
266 {
267 public:
269
270 DAOAI_API Mask(const Image& image);
271
272 Mask(const Image& image, const Box& location, const int& image_height, const int& image_width);
273
275
276 DAOAI_API Mask merge(const Mask& mask) const;
277
278 DAOAI_API std::vector<Polygon> toPolygons() const;
279
280 DAOAI_API long long area() const;
281
283
284 DAOAI_API bool empty() const;
285
287 private:
288 std::vector<Image> image_data;
289 std::vector<Box> locations;
290 };
291#else
292 class Mask
293 {
294 public:
295 Mask();
296 Mask(const std::string& base64_image_data);
297 Mask(const std::vector<Polygon> polygon_data);
298
299 // either polygon_data or base64_image_data should be non-null
300 std::unique_ptr<std::vector<Polygon>> polygon_data;
301 std::unique_ptr<std::string> base64_image_data;
302 };
303#endif // !BUILD_INFERENCE_CLIENT
304 }
305}
#define DAOAI_API
Definition API_EXPORT.h:13
Definition common.h:62
DAOAI_API Polygon toPolygon() const
Convert to polygon type considering box angle.
Type type
Definition common.h:181
DAOAI_API std::string toString() const
DAOAI_API float h() const
Bounding box height.
std::vector< float > data
Definition common.h:180
DAOAI_API float x1() const
The x coordinate of top-left corner.
Type
Definition common.h:66
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:186
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:258
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:189
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:257
DAOAI_API void save(const std::string &file_path) const
Save the image to a file.
int width
Definition common.h:257
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:266
DAOAI_API Mask(const Image &image)
DAOAI_API long long area() const
Mask(const Image &image, const Box &location, const int &image_height, const int &image_width)
int height
Definition common.h:286
DAOAI_API Mask merge(const Mask &mask) const
DAOAI_API std::vector< Polygon > toPolygons() const
DAOAI_API Image toImage() const
int width
Definition common.h:286
DAOAI_API bool empty() const
Definition common.h:28
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:42
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:41
float y
Definition common.h:41
float confidence
Definition common.h:41
Definition common.h:46
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:58
friend DAOAI_API std::ostream & operator<<(std::ostream &ss, const Polygon &polygon)
DeviceType
Definition common.h:18
DetectionLevel
Definition common.h:23
Definition common.h:14