-
Notifications
You must be signed in to change notification settings - Fork 870
/
objdetect.cpp
314 lines (236 loc) · 9.92 KB
/
objdetect.cpp
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
#include "objdetect.h"
// CascadeClassifier
CascadeClassifier CascadeClassifier_New() {
return new cv::CascadeClassifier();
}
void CascadeClassifier_Close(CascadeClassifier cs) {
delete cs;
}
int CascadeClassifier_Load(CascadeClassifier cs, const char* name) {
return cs->load(name);
}
struct Rects CascadeClassifier_DetectMultiScale(CascadeClassifier cs, Mat img) {
std::vector<cv::Rect> detected;
cs->detectMultiScale(*img, detected); // uses all default parameters
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
struct Rects CascadeClassifier_DetectMultiScaleWithParams(CascadeClassifier cs, Mat img,
double scale, int minNeighbors, int flags, Size minSize, Size maxSize) {
cv::Size minSz(minSize.width, minSize.height);
cv::Size maxSz(maxSize.width, maxSize.height);
std::vector<cv::Rect> detected;
cs->detectMultiScale(*img, detected, scale, minNeighbors, flags, minSz, maxSz);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
// HOGDescriptor
HOGDescriptor HOGDescriptor_New() {
return new cv::HOGDescriptor();
}
void HOGDescriptor_Close(HOGDescriptor hog) {
delete hog;
}
int HOGDescriptor_Load(HOGDescriptor hog, const char* name) {
return hog->load(name);
}
struct Rects HOGDescriptor_DetectMultiScale(HOGDescriptor hog, Mat img) {
std::vector<cv::Rect> detected;
hog->detectMultiScale(*img, detected);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
struct Rects HOGDescriptor_DetectMultiScaleWithParams(HOGDescriptor hog, Mat img,
double hitThresh, Size winStride, Size padding, double scale, double finalThresh,
bool useMeanshiftGrouping) {
cv::Size wSz(winStride.width, winStride.height);
cv::Size pSz(padding.width, padding.height);
std::vector<cv::Rect> detected;
hog->detectMultiScale(*img, detected, hitThresh, wSz, pSz, scale, finalThresh,
useMeanshiftGrouping);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
Mat HOG_GetDefaultPeopleDetector() {
return new cv::Mat(cv::HOGDescriptor::getDefaultPeopleDetector());
}
void HOGDescriptor_SetSVMDetector(HOGDescriptor hog, Mat det) {
hog->setSVMDetector(*det);
}
struct Rects GroupRectangles(struct Rects rects, int groupThreshold, double eps) {
std::vector<cv::Rect> vRect;
for (int i = 0; i < rects.length; ++i) {
cv::Rect r = cv::Rect(rects.rects[i].x, rects.rects[i].y, rects.rects[i].width,
rects.rects[i].height);
vRect.push_back(r);
}
cv::groupRectangles(vRect, groupThreshold, eps);
Rect* results = new Rect[vRect.size()];
for (size_t i = 0; i < vRect.size(); ++i) {
Rect r = {vRect[i].x, vRect[i].y, vRect[i].width, vRect[i].height};
results[i] = r;
}
Rects ret = {results, (int)vRect.size()};
return ret;
}
// QRCodeDetector
QRCodeDetector QRCodeDetector_New() {
return new cv::QRCodeDetector();
}
void QRCodeDetector_Close(QRCodeDetector qr) {
delete qr;
}
const char* QRCodeDetector_DetectAndDecode(QRCodeDetector qr, Mat input,Mat points,Mat straight_qrcode) {
cv::String *str = new cv::String(qr->detectAndDecode(*input,*points,*straight_qrcode));
return str->c_str();
}
bool QRCodeDetector_Detect(QRCodeDetector qr, Mat input,Mat points) {
return qr->detect(*input,*points);
}
const char* QRCodeDetector_Decode(QRCodeDetector qr, Mat input,Mat inputPoints,Mat straight_qrcode) {
cv::String *str = new cv::String(qr->detectAndDecode(*input,*inputPoints,*straight_qrcode));
return str->c_str();
}
bool QRCodeDetector_DetectMulti(QRCodeDetector qr, Mat input, Mat points) {
return qr->detectMulti(*input,*points);
}
bool QRCodeDetector_DetectAndDecodeMulti(QRCodeDetector qr, Mat input, CStrings* decoded, Mat points, struct Mats* qrCodes) {
std::vector<cv::String> decodedCodes;
std::vector<cv::Mat> straightQrCodes;
bool res = qr->detectAndDecodeMulti(*input, decodedCodes, *points, straightQrCodes);
if (!res) {
return res;
}
qrCodes->mats = new Mat[straightQrCodes.size()];
qrCodes->length = straightQrCodes.size();
for (size_t i = 0; i < straightQrCodes.size(); i++) {
qrCodes->mats[i] = new cv::Mat(straightQrCodes[i]);
}
const char **strs = new const char*[decodedCodes.size()];
for (size_t i = 0; i < decodedCodes.size(); ++i) {
strs[i] = decodedCodes[i].c_str();
}
decoded->length = decodedCodes.size();
decoded->strs = strs;
return res;
}
FaceDetectorYN FaceDetectorYN_Create(const char* model, const char* config, Size size) {
cv::String smodel = cv::String(model);
cv::String sconfig = cv::String(config);
cv::Size ssize = cv::Size(size.width, size.height);
return new cv::Ptr<cv::FaceDetectorYN>(cv::FaceDetectorYN::create(smodel, sconfig, ssize));
}
FaceDetectorYN FaceDetectorYN_Create_WithParams(const char* model, const char* config, Size size, float score_threshold, float nms_threshold, int top_k, int backend_id, int target_id) {
cv::String smodel = cv::String(model);
cv::String sconfig = cv::String(config);
cv::Size ssize = cv::Size(size.width, size.height);
return new cv::Ptr<cv::FaceDetectorYN>(cv::FaceDetectorYN::create(smodel, sconfig, ssize, score_threshold, nms_threshold, top_k, backend_id, target_id));
}
FaceDetectorYN FaceDetectorYN_Create_FromBytes(const char* framework, void* bufferModel, int model_size, void* bufferConfig, int config_size, Size size) {
cv::String sframework = cv::String(framework);
cv::Size ssize = cv::Size(size.width, size.height);
std::vector<uchar> bufferModelV;
std::vector<uchar> bufferConfigV;
uchar* bmv = (uchar*)bufferModel;
uchar* bcv = (uchar*)bufferConfig;
for(int i = 0; i < model_size; i ++) {
bufferModelV.push_back(bmv[i]);
}
for(int i = 0; i < config_size; i ++) {
bufferConfigV.push_back(bcv[i]);
}
return new cv::Ptr<cv::FaceDetectorYN>(cv::FaceDetectorYN::create(sframework, bufferModelV, bufferConfigV, ssize));
}
FaceDetectorYN FaceDetectorYN_Create_FromBytes_WithParams(const char* framework, void* bufferModel, int model_size, void* bufferConfig, int config_size, Size size, float score_threshold, float nms_threshold, int top_k, int backend_id, int target_id) {
cv::String sframework = cv::String(framework);
cv::Size ssize = cv::Size(size.width, size.height);
std::vector<uchar> bufferModelV;
std::vector<uchar> bufferConfigV;
uchar* bmv = (uchar*)bufferModel;
uchar* bcv = (uchar*)bufferConfig;
for(int i = 0; i < model_size; i ++) {
bufferModelV.push_back(bmv[i]);
}
for(int i = 0; i < config_size; i ++) {
bufferConfigV.push_back(bcv[i]);
}
return new cv::Ptr<cv::FaceDetectorYN>(cv::FaceDetectorYN::create(sframework, bufferModelV, bufferConfigV, ssize, score_threshold, nms_threshold, top_k, backend_id, target_id));
}
void FaceDetectorYN_Close(FaceDetectorYN fd) {
delete fd;
}
int FaceDetectorYN_Detect(FaceDetectorYN fd, Mat image, Mat faces) {
return (*fd)->detect(*image, *faces);
}
Size FaceDetectorYN_GetInputSize(FaceDetectorYN fd) {
Size sz;
cv::Size cvsz = (*fd)->getInputSize();
sz.width = cvsz.width;
sz.height = cvsz.height;
return sz;
}
float FaceDetectorYN_GetNMSThreshold(FaceDetectorYN fd) {
return (*fd)->getNMSThreshold();
}
float FaceDetectorYN_GetScoreThreshold(FaceDetectorYN fd) {
return (*fd)->getScoreThreshold();
}
int FaceDetectorYN_GetTopK(FaceDetectorYN fd) {
return (*fd)->getTopK();
}
void FaceDetectorYN_SetInputSize(FaceDetectorYN fd, Size input_size){
cv::Size isz(input_size.width, input_size.height);
(*fd)->setInputSize(isz);
}
void FaceDetectorYN_SetNMSThreshold(FaceDetectorYN fd, float nms_threshold){
(*fd)->setNMSThreshold(nms_threshold);
}
void FaceDetectorYN_SetScoreThreshold(FaceDetectorYN fd, float score_threshold){
(*fd)->setScoreThreshold(score_threshold);
}
void FaceDetectorYN_SetTopK(FaceDetectorYN fd, int top_k){
(*fd)->setTopK(top_k);
}
FaceRecognizerSF FaceRecognizerSF_Create(const char* model, const char* config) {
return FaceRecognizerSF_Create_WithParams(model, config, 0, 0);
}
FaceRecognizerSF FaceRecognizerSF_Create_WithParams(const char* model, const char* config, int backend_id, int target_id) {
cv::Ptr<cv::FaceRecognizerSF>* p = new cv::Ptr<cv::FaceRecognizerSF>(cv::FaceRecognizerSF::create(model, config, backend_id, target_id));
return p;
}
void FaceRecognizerSF_Close(FaceRecognizerSF fr) {
delete fr;
}
void FaceRecognizerSF_AlignCrop(FaceRecognizerSF fr, Mat src_img, Mat face_box, Mat aligned_img) {
(*fr)->alignCrop(*src_img, *face_box, *aligned_img);
}
void FaceRecognizerSF_Feature(FaceRecognizerSF fr, Mat aligned_img, Mat face_feature) {
(*fr)->feature(*aligned_img, *face_feature);
}
float FaceRecognizerSF_Match(FaceRecognizerSF fr, Mat face_feature1, Mat face_feature2) {
return FaceRecognizerSF_Match_WithParams(fr, face_feature1, face_feature2, 0);
}
float FaceRecognizerSF_Match_WithParams(FaceRecognizerSF fr, Mat face_feature1, Mat face_feature2, int dis_type) {
double rv = (*fr)->match(*face_feature1, *face_feature2, dis_type);
return (float)rv;
}