Python调用腾讯优图OCR通用API实现文字识别

2 篇文章 0 订阅

API地址:https://ai.qq.com/doc/ocrgeneralocr.shtml

腾讯优图的API比较复杂的就是生成签名,不过不知道腾讯的服务器出什么问题了,调用的时候一直提示504,演示页面也是无法识别图片了。

# !/user/bin/env Python3
# -*- coding:utf-8 -*-

"""
file:qq_api.py
create time:2019/4/12 15:14
author:Loong Xu
desc: 调用腾讯OCRapi实现文本识别
"""
import base64, hashlib, json, cv2, random, string, time
from urllib import parse, request


def GetAccessToken(formdata, app_key):
    '''
    获取签名
    :param formdata:请求参数键值对
    :param app_key:应用秘钥
    :return:返回接口调用签名
    '''
    dic = sorted(formdata.items(), key=lambda d: d[0])
    sign = parse.urlencode(dic) + '&app_key=' + app_key
    m = hashlib.md5()
    m.update(sign.encode('utf8'))
    return m.hexdigest().upper()


def RecogniseGeneral(app_id, time_stamp, nonce_str, image, app_key):
    '''
    腾讯OCR通用接口
    :param app_id:应用标识,正整数
    :param time_stamp:请求时间戳(单位秒),正整数
    :param nonce_str: 随机字符串,非空且长度上限32字节
    :param image:原始图片的base64编码
    :return:
    '''
    host = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr'
    formdata = {'app_id': app_id, 'time_stamp': time_stamp, 'nonce_str': nonce_str, 'image': image}
    app_key = app_key
    sign = GetAccessToken(formdata=formdata, app_key=app_key)
    formdata['sign'] = sign
    req = request.Request(method='POST', url=host, data=parse.urlencode(formdata).encode('utf8'))
    response = request.urlopen(req)
    if (response.status == 200):
        json_str = response.read().decode()
        print(json_str)
        jobj = json.loads(json_str)
        datas = jobj['data']['item_list']
        recognise = {}
        for obj in datas:
            recognise[obj['itemstring']] = obj
        return recognise


def Recognise(img_path):
    with open(file=img_path, mode='rb') as file:
        base64_data = base64.b64encode(file.read())
    nonce = ''.join(random.sample(string.digits + string.ascii_letters, 32))
    stamp = int(time.time())
    recognise = RecogniseGeneral(app_id=app_id, time_stamp=stamp, nonce_str=nonce, image=base64_data,
                                 app_key=app_key) # 替换成自己的app_id,app_key
    for k, v in recognise.items():
        print(k, v)
    return recognise


img_path = r'C:\Users\Administrator\Desktop\demo\1.jpg'
im = cv2.imread(img_path)
recognise_dic = Recognise(img_path)
for k, value in recognise_dic.items():
    print(k)
    for v in value['itemcoord']:
        cv2.rectangle(im, (v['x'], v['y']), (v['x'] + v['width'], v['y'] + v['height']), (255, 0, 0), 4)

cv2.imshow('img', im)
cv2.waitKey(0)

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值