-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbNet.js
267 lines (230 loc) · 6.38 KB
/
dbNet.js
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
import BaseModel from "./baseModel.js"
import OcrUtils from "./utils.js";
class TextBox {
}
export default class dbNetModel extends BaseModel {
constructor() {
super("./models/dbnet.onnx");
}
async run(src, scale, boxScoreThresh, boxThresh, unClipRatio) {
await this.modelLoading;
let srcResize = new cv.Mat();
cv.resize(src, srcResize, new cv.Size(scale.dstWidth, scale.dstHeight));
console.time("SubstractMeanNormalize")
const input0 = OcrUtils.SubstractMeanNormalize(
srcResize, [0.485 * 255, 0.456 * 255, 0.406 * 255], [1.0 / 0.229 / 255.0, 1.0 / 0.224 / 255.0, 1.0 / 0.225 / 255.0]
);
console.timeEnd("SubstractMeanNormalize")
const feeds = {
input0
};
console.time("dbNetModel")
const results = await this.session.run(feeds);
console.timeEnd("dbNetModel")
try {
return this.GetTextBoxes(
results.out1.data,
srcResize.rows,
srcResize.cols,
scale,
boxScoreThresh,
boxThresh,
unClipRatio
);
} finally {
srcResize.delete();
}
}
LengthOfPoints(box) {
let length = 0;
let pt = box[0];
let x0 = pt.x;
let y0 = pt.y;
let x1 = 0,
y1 = 0,
dx = 0,
dy = 0;
box.push(pt);
let count = box.length;
for (let idx = 1; idx < count; idx++) {
let pts = box[idx];
x1 = pts.x;
y1 = pts.y;
dx = x1 - x0;
dy = y1 - y0;
length += Math.sqrt(dx * dx + dy * dy);
x0 = x1;
y0 = y1;
}
box.splice(count - 1);
return length;
}
SignedPolygonArea(Points) {
// Add the first point to the end.
let num_points = Points.length;
let pts = Points.slice();
// Points.CopyTo(pts, 0);
pts[num_points] = Points[0];
// Get the areas.
let area = 0;
for (let i = 0; i < num_points; i++) {
area +=
(pts[i + 1].x - pts[i].x) *
(pts[i + 1].y + pts[i].y) / 2;
}
return area;
}
Unclip(box, unclip_ratio) {
let theCliperPts = [];
for (let pt of box) {
let a1 = new ClipperLib.IntPoint(pt.x, pt.y);
theCliperPts.push(a1);
}
let area = Math.abs(this.SignedPolygonArea(box));
let length = this.LengthOfPoints(box);
let distance = area * unclip_ratio / length;
let co = new ClipperLib.ClipperOffset();
co.AddPath(theCliperPts, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
let solution = [];
co.Execute(solution, distance);
if (solution.length == 0) {
return null;
}
let retPts = [];
for (let ip of solution[0]) {
retPts.push(ip.X, ip.Y);
}
return cv.matFromArray(retPts.length / 2, 1, cv.CV_32SC2, new Float32Array(retPts));
}
GetMiniBox(contours) {
let box = [];
let rrect = cv.minAreaRect(contours);
let points = cv.RotatedRect.points(rrect);
let thePoints = points.slice();
thePoints.sort((left, right) => {
if (left == null && right == null) {
return 1;
}
if (left == null) {
return 0;
}
if (right == null) {
return 1;
}
if (left.x > right.x) {
return 1;
}
if (left.x == right.x) {
return 0;
}
return -1;
});
let index_1 = 0,
index_2 = 1,
index_3 = 2,
index_4 = 3;
if (thePoints[1].y > thePoints[0].y) {
index_1 = 0;
index_4 = 1;
} else {
index_1 = 1;
index_4 = 0;
}
if (thePoints[3].y > thePoints[2].y) {
index_2 = 2;
index_3 = 3;
} else {
index_2 = 3;
index_3 = 2;
}
box.push(thePoints[index_1]);
box.push(thePoints[index_2]);
box.push(thePoints[index_3]);
box.push(thePoints[index_4]);
return box;
}
GetScore(contour, fMapMat,contours,i) {
let xmin = 9999;
let ymin = 9999;
let brect = cv.boundingRect(contour);
xmin = brect.x;
ymin = brect.y;
let roiWidth = brect.width;
let roiHeight = brect.height;
let rect = new cv.Rect(xmin, ymin, roiWidth, roiHeight);
let roiBitmap = fMapMat.roi(rect);
let mask2 = new cv.Mat.zeros(fMapMat.rows, fMapMat.cols, cv.CV_8U);
cv.drawContours(mask2, contours, i, new cv.Scalar(255, 255, 255), -1);
let mask = mask2.roi(rect);
mask2.delete();
try {
return cv.mean(roiBitmap, mask)[0];
} catch (error) {
vvp.delete();
roiBitmap.delete();
mask.delete();
}
}
GetTextBoxes(data, rows, cols, s, boxScoreThresh, boxThresh, unClipRatio) {
let minArea = 3.0;
let rsBoxes = [];
let outputData = data;
let fMapMat = cv.matFromArray(rows, cols, cv.CV_32F, outputData);
let norfMapMat2 = new cv.Mat();
let norfMapMat = new cv.Mat();
cv.threshold(fMapMat, norfMapMat2, boxThresh, 255, cv.THRESH_BINARY)
norfMapMat2.convertTo(norfMapMat, cv.CV_8U)
norfMapMat2.delete();
let hierarchy = new cv.Mat();
let contours = new cv.MatVector();
cv.findContours(
norfMapMat,
contours,
hierarchy,
cv.RETR_LIST,
cv.CHAIN_APPROX_SIMPLE
);
norfMapMat.delete();
hierarchy.delete();
for (let i = 0; i < contours.size(); i++) {
let minEdgeSize = 0;
let box = this.GetMiniBox(contours.get(i), minEdgeSize);
let rrect = cv.minAreaRect(contours.get(i));
minEdgeSize = Math.min(rrect.size.width, rrect.size.height);
if (minEdgeSize < minArea) {
continue;
}
let score = this.GetScore(contours.get(i), fMapMat,contours,i);
if (score < boxScoreThresh) {
continue;
}
let newBox = this.Unclip(box, unClipRatio);
if (newBox == null) {
continue;
}
let minBox = this.GetMiniBox(newBox, minEdgeSize);
rrect = cv.minAreaRect(newBox);
minEdgeSize = Math.min(rrect.size.width, rrect.size.height);
if (minEdgeSize < minArea + 2) {
continue;
}
let finalPoints = [];
for (let item of minBox) {
let x = parseInt(item.x / s.scaleWidth);
let ptx = Math.min(Math.max(x, 0), s.srcWidth);
let y = parseInt(item.y / s.scaleHeight);
let pty = Math.min(Math.max(y, 0), s.srcHeight);
let dstPt = new cv.Point(ptx, pty);
finalPoints.push(dstPt);
}
let textBox = new TextBox();
textBox.Score = score;
textBox.Points = finalPoints;
rsBoxes.push(textBox);
}
contours.delete();
fMapMat.delete();
rsBoxes.reverse();
return rsBoxes;
}
}