-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutil.cpp
More file actions
157 lines (145 loc) · 4.78 KB
/
util.cpp
File metadata and controls
157 lines (145 loc) · 4.78 KB
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
#include "util.h"
std::vector<std::string> split(const std::string &text, const char &separator){
std::vector<std::string> strings;
std::istringstream f(text);
std::string s;
while (getline(f, s, separator)) {
strings.push_back(s);
}
return strings;
}
std::tuple<std::vector<cv::Rect2f>, std::vector<int>> parse_box_prompts(const std::string &boxes){
std::vector<cv::Rect2f> rects;
std::vector<int> labels;
std::vector<std::string> split_semicolon = split(boxes, ';');
for(int i = 0; i < split_semicolon.size(); i++){
std::string coords;
int label = 1;
if(split_semicolon[i].rfind("pos:", 0) == 0) {
coords = split_semicolon[i].substr(4);
label = 1;
}else if(split_semicolon[i].rfind("neg:", 0) == 0) {
coords = split_semicolon[i].substr(4);
label = 0;
}
std::vector<std::string> split_comma = split(coords, ',');
if(split_comma.size() == 4){
cv::Rect2f rect = cv::Rect2f(
std::stof(split_comma[0]),
std::stof(split_comma[1]),
std::stof(split_comma[2]),
std::stof(split_comma[3]));
rects.push_back(rect);
labels.push_back(label);
}
}
return std::make_tuple(rects, labels);
}
void normalizeRects(std::vector<cv::Rect2f> *rects, const cv::Size &imageSize){
for(int i = 0; i < (*rects).size(); i++){
(*rects)[i].x /= imageSize.width;
(*rects)[i].y /= imageSize.height;
(*rects)[i].width /= imageSize.width;
(*rects)[i].height /= imageSize.height;
(*rects)[i].x = (*rects)[i].x + (*rects)[i].width / 2;
(*rects)[i].y = (*rects)[i].y + (*rects)[i].height / 2;
}
}
bool modelExists(const std::string& modelPath){
std::ifstream f(modelPath);
if (!f.good()) {
return false;
}
return true;
}
std::string LoadBytesFromFile(const std::string& path) {
std::string data;
std::ifstream fs(path, std::ios::in | std::ios::binary);
if (fs.fail()) {
std::cerr << "Cannot open " << path << std::endl;
return data;
}
fs.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(fs.tellg());
fs.seekg(0, std::ios::beg);
data.resize(size);
fs.read(data.data(), size);
return data;
}
void printShape(const std::vector<int64_t> &shape){
std::string text = "";
for(int i = 0; i < shape.size(); i++){
text = text + std::to_string(shape[i]) + " ";
}
std::cout << text << std::endl;
}
int getShapeSize(const std::vector<int64_t> &shape){
int size = 1;
for(int i = 0; i < shape.size(); i++){
size *= shape[i];
}
return size;
}
std::vector<const char*> getInputNames(std::unique_ptr<Ort::Session> &session){
std::vector<const char*> inputNames;
Ort::AllocatorWithDefaultOptions allocator;
for (size_t i = 0; i < session->GetInputCount(); ++i) {
Ort::AllocatedStringPtr name_Ptr = session->GetInputNameAllocated(i, allocator);
char* name = name_Ptr.get();
size_t name_length = strlen(name) + 1;
char* name_new = new char[name_length];
strncpy(name_new, name, name_length);
inputNames.push_back(name_new);
}
return inputNames;
}
std::vector<const char*> getOutputNames(std::unique_ptr<Ort::Session> &session){
std::vector<const char*> outputNames;
Ort::AllocatorWithDefaultOptions allocator;
for (size_t i = 0; i < session->GetOutputCount(); ++i) {
Ort::AllocatedStringPtr name_Ptr = session->GetOutputNameAllocated(i, allocator);
char* name = name_Ptr.get();
size_t name_length = strlen(name) + 1;
char* name_new = new char[name_length];
strncpy(name_new, name, name_length);
outputNames.push_back(name_new);
}
return outputNames;
}
std::vector<int> sort_indexes(const std::vector<float> &v) {
std::vector<int> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
stable_sort(idx.begin(), idx.end(), [&v](int i1, int i2) {return v[i1] > v[i2];});
return idx;
}
float calc_iou(const std::vector<int> &box1, const std::vector<int> &box2) {
int inter_x1 = std::max(box1[0], box2[0]);
int inter_y1 = std::max(box1[1], box2[1]);
int inter_x2 = std::min(box1[0] + box1[2], box2[0] + box2[2]);
int inter_y2 = std::min(box1[1] + box1[3], box2[1] + box2[3]);
int inter_width = std::max(0, inter_x2 - inter_x1);
int inter_height = std::max(0, inter_y2 - inter_y1);
int inter_area = inter_width * inter_height;
if (inter_area == 0) {
return 0;
}
int area1 = box1[2] * box1[3];
int area2 = box2[2] * box2[3];
int union_area = area1 + area2 - inter_area;
return (float)inter_area / union_area;
}
bool can_append_box(const std::vector<int> box, const std::vector<int> &boxes){
float threshold = 0.9;
int num = (int)boxes.size() / 4;
for(int i = 0; i < num; i++){
std::vector<int> box_tmp;
for(int j = 0; j < 4; j++){
box_tmp.push_back(boxes[4 * i + j]);
}
float iou = calc_iou(box, box_tmp);
if(iou > threshold){
return false;
}
}
return true;
}