名片识别信息分类python_python体验名片识别OCR

本文介绍了如何使用Python调用腾讯云和腾讯优图的API进行名片识别OCR。通过示例代码展示了从生成签名到发送请求的完整过程,包括处理URL和本地图片的方式。
摘要由CSDN通过智能技术生成

想使用名片识别OCR,主要调研了腾讯云的智能图像和腾讯优图。

腾讯云

解析出来的汉字直接是unicode,比如u'\u90e8\u95e8\u526f\u603b\u7ecf\u7406',按照腾讯的文档,撸出代码体验:

首先是一堆引入。

import time

import random

import hmac, hashlib

import binascii

import base64

import requests

import os

import json

然后按照文档生成签名(签权),这一步文案写的很不通顺。其中需要替代的地方自行看文档找下,比如appid、secret_id、secret_key。至于bucket,我没看明白有啥用。

appid = 'your app id' #自己填

secret_id = 'your secret id' #自己填

secret_key = 'your secret key' #自己填

bucket= 'seem no use' #自己填

# 还有个https的,我没试

url = 'http://recognition.image.myqcloud.com/ocr/businesscard'

now = int(time.time())

rdm = random.randint(0,9999999999)

expired = now + 2592000 #一个月

plain_text = 'a='+appid+'&b='+bucket+'&k='+secret_id+'&e='+str(expired)+'&t='+str(now)+'&r='+str(rdm)+'&u=0&f='

bin = hmac.new(secret_key.encode(), plain_text.encode(), hashlib.sha1)

s = bin.hexdigest()

s = binascii.unhexlify(s)

s = s + plain_text.encode('ascii')

signature = base64.b64encode(s).rstrip() # 生成签名

print(signature)

接下来分两种情形,如果需要识别的图片是url,比较简单点。设置headers和data,用于requests发送请求,图片url使用了优图的示例。打印出结果即可。

使用图片url

headers = {

'Authorization': signature,

'content-type': 'application/json',

}

data = {

'appid': appid,

'url_list': ['http://yoututest-1251966477.cossh.myqcloud.com/mingpian.jpg']

}

r = requests.post(url, headers=headers, data = json.dumps(data))

ret = r.json()

print(ret)

使用本地图片

因为本人对于'content-type': 'multipart/form-data'这种请求不熟,研究了半天,后来发现其实也简单。

headers = {

'Authorization': signature,

# 不用设置content-type!!!

}

files = {

'appid': appid,

# 可以多张图片,但是必须image开头,路径自己设置好

'image[0]': ( 'image[0].jpeg', open(os.path.abspath('mingpian0.jpeg'), 'rb'), 'image/jpeg', {}),

# 'image[1]': ( 'image[1].jpeg', open(os.path.abspath('mingpian1.jpeg'), 'rb'), 'image/jpeg', {}),

}

r = requests.post(url, headers=headers, files=files)

ret = r.json()

print(ret)

有没有发现,根本没用到bucket,另外也没扣我钱,可能是因为腾讯云是预支消费,下个月扣钱?拭目以待。

腾讯优图

优图的demo确实做得不错,吸引了我,不过识别结果有几个注意点:第一,返回的结果好多,可能是一些原始数据;第二,汉字的结果编码我没搞定,是\x开头的。这里我还是体验一下,参考文档,跟上面腾讯云的差不了多少。

对了,优图demo有python的代码,我这里也是借鉴了简化下,大家都明白,自己不敲一遍,感觉不实在。

首先一堆引入。

# import 跟上面一样吧

签名部分,也类似,不过bucket变成了申请优图的qq号码了,这个字段随便写好像也没事。

appid = 'your app id' # 在优图申请应用后就得到

secret_id = 'your secret id' # 在优图申请应用后就得到

secret_key = 'your secret key' # 在优图申请应用后就得到

userid= 'your qq number'

url = 'http://api.youtu.qq.com/youtu/ocrapi/bcocr'

now = int(time.time())

rdm = random.randint(0,9999999999)

expired = now + 2592000

plain_text = 'u=' + userid + '&a=' + appid + '&k=' + secret_id + '&e=' + str(expired) + '&t=' + str(now) + '&r=' + str(rdm) + '&f='

bin = hmac.new(secret_key.encode(), plain_text.encode(), hashlib.sha1)

s = bin.hexdigest()

s = binascii.unhexlify(s)

s = s + plain_text.encode('ascii')

signature = base64.b64encode(s).rstrip() # 生成签名

print(signature)

也是分两种情况,使用url和使用image,但是优图的image方式其实是转成了base64字符串了,比较简单。

headers = {

'Authorization': signature,

'Content-Type': 'text/json',

}

data = {

'app_id': appid,

'session_id': '',

}

# 下面自行选择字段

data['url'] = 'http://yoututest-1251966477.cossh.myqcloud.com/mingpian.jpg'

data["image"] = base64.b64encode(open(os.path.abspath('mingpian.jpeg'), 'rb').read()).rstrip().decode('utf-8')

r = requests.post(url, headers=headers, data = json.dumps(data))

ret = r.json()

print(ret)

返回的结果我就不贴了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值