-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbaidu_api_util.py
More file actions
41 lines (36 loc) · 1.45 KB
/
baidu_api_util.py
File metadata and controls
41 lines (36 loc) · 1.45 KB
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
# coding = utf-8
import yaml
import os
from aip import AipOcr
"""OCR图片文字识别
@author: xuwenyuan
date: 2019-09-05
desc: 根据传入的图片(全路径),识别出图片中的文字,主要用于验证码的识别
"""
class BaiduOCR(object):
def __init__(self):
file_path = os.path.dirname(__file__)
yaml_file = os.path.join(file_path, 'baidu_api_conf.yml')
with open(yaml_file, 'r', encoding='utf-8') as fp:
content = fp.read()
conf = yaml.load(content, Loader=yaml.FullLoader)
# baiduOCRKey
self.app_id = conf['baiduOCR']['app_id']
self.api_key = conf['baiduOCR']['api_key']
self.secret_key = conf['baiduOCR']['secret_key']
self.client = AipOcr(self.app_id, self.api_key, self.secret_key)
def get_img_text(self, img_name):
"""读取图片,并识别图片里面的文本
:param img_name: 图片全路径
:return: 返回文本
{'log_id': 7202748421669539685, 'words_result_num': 1, 'words_result': [{'words': 'FWYP'}]}
"""
with open(img_name, 'rb') as fp:
result_dic = self.client.basicGeneral(fp.read())
result = result_dic['words_result'][0]['words']
result = result.replace(' ', '')
return result
if __name__ == '__main__':
baidu_ocr = BaiduOCR()
img_text = baidu_ocr.get_img_text("C:/Users/64174/Downloads/ValidateImage.jfif")
print(img_text)