win7 python3.6 调用百度AI进行 图片文字识别 文字识别
1、安装 baidu_ai包 L在python3环境下,pip install baidu_ai/2、创建 文字识别 应用,获取密码 APP_ID API_KEY SECRET_KEY。https://console.bce.baidu.com/ai/?_=1554088705892&fromai=1#/ai/ocr/overview/index
# -*- coding: UTF-8 -*-
from aip import AipOcr
import json
import pandas as pd
# 定义常量 换为您自己的 应用密码
APP_ID = '您自己的 应用密码'
API_KEY = '您自己的 应用密码'
SECRET_KEY = '您自己的 应用密码'
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 读取图片
# filePath = "WechatIMG1.jpeg"
filePath = "20190401.jpeg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定义参数变量
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
# 调用通用文字识别接口
result = aipOcr.basicGeneral(get_file_content(filePath), options)
data = pd.DataFrame(result).T
print (data.loc['words_result'], '*---通用文字识别----*')
# print(json.dumps(result).decode("unicode-escape"))
# 调用通用文字识别接口 高精度版
result = aipOcr.basicAccurate(get_file_content(filePath), options)
data = pd.DataFrame(result).T
print (data.loc['words_result'], '*---通用文字识别 高精度----*')
# print(json.dumps(result).decode("unicode-escape"))
# 调用通用文字识别接口 含位置信息版
result = aipOcr.general(get_file_content(filePath), options)
data = pd.DataFrame(result).T
# print (data.loc['words_result'], '*---通用文字识别 含位置信息版----*')
print ( result['words_result'], '*---通用文字识别 含位置信息 左右----*')
# print(json.dumps(result).decode("unicode-escape"))
就可以了。
4、结果: 不同的 精度的 图片文字识别 得到的结果有一点不同。
https://ai.baidu.com/docs#/OCR-Python-SDK/top
代码打包下: