CDN加速

文字识别

更新时间:2019-12-12 17:26:48
适用产品:定制化接口

接口描述

OCR文字探测模型,返回识别的文字

请求参数

参数名称类型必填描述
filestring用户传上来识别的文字图片。采用base64加密

返回参数

参数名称类型描述
codestring返回调用结果的状态
messagestring调用接口,返回状态码的说明
datalist返回识别到的图片文字内容。

错误码

错误代码(code)描述(message)HTTP状态码语义
30052002post字段没有图片内容200

示例

示例说明
请求示例
复制
#!/usr/bin/env python
# -- coding: utf-8 --


import requests
import sys
import os
import json
import base64
import time
from hashlib import sha256

url = 'http://open.chinanetcenter.com/ocr/v1/text/recognition'
username = 'xxx'
apikey = 'xxx'
__curPath__ = os.path.split(os.path.realpath(__file__))[0]


def getDate():
    import datetime
    GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
    date_gmt = datetime.datetime.utcnow().strftime(GMT_FORMAT)
    print("getDate: " + date_gmt)
    return date_gmt


def sha(date):
    import hmac
    print("sha: " + date)
    signed_apikey = hmac.new(apikey.encode(
        'utf-8'), date.encode('utf-8'), sha256).digest()
    signed_apikey = base64.b64encode(signed_apikey)
    print("sha: " + signed_apikey.decode())
    return signed_apikey


def encode(time):
    print("encode: " + time.decode())
    msg = username + ":" + time.decode()
    result = base64.b64encode(msg.encode('utf-8'))
    print("encode: " + result.decode())
    return result


def getAuth():
    return encode(sha(getDate()))

def getABase64StrFromFile(fname):
    with open(fname, 'rb') as f:
        c = f.read()
        c = base64.b64encode(c)
        return str(c, encoding='utf-8') 

def connect(req, fname='./img/test2.png'):
    auth = getAuth()
    auth = 'Basic ' + auth.decode()
    d = {'file': getABase64StrFromFile(fname)}
    headers = {'Content-Type': 'application/json', 'Accept': "application/json",
               "Date": getDate(), 'Authorization': auth}
    r = requests.post(url, headers=headers, data=json.dumps(d))
    ret = r.text
    print(ret)

if __name__ == '__main__':
    s = requests.session()
    s.keep_alive = False
    connect(1)
返回示例
复制
{"code": "0", "message": "ok", "data": [["图片文字内容"]]]}