百度文字识别API调用
官网获取api和secret key
百度AI智能平台通用文字识别:https://ai.baidu.com/tech/ocr/general
创建应用即可,可选择适合的文字识别类型,如标准版、精确版等等,使用文档会有详细说明和调用方法。
调用API接口
分享源代码(标准版,一天可免费调用50000次):
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import os
import json
import requests
import base64
import urllib
import urllib.parse
import urllib.request
# client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = '***'
client_secret = '***'
# 获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key
# 读取图片
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 获取图片信息
def get_content(path):
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 二进制方式打开图片文件
f = open(path, 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
access_token = get_token()
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
return response.json()
rootdir=r'D:\book' #遍历该目录下的所有图片
list = os.listdir(rootdir)
words=[]
for i in range(0,len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path):
content=get_content(path)
if content:
words_result = content['words_result']
for w in words_result:
word = "".join(map(str, w['words'])).strip()
words.append(word)
print(path)
output = "".join(map(str, words)).strip() #将识别内容合成一个字符串
f=open('output.txt','w',encoding='utf8')
f.write(output)