步骤一:浏览器中搜索“百度智能云”
注册并登陆
步骤二:领取资源并进行认证
价格:
通用场景文字识别 - 文字识别OCR (baidu.com)
应用地址:
步骤三:找到文本识别ocr模块,并建立应用
步骤四:测试
创建sourceai/baidu/ocr_cases.py文件
前置条件
pip install requests
pip install urllib3==1.26.15
ocr_cases.py文件代码如下:
# encoding:utf-8
import requests
import base64
def baidu_ocr(img_path,isurl = False):
ak = 'XXXXX'
sk = 'XXXXX'
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'
response = requests.get(host)
access_result = response.json()
print(access_result)
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
if isurl:
params = {"url": img_path}
else:
# 二进制方式打开图片文件
f = open(img_path, 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
request_url = request_url + "?access_token=" + str(access_result['access_token'])
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
res = ''
if response:
result = response.json()
print(result)
lines = result['words_result']
for line in lines:
try:
words = line['words']
res = res + words + '<br/>'
except:
pass
return res
if __name__ == '__main__':
baidu_ocr("1.jpg")
从网站找一个带有文字的图片,下载到本地,重命名1.jpg,将1.jpg图片放到sourceai/baidu文件夹中,运行当前程序