-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
objdetect.dart
253 lines (227 loc) · 8.99 KB
/
objdetect.dart
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
library cv;
import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';
import '../core/rect.dart';
import '../core/base.dart';
import '../core/mat.dart';
import '../core/size.dart';
import '../core/vec.dart';
import '../core/point.dart';
import '../opencv.g.dart' as cvg;
class CascadeClassifier extends CvStruct<cvg.CascadeClassifier> {
CascadeClassifier._(cvg.CascadeClassifierPtr ptr) : super.fromPointer(ptr) {
finalizer.attach(this, ptr.cast());
}
factory CascadeClassifier.empty() {
final p = calloc<cvg.CascadeClassifier>();
cvRun(() => CFFI.CascadeClassifier_New(p));
return CascadeClassifier._(p);
}
/// Load cascade classifier from a file.
///
/// For further details, please see:
/// http://docs.opencv.org/master/d1/de5/classcv_1_1CascadeClassifier.html#a1a5884c8cc749422f9eb77c2471958bc
bool load(String name) {
return using<bool>((arena) {
final cname = name.toNativeUtf8(allocator: arena);
final p = arena<ffi.Int>();
cvRun(() => CFFI.CascadeClassifier_Load(ref, cname.cast(), p));
return p.value != 0;
});
}
/// DetectMultiScale detects objects of different sizes in the input Mat image.
/// The detected objects are returned as a slice of image.Rectangle structs.
///
/// For further details, please see:
/// http://docs.opencv.org/master/d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498
VecRect detectMultiScale(
InputArray image, {
double scaleFactor = 1.1,
int minNeighbors = 3,
int flags = 0,
Size minSize = (0, 0),
Size maxSize = (0, 0),
}) {
return using<VecRect>((arena) {
final ret = arena<cvg.VecRect>();
cvRun(() => CFFI.CascadeClassifier_DetectMultiScaleWithParams(ref, image.ref, scaleFactor,
minNeighbors, flags, minSize.toSize(arena).ref, maxSize.toSize(arena).ref, ret));
return VecRect.fromVec(ret.ref);
});
}
@override
cvg.CascadeClassifier get ref => ptr.ref;
static final finalizer =
OcvFinalizer<cvg.CascadeClassifierPtr>(CFFI.addresses.CascadeClassifier_Close);
@override
List<int> get props => [ptr.address];
}
class HOGDescriptor extends CvStruct<cvg.HOGDescriptor> {
HOGDescriptor._(cvg.HOGDescriptorPtr ptr) : super.fromPointer(ptr) {
finalizer.attach(this, ptr.cast());
}
factory HOGDescriptor.empty() {
final p = calloc<cvg.HOGDescriptor>();
cvRun(() => CFFI.HOGDescriptor_New(p));
return HOGDescriptor._(p);
}
/// DetectMultiScale calls DetectMultiScale but allows setting parameters
/// The detected objects are returned as a slice of image.Rectangle structs.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d5/d33/structcv_1_1HOGDescriptor.html#a660e5cd036fd5ddf0f5767b352acd948
VecRect detectMultiScale(
InputArray image, {
double hitThreshold = 0,
int minNeighbors = 3,
Size winStride = (0, 0),
Size padding = (0, 0),
double scale = 1.05,
double groupThreshold = 2.0,
bool useMeanshiftGrouping = false,
}) {
return using<VecRect>((arena) {
final rects = VecRect();
cvRun(
() => CFFI.HOGDescriptor_DetectMultiScaleWithParams(
ref,
image.ref,
hitThreshold,
winStride.toSize(arena).ref,
padding.toSize(arena).ref,
scale,
groupThreshold,
useMeanshiftGrouping,
rects.ptr,
),
);
return rects;
});
}
/// HOGDefaultPeopleDetector returns a new Mat with the HOG DefaultPeopleDetector.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d5/d33/structcv_1_1HOGDescriptor.html#a660e5cd036fd5ddf0f5767b352acd948
static VecFloat getDefaultPeopleDetector() {
return cvRunArena<VecFloat>((arena) {
final v = arena<cvg.VecFloat>();
cvRun(() => CFFI.HOG_GetDefaultPeopleDetector(v));
return VecFloat.fromVec(v.ref);
});
}
/// SetSVMDetector sets the data for the HOGDescriptor.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d5/d33/structcv_1_1HOGDescriptor.html#a09e354ad701f56f9c550dc0385dc36f1
void setSVMDetector(VecFloat det) {
cvRun(() => CFFI.HOGDescriptor_SetSVMDetector(ref, det.ref));
}
@override
cvg.HOGDescriptor get ref => ptr.ref;
static final finalizer = OcvFinalizer<cvg.HOGDescriptorPtr>(CFFI.addresses.HOGDescriptor_Close);
@override
List<int> get props => [ptr.address];
}
// GroupRectangles groups the object candidate rectangles.
//
// For further details, please see:
// https://docs.opencv.org/master/d5/d54/group__objdetect.html#ga3dba897ade8aa8227edda66508e16ab9
VecRect groupRectangles(VecRect rects, int groupThreshold, double eps) {
return using<VecRect>((arena) {
cvRun(() => CFFI.GroupRectangles(rects.ref, groupThreshold, eps));
return rects;
});
}
// QRCodeDetector groups the object candidate rectangles.
//
// For further details, please see:
// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html
class QRCodeDetector extends CvStruct<cvg.QRCodeDetector> {
QRCodeDetector._(cvg.QRCodeDetectorPtr ptr) : super.fromPointer(ptr) {
finalizer.attach(this, ptr.cast());
}
factory QRCodeDetector.empty() {
final p = calloc<cvg.QRCodeDetector>();
cvRun(() => CFFI.QRCodeDetector_New(p));
return QRCodeDetector._(p);
}
/// DetectAndDecode Both detects and decodes QR code.
///
/// Returns true as long as some QR code was detected even in case where the decoding failed
/// For further details, please see:
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#a7290bd6a5d59b14a37979c3a14fbf394
(String ret, VecPoint? points, Mat? straightCode) detectAndDecode(
InputArray img, {
VecPoint? points,
OutputArray? straightCode,
}) {
straightCode ??= Mat.empty();
final points = VecPoint.fromList([]);
final s = cvRunArena<String>((arena) {
final v = arena<cvg.VecChar>();
cvRun(() =>
CFFI.QRCodeDetector_DetectAndDecode(ref, img.ref, points.ptr, straightCode!.ptr, v));
if (v == ffi.nullptr) return "";
return VecChar.fromVec(v.ref).toString();
});
return (s, points, straightCode);
}
/// Detect detects QR code in image and returns the quadrangle containing the code.
///
/// For further details, please see:
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#a64373f7d877d27473f64fe04bb57d22b
(bool ret, VecPoint? points) detect(InputArray input, {VecPoint? points}) {
return cvRunArena<(bool, VecPoint?)>((arena) {
final pts = VecPoint();
final ret = arena<ffi.Bool>();
cvRun(() => CFFI.QRCodeDetector_Detect(ref, input.ref, pts.ref, ret));
return (ret.value, pts);
});
}
/// Decode decodes QR code in image once it's found by the detect() method. Returns UTF8-encoded output string or empty string if the code cannot be decoded.
///
/// For further details, please see:
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#a4172c2eb4825c844fb1b0ae67202d329
(String ret, VecPoint? points, Mat? straightCode) decode(
InputArray img, {
VecPoint? points,
Mat? straightCode,
}) {
return cvRunArena<(String, VecPoint?, Mat?)>((arena) {
points ??= VecPoint();
final ret = VecChar();
straightCode ??= Mat.empty();
cvRun(
() => CFFI.QRCodeDetector_Decode(ref, img.ref, points!.ref, straightCode!.ref, ret.ptr));
return (ret.toString(), points, straightCode!);
});
}
/// Detects QR codes in image and finds of the quadrangles containing the codes.
///
/// Each quadrangle would be returned as a row in the `points` Mat and each point is a Vecf.
/// Returns true if QR code was detected
/// For usage please see TestQRCodeDetector
/// For further details, please see:
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#aaf2b6b2115b8e8fbc9acf3a8f68872b6
(bool, VecPoint? points) detectMulti(InputArray img, {VecPoint? points}) {
return cvRunArena<(bool, VecPoint?)>((arena) {
points ??= VecPoint.fromList([]);
final ret = arena<ffi.Bool>();
cvRun(() => CFFI.QRCodeDetector_DetectMulti(ref, img.ref, points!.ref, ret));
return ret.value ? (ret.value, VecPoint.fromVec(points!.ref)) : (ret.value, null);
});
}
/// Detects QR codes in image, finds the quadrangles containing the codes, and decodes the QRCodes to strings.
///
/// Each quadrangle would be returned as a row in the `points` Mat and each point is a Vecf.
/// Returns true as long as some QR code was detected even in case where the decoding failed
/// For usage please see TestQRCodeDetector
/// For further details, please see:
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#a188b63ffa17922b2c65d8a0ab7b70775
// TODO: (bool, List<String>, Mat, List<Mat>) detectAndDecodeMulti(InputArray img) {}
static final finalizer = OcvFinalizer<cvg.QRCodeDetectorPtr>(CFFI.addresses.QRCodeDetector_Close);
@override
cvg.QRCodeDetector get ref => ptr.ref;
@override
List<int> get props => [ptr.address];
}