-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencvOcr.py
493 lines (447 loc) · 10 KB
/
opencvOcr.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2021/10/22 上午6:49
# @Author : Galonestar
# @Version : V 1.0
# @File : opencvOcr.py
# @desc :
# region Constructor
# region Constructor
const
double
Thresh = 80;
const
double
ThresholdMaxVal = 255;
const
int
_minHeight = 35;
bool
_isDebug = false;
CvKNearest
_cvKNearest = null;
OpencvOcrConfig
_config = new
OpencvOcrConfig()
{ZoomLevel = 2, ErodeLevel = 3};
# endregion
// / < summary >
// / 构造函数
// / < / summary >
// / < param
name = "path" > 训练库完整路径 < / param >
// / < param
name = "opencvOcrConfig" > OCR相关配置信息 < / param >
public
OpencvOcr(string
path, OpencvOcrConfig
opencvOcrConfig = null)
{
if (string.IsNullOrEmpty(path))
throw
new
ArgumentNullException("path is not null");
if (opencvOcrConfig != null)
_config = opencvOcrConfig;
this.LoadKnearest(path);
}
// / < summary >
// / 加载Knn
训练库模型
// / < / summary >
// / < param
name = "dataPathFile" > < / param >
// / < returns > < / returns >
private
CvKNearest
LoadKnearest(string
dataPathFile)
{
if (_cvKNearest == null)
{
using(var
fs = new
FileStorage(dataPathFile, FileStorageMode.Read))
{
var
samples = fs["samples"].ReadMat();
var
responses = fs["responses"].ReadMat();
this._cvKNearest = new
CvKNearest();
this._cvKNearest.Train(samples, responses);
}
}
return _cvKNearest;
}
// / < summary >
// / OCR
识别, 仅仅只能识别单行数字
// / < / summary >
// / < param
name = "kNearest" > 训练库 < / param >
// / < param
name = "path" > 要识别的图片路径 < / param >
public
override
string
GetText(Mat
src, bool
isDebug = false)
{
this._isDebug = isDebug;
# region 图片处理
var
respMat = MatProcessing(src, isDebug);
if (respMat == null)
return "";
# endregion
# region 查找轮廓
var
sortRect = FindContours(respMat.FindContoursMat);
# endregion
return GetText(sortRect, respMat.ResourcMat, respMat.RoiResultMat);
}
// / < summary >
// / 查找轮廓
// / < / summary >
// / < param
name = "src" > < / param >
// / < returns > < / returns >
private
List < Rect > FindContours(Mat
src)
{
try
{
# region 查找轮廓
Point[][]
contours;
HierarchyIndex[]
hierarchyIndexes;
Cv2.FindContours(
src,
out
contours,
out
hierarchyIndexes,
mode: OpenCvSharp.ContourRetrieval.External,
method: OpenCvSharp.ContourChain.ApproxSimple);
if (contours.Length == 0)
throw new NotSupportedException("Couldn't find any object in the image.");
# endregion
# region 单行排序(目前仅仅支持单行文字,多行文字顺序可能不对,按照x坐标进行排序)
var sortRect = GetSortRect(contours, hierarchyIndexes);
sortRect = sortRect.OrderBy(item = > item.X).ToList();
# endregion
return sortRect;
}
catch
{}
return null;
}
// / < summary >
// / 获得切割后的数量列表
// / < / summary >
// / < param
name = "contours" > < / param >
// / < param
name = "hierarchyIndex" > < / param >
// / < returns > < / returns >
private
List < Rect > GetSortRect(Point[][]
contours, HierarchyIndex[]
hierarchyIndex)
{
var
sortRect = new
List < Rect > ();
var
_contourIndex = 0;
while ((_contourIndex >= 0))
{
var
contour = contours[_contourIndex];
var
boundingRect = Cv2.BoundingRect(contour); // Find
bounding
rect
for each contour
sortRect.Add(boundingRect);
_contourIndex = hierarchyIndex[_contourIndex].Next;
}
return sortRect;
}
// / < summary >
// / 是否放大
// / < / summary >
// / < param
name = "src" > < / param >
// / < returns > < / returns >
private
bool
IsZoom(Mat
src)
{
if (src.Height <= _minHeight)
return true;
return false;
}
private
List < EnumMatAlgorithmType > GetAlgoritmList(Mat
src)
{
var
result = new
List < EnumMatAlgorithmType > ();
var
algorithm = this._config.Algorithm;
# region 自定义的算法
try
{
if (algorithm.Contains("|"))
{
result = algorithm.Split('|').ToList()
.Select(item= > (EnumMatAlgorithmType)
Convert.ToInt32(item))
.ToList();
if (!IsZoom(src))
result.Remove(EnumMatAlgorithmType.Zoom);
return result;
}
}
catch
{}
# endregion
# region 默认算法
if (IsZoom(src))
{
result.Add(EnumMatAlgorithmType.Zoom);
}
if (this._config.ThresholdType == ThresholdType.Binary)
{
// result.Add(EnumMatAlgorithmType.Blur);
result.Add(EnumMatAlgorithmType.Gray);
result.Add(EnumMatAlgorithmType.Thresh);
if (this._config.DilateLevel > 0)
result.Add(EnumMatAlgorithmType.Dilate);
result.Add(EnumMatAlgorithmType.Erode);
return result;
}
// result.Add(EnumMatAlgorithmType.Blur);
result.Add(EnumMatAlgorithmType.Gray);
result.Add(EnumMatAlgorithmType.Thresh);
if (this._config.DilateLevel > 0)
result.Add(EnumMatAlgorithmType.Dilate);
result.Add(EnumMatAlgorithmType.Erode);
return result;
# endregion
}
// / < summary >
// / 对查找的轮廓数据进行训练模型匹配,这里使用的是KNN
匹配算法
// / < / summary >
private
string
GetText(List < Rect > sortRect, Mat
source, Mat
roiSource)
{
var
response = "";
try
{
if ((sortRect?.Count ?? 0) <= 0)
return response;
var
contourIndex = 0;
using(var
dst = new
Mat(source.Rows, source.Cols, MatType.CV_8UC3, Scalar.All(0)))
{
sortRect.ForEach(boundingRect= >
{
try
{
# region 绘制矩形
if (this._isDebug)
{
Cv2.Rectangle(source, new
Point(boundingRect.X, boundingRect.Y),
new
Point(boundingRect.X + boundingRect.Width, boundingRect.Y + boundingRect.Height),
new
Scalar(0, 0, 255), 1);
Cv2.Rectangle(roiSource, new
Point(boundingRect.X, boundingRect.Y),
new
Point(boundingRect.X + boundingRect.Width, boundingRect.Y + boundingRect.Height),
new
Scalar(0, 0, 255), 1);
}
# endregion
# region 单个ROI
var
roi = roiSource.GetROI(boundingRect); // Crop
the
image
roi = roi.Compress();
var
result = roi.ConvertFloat();
# endregion
# region KNN 匹配
var
results = new
Mat();
var
neighborResponses = new
Mat();
var
dists = new
Mat();
var
detectedClass = (int)
this._cvKNearest.FindNearest(result, 1, results, neighborResponses, dists);
var
resultText = detectedClass.ToString(CultureInfo.InvariantCulture);
# endregion
# region 匹配
var
isDraw = false;
if (detectedClass >= 0)
{
response += detectedClass.ToString();
isDraw = true;
}
if (detectedClass == -1 & & !response.Contains("."))
{
response += ".";
resultText = ".";
isDraw = true;
}
# endregion
# region 绘制及输出切割信息库
try
{
// if (this._isDebug)
// {
Write(contourIndex, detectedClass, roi);
//}
}
catch
{}
if (this._isDebug & & isDraw)
{
Cv2.PutText(dst, resultText, new
Point(boundingRect.X, boundingRect.Y + boundingRect.Height), 0, 1, new
Scalar(0, 255, 0), 2);
}
# endregion
result?.Dispose();
results?.Dispose();
neighborResponses?.Dispose();
dists?.Dispose();
contourIndex + +;
}
catch(Exception
ex)
{
TextHelper.Error("GetText ex", ex);
}
});
# region 调试模式显示过程
source.IsDebugShow("Segmented Source", this._isDebug);
dst.IsDebugShow("Detected", this._isDebug);
dst.IsDebugWaitKey(this._isDebug);
dst.IsDebugImWrite("dest.jpg", this._isDebug);
# endregion
}
}
catch
{
throw;
}
finally
{
source?.Dispose();
roiSource?.Dispose();
}
return response;
}
// / < summary >
// / 图片处理算法
// / < / summary >
// / < param
name = "src" > < / param >
// / < param
name = "isDebug" > < / param >
// / < returns > < / returns >
public
ImageProcessModel
MatProcessing(Mat
src, bool
isDebug = false)
{
src.IsDebugShow("原图", isDebug);
var
list = GetAlgoritmList(src);
var
resultMat = new
Mat();
src.CopyTo(resultMat);
var
isZoom = IsZoom(src);
list?.ForEach(item= >
{
switch(item)
{
case
EnumMatAlgorithmType.Dilate: \
resultMat = resultMat.ToDilate(Convert.ToInt32(this._config.DilateLevel));
resultMat.IsDebugShow(EnumMatAlgorithmType.Dilate.GetDescription(), isDebug);
break;
case
EnumMatAlgorithmType.Erode:
var
eroderLevel = isZoom ? this._config.ErodeLevel * this._config.ZoomLevel: this._config.ErodeLevel;
resultMat = resultMat.ToErode(eroderLevel);
resultMat.IsDebugShow(EnumMatAlgorithmType.Erode.GetDescription(), isDebug);
break;
case
EnumMatAlgorithmType.Gray:
resultMat = resultMat.ToGrey();
resultMat.IsDebugShow(EnumMatAlgorithmType.Gray.GetDescription(), isDebug);
break;
case
EnumMatAlgorithmType.Thresh:
var
thresholdValue = this._config.ThresholdValue <= 0 ? resultMat.GetMeanThreshold(): this._config.ThresholdValue;
resultMat = resultMat.ToThreshold(thresholdValue, thresholdType: this._config.ThresholdType);
resultMat.IsDebugShow(EnumMatAlgorithmType.Thresh.GetDescription(), isDebug);
break;
case
EnumMatAlgorithmType.Zoom:
resultMat = resultMat.ToZoom(this._config.ZoomLevel);
src = resultMat;
resultMat.IsDebugShow(EnumMatAlgorithmType.Zoom.GetDescription(), isDebug);
break;
case
EnumMatAlgorithmType.Blur:
resultMat = resultMat.ToBlur();
src = resultMat;
resultMat.IsDebugShow(EnumMatAlgorithmType.Blur.GetDescription(), isDebug);
break;
}
});
var
oldThreshImage = new
Mat();
resultMat.CopyTo(oldThreshImage);
return new
ImageProcessModel()
{
ResourcMat = src,
FindContoursMat = oldThreshImage,
RoiResultMat = resultMat
};
}