最近学习了python调用al的ocr识别,跟大家分享下
import os
import yaml
import base64
import json
import urllib.request
##将图片生成符合要求的二进制数据
def img(image_path):
with open(image_path, 'rb') as f: # 以二进制读取本地图片
data = f.read()
encodestr = str(base64.b64encode(data),'utf-8') # base64编码图片
return encodestr
def ocl():
# 获取当前文件路径
filePath = os.path.dirname(__file__)
# 获取当前文件的
fileNamePath = os.path.split(os.path.realpath(__file__))[0]
# 获取配置文件的路径
yamlPath = os.path.join(fileNamePath, 'OCRrecognition.yml')
filetest = open(yamlPath, 'r', encoding='utf-8')
f = filetest.read()
con = yaml.load(f, Loader=yaml.FullLoader)
list = con.get('aly')
AppCode = list.get("AppCode")
url="http://engcompos.market.alicloudapi.com/ocrservice/engcompos"
# 图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg / png / bmp格式,和url参数只能同时存在一个
# 图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,
#最短边至少15px,最长边最大4096px,支持jpg / png / bmp格式,和img参数只能同时存在一个
# 是否需要识别结果中每一行的置信度,默认不需要。 true:需要 false:不需要
# 是否需要单字识别功能,默认不需要。 true:需要 #false:不需要
#调用上方的方法,生成数据
encodestr=img('C:/Users/Administrator/Desktop/ocr/ceshi8.png')
#将数据放入date,根据al中api要求填写参数
date={
"img":encodestr ,
#"url": "",
"prob": "false",
"charInfo":"true"
}
#将al的到appcode放入hand
hand = {
'Authorization':"APPCODE "+ AppCode,
'Content-Type': 'application/json; charset=UTF-8'
}
#将参数序列化,不进行序列化会报错,但不知道为啥会报错
params = json.dumps(date).encode(encoding='UTF8')
#执行请求
req = urllib.request.Request(url, params, hand)
r = urllib.request.urlopen(req)
#打开
html = r.read()
# r.close();
#进行json化
jos = json.loads(html)
print(jos,type(jos))
#后续代码是根据一个栗子写的小处理
date1=jos["prism_wordsInfo"]
x = ""
for c in range(len(date1)):
# print(date1[c]['charInfo'])
for b in range(len(date1[c]['charInfo'])):
# print(date1[c]['charInfo'][b]['word'])
d = ((date1[c]['charInfo'])[b]['word'])
x = x + d
x = x + " /n "
# print(str(x))
result = jos['content'] # 就可以按key取值了
print(str(result))
print('识别的结果:',str(x))
##date1=jos["prism_wordsInfo"]得到的数据
def da():
date1 = [
{
'word': '().', 'pos': [{
'x': 14, 'y': 21}, {
'x': 165, 'y': 23}, {
'x': 165, 'y': 40}, {
'x': 14, 'y': 39}],
'charInfo': [{
'word': '下', 'x': 17, 'y': 22, 'w': 1, 'h': 15},
{
'word': '列', 'x': 14, 'y': 22, 'w': 21, 'h': 15},
{
'word': '说', 'x': 42, 'y': 22, 'w': 16, 'h': 15},
{
'word': '去', 'x': 62, 'y': 22, 'w': 7, 'h': 15},
{
'word': '正', 'x': 82, 'y': 22, 'w': 16, 'h': 15},
{
'word': '确', 'x': 97, 'y': 22, 'w': 1, 'h': 15},
{
'word': '的', 'x': 111, 'y': 23, 'w': 12, 'h': 15},
{
'word': '是', 'x': 126, 'y': 23, 'w': 7, 'h': 15},
{
'word': '(', 'x': 137, 'y': 23</