阿里云OCR识别服务python3调用及批量测试
#!/usr/bin/env python# -*- coding: utf-8 -*-import sys,osimport base64import timeimport jsonimport urllib.parseimport urllib.requestfrom com.aliyun.api.gateway.sdk import clientfrom com.al...
·
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,os
import base64
import time
import json
import urllib.parse
import urllib.request
from com.aliyun.api.gateway.sdk import client
from com.aliyun.api.gateway.sdk.http import request
from com.aliyun.api.gateway.sdk.common import constant
import traceback
import urllib
import base64
import shutil
IMG_path = 'root_image_path/'
FILE_TEXT = 'listimagename.txt'
IMG_R_SAVE_path = 'result_img_save_path/'
def get_img_base64(img_file):
with open(img_file, 'rb') as infile:
s = infile.read()
return base64.b64encode(s)
def predict(url, appcode, img_base64, kv_config, old_format):
if not old_format:
param = {}
# param['image'] = img_base64
param['img'] = str(img_base64,encoding="utf-8")
if kv_config is not None:
param['configure'] = json.dumps(kv_config)
body = json.dumps(param).encode('utf-8')
else:
param = {}
pic = {}
pic['dataType'] = 50
pic['dataValue'] = str(img_base64,encoding="utf-8")
param['image'] = pic
if kv_config is not None:
conf = {}
conf['dataType'] = 50
conf['dataValue'] = json.dumps(kv_config)
param['configure'] = conf
inputs = { "inputs" : [param]}
body = json.dumps(inputs).encode('utf-8')
headers = {'Authorization' : 'APPCODE %s' % appcode}
# request = urllib2.Request(url = url, headers = headers, data = body)
request = urllib.request.Request(url = url, headers = headers, data = body)
try:
# response = urllib2.urlopen(request, timeout = 10)
response = urllib.request.urlopen(request, timeout = 10)
return response.code, response.headers, response.read()
# except urllib2.HTTPError as e:
except urllib.error.HTTPError as e:
return e.code, e.headers, e.read()
def readTxt(txtpath):
'''
按行读取txt文件
'''
filelist = []
with open(txtpath, 'r') as f:
for line in f.readlines():
filelist.append(line.strip())
return filelist
def get_word_contentAliyun(jsonresult):
'''
获取OCR通用识别内容
'''
contentstr = ""
codestr = jsonresult['success']
if codestr == True:
print (1)
if 'ret' in jsonresult:
blockarray = jsonresult['ret']
else:
return ""
if len(blockarray) > 0:
for item in blockarray:
contentone = item['word']
contentstr = contentstr + contentone
contentstr = contentone
print (contentstr)
return contentstr
def get_word_contentAliyungaojing(jsonresult):
'''
获取OCR高精识别内容
'''
contentstr = ""
if 'prism_wordsInfo' in jsonresult:
blockarray = jsonresult['prism_wordsInfo']
else:
return ""
if len(blockarray) > 0:
for item in blockarray:
contentone = item['word']
contentstr = contentstr + contentone
else:
return ""
print (contentstr)
return contentstr
def demo():
#general appcode
# appcode = 'your appcode'
# url = 'https://tysbgpu.market.alicloudapi.com/api/predict/ocr_general'
# gaojing appcode
appcode = 'your appcode'
url = 'https://ocrapi-advanced.taobao.com/ocrservice/advanced'
#如果输入带有inputs, 设置为True,否则设为False
is_old_format = False
# config = {'side':'face'}
#如果没有configure字段,config设为None
config = None
num_right =0
# listimg = os.listdir(IMG_path)
listimg = readTxt(FILE_TEXT)
num_cnt = 0
for item in listimg:
num_cnt = num_cnt + 1
print(num_cnt,' filename: ',item)
# if num_cnt < 600:
# continue
if item[-3:] != 'png':
continue
s_label_p = item.split('_')
imglabel=s_label_p[-1]
imglabel = imglabel[:-4]
filefullpath = os.path.join(IMG_path,item)
if not os.path.exists(filefullpath):
continue
img_file = filefullpath
img_base64data = get_img_base64(img_file)
stat, header, content = predict( url, appcode, img_base64data, config, is_old_format)
if stat != 200:
print ('Http status code: ', stat)
print ('Error msg in header: ', header['x-ca-error-message'] if 'x-ca-error-message' in header else '')
print ('Error msg in body: ', content)
exit()
if is_old_format:
result_str = json.loads(content)['outputs'][0]['outputValue']['dataValue']
else:
result_str = content
result_str = str(result_str,encoding="utf-8")
print (result_str)
jsonresult = json.loads(result_str)
# if jsonresult['success'] == True :
if jsonresult['prism_wnum']:
str_result = get_word_contentAliyungaojing(jsonresult)
if imglabel == str_result :
num_right = num_right +1
print("rightnum :",num_right)
save_fullname = item[:-4]
str_result = str_result.replace('/','')
tempstr = [save_fullname,'_ali_',str_result,'.png']
save_fullname = ''.join(tempstr)
save_fullname = os.path.join(IMG_R_SAVE_path,save_fullname)
shutil.copyfile(filefullpath,save_fullname)
if __name__ == '__main__':
demo()
更多推荐
已为社区贡献1条内容
所有评论(0)