阿里云OCR识别服务python3调用及批量测试

#!/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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值