效果展示:

效果非常好~~~~

20190109001.png

20190109002.png

20190109003.png

20190109004.png

创建应用

首先你需要登录百度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)

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐