腾讯OCR的使用(卡证)

编程入门 行业动态 更新时间:2024-10-05 07:23:17

<a href=https://www.elefans.com/category/jswz/34/1770070.html style=腾讯OCR的使用(卡证)"/>

腾讯OCR的使用(卡证)

腾讯OCR的使用(卡证)

注:以一个身份证正面举例,涉及隐私问题,只贴代码以及代码的讲解

使用腾讯OCR的原因

我作为RPA工程师很清楚大家大多使用的都是百度OCR,但是经过上千张身份证银行卡的识别,事实证明腾讯OCR的分辨情况要比百度OCR高,这也是我花了半天时间琢磨了一下腾讯OCR该怎么使用,以及我在实践中遇到的问题以及改正方式。

首先百度OCR和腾讯OCR都有一个月1000次的免费,所以大家想自己玩玩可以自己去搜索着去玩玩试试看。提示,腾讯OCR和百度OCR都需要绑定微信号并实名认证。

百度OCR提供的API更丰富一些,比如说百度OCR调用接口后返还的数据是“字典”格式,而腾讯OCR返还的数据格式是“字符串”,你在数据处理的时候就有些搞人心态。

废话不多说直接贴代码:我用的是python,用java的小伙伴自己去官网给的代码调试吧

import ast
import base64
import json
from tencentcloudmon import credential
from tencentcloudmon.profile.client_profile import ClientProfile
from tencentcloudmon.profile.http_profile import HttpProfile
from tencentcloudmon.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models#调用的图片需要经过base64编码处理
def path2base64(path):with open(path, "rb")as f:byte_data = f.read()base64_str = base64.b64encode(byte_data).decode("ascii")  #base64return base64_strtry:#注意输入的不是你登录的账号密码,是在登录后解锁打开的一个uid和key,自己找找cred = credential.Credential("输入你自己的uid", "输入你自己的key")httpProfile = HttpProfile()httpProfile.endpoint = "ocr.tencentcloudapi"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)req = models.IDCardOCRRequest()file = "C:\\Users\\tianyi.zhang\\Desktop\\963.jpg"image = path2base64(file)params = {"ImageBase64": image,"CardSide": "FRONT"}req.from_json_string(json.dumps(params))resp = client.IDCardOCR(req)#这部是将字符串格式的数据转换成字典格式resp_info = ast.literal_eval(resp.to_json_string())print(resp.to_json_string())print(resp_info)except TencentCloudSDKException as err:print(err)

再说一下我在实际使用时碰到的一个问题
下列代码是银行卡的识别

import ast
import base64
import json
from tencentcloudmon import credential
from tencentcloudmon.profile.client_profile import ClientProfile
from tencentcloudmon.profile.http_profile import HttpProfile
from tencentcloudmon.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, modelsdef path2base64(path):with open(path, "rb")as f:byte_data = f.read()base64_str = base64.b64encode(byte_data).decode("ascii")  #base64return base64_strtry:cred = credential.Credential("", "")httpProfile = HttpProfile()httpProfile.endpoint = "ocr.tencentcloudapi"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)file = "C:\\Users\\tianyi.zhang\\Desktop\\456.jpg"image = path2base64(file)req = models.BankCardOCRRequest()params = {"ImageBase64": image}req.from_json_string(json.dumps(params))resp = client.BankCardOCR(req)resp_info = ast.literal_eval(resp.to_json_string())print(resp.to_json_string())except TencentCloudSDKException as err:print(err)

然后运行会报这样的错误

Traceback (most recent call last):File "C:/Users/tianyi.zhang/PycharmProjects/数据分析/tencentOCR银行卡.py", line 36, in <module>resp_info = ast.literal_eval(resp.to_json_string())File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 99, in literal_evalreturn _convert(node_or_string)File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 88, in _convertreturn dict(zip(map(_convert, node.keys),File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 98, in _convertreturn _convert_signed_num(node)File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 75, in _convert_signed_numreturn _convert_num(node)File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 66, in _convert_num_raise_malformed_node(node)File "C:\Users\tianyi.zhang\AppData\Local\Programs\Python\Python38\lib\ast.py", line 63, in _raise_malformed_noderaise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x0000026DF1391850>

报错的原因是银行卡获取到的字符串中有null值,在python中没办法直接转换成字典格式
修改方法是将:

resp_info = ast.literal_eval(resp.to_json_string())

改成

resp_info = ast.literal_eval(resp.to_json_string().replace('null','""'))

腾讯OCR的卡证识别就这些,希望对大家有帮助

更多推荐

腾讯OCR的使用(卡证)

本文发布于:2024-03-13 18:07:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:腾讯   OCR   卡证

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!