您的位置:首页 > 编程语言 > Python开发

[置顶] 【python 百度文字识别】通用文字识别(高精度版)

2018-03-09 17:20 621 查看
效果展示:

效果非常好~~~~









创建应用

首先你需要登录百度AI,选择文字识别,创建一个应用,会生成 应用名称、AppID、API Key、Secret Key 这些东西,下面我们代码是需要用到API_Key 和 Secret_Key 生成access_token。

python代码:

# encoding: utf-8
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
time1 = time.time()
import urllib, urllib2, base64
import json
import re

def get_token(API_Key,Secret_Key):
# 获取access_token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_Key+'&client_secret='+Secret_Key
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
content_json=json.loads(content)
access_token=content_json['access_token']
return access_token

def recognition_word_high(filepath,filename,access_token):
url='https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + access_token
# 二进制方式打开图文件
f = open(filepath + filename, 'rb')  # 二进制方式打开图文件
# 参数image:图像base64编码
img = base64.b64encode(f.read())
params = {"image": img}
params = urllib.urlencode(params)
request = urllib2.Request(url, params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
content = response.read()
if (content):
# print(content)
world=re.findall('"words": "(.*?)"}',str(content),re.S)
for each in world:
print each

if __name__ == '__main__':
API_Key = "****************************"
Secret_Key = "*****************************"
filepath = "E:/ID/"
filename="59.jpg"
access_token=get_token(API_Key,Secret_Key)
recognition_word_high=recognition_word_high(filepath,filename,access_token)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: