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
| import re
| import time
|
| import cv2
| import easyocr
| from cnocr import CnOcr
|
|
| # 图像识别类
| class OcrUtil:
| __ocr = CnOcr()
| reader = easyocr.Reader(['en'], gpu=False)
|
| @classmethod
| def ocr(cls, mat):
| res = cls.__ocr.ocr(mat)
| return res
|
| # 返回(识别内容,位置信息)
| @classmethod
| def ocr_with_key(cls, mat, key):
| start = time.time()
| res = cls.ocr(mat)
| res_final = []
| for r in res:
| text = r["text"]
| if re.match(key, text):
| ps = r["position"]
| res_final.append((text, [(int(ps[0][0]), int(ps[0][1])), (int(ps[1][0]), int(ps[1][1])),
| (int(ps[2][0]), int(ps[2][1])), (int(ps[3][0]), int(ps[3][1]))]))
| print("识别时间", time.time() - start)
| return res_final
|
| @classmethod
| def ocr_num(cls, mat, key):
| start = time.time()
| res = cls.reader.readtext(mat, detail=1, text_threshold=0.6, mag_ratio=1.5)
| res_final = []
| if res:
| for r in res:
| text = r[1]
| if re.match(key, text):
| ps = r[0]
| res_final.append((text, [(int(ps[0][0]), int(ps[0][1])), (int(ps[1][0]), int(ps[1][1])),
| (int(ps[2][0]), int(ps[2][1])), (int(ps[3][0]), int(ps[3][1]))]))
| print("数字识别时间", time.time() - start)
| return res_final
|
|
| if __name__ == "__main__":
| result = OcrUtil.ocr_num("D:/test1.png", "000977")
| print(result)
|
|