随笔 - 268,  文章 - 5,  评论 - 8,  阅读 - 26万
复制代码
# !/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)
复制代码

 

posted on   root-123  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示