f ocr_detect(path):
"""
OCR汉字识别
"""
fp, code = None, None
if not os.path.exists(path): return code
_, file_type = path.rsplit(".",1)
if file_type.lower() not in ALLOW_FILE_TYPE: return code
try:
fp = open(path, 'rb')
con1 = fp.read()
img = Image.open(BytesIO(con1))
code = ocr.classification(img)
except Exception as exc:
print('[ERROR] 识别发生错误:', exc)
finally:
if fp: fp.close()
return code
if __name__ == '__main__':
code = ocr_detect(r'E:\pic\123\test.jpg')
for i in range(8):
code2 = ocr_detect(f'E:\pic/123/{i}.jpg')
if code == code2:
print(f"编码为{i}的是对的")
else:
print("寄了")
具体使用方式参考官方文档即可
更多方法用到后会继续更新~
|