import re import time import cv2 from cnocr import CnOcr # 图像识别类 class OcrUtil: __ocr = CnOcr() @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