今天突然有个需求,就是把提取图片里面的文字,找了python的一些库,除了安装库还要安装其他的识别文字的驱动什么的,感觉太麻烦了,后面找到了百度ai的api,申请个id,就可以开始干了,当然也要安装模块:
pip install baidu-aip
效果杠杠的(python sdk文档 https://ai.baidu.com/docs#/OCR-Python-SDK/top)
from aip import AipOcr
# 百度api接口,读取图片文字
APP_ID = 'XXXXX'
API_KEY = 'XXXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXXX'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(file):
with open(file, 'rb') as fp:
return fp.read()
def img_to_str(image_path):
image = get_file_content(image_path)
result = client.basicGeneral(image)
if 'words_result' in result:
return '\n'.join([w['words'] for w in result['words_result']])
print(img_to_str('1.jpg'))