找回密码
 立即注册

QQ登录

只需一步,快速开始

腾讯云健康码、行程卡OCR 接口封装 利用前端事件解决防疫资料信息收集

我是社区第236184位番薯,欢迎点我头像关注我哦~

准备:1、购买腾讯云服务器(腾讯云轻量服务器 centOS),这里我选择的是已经部署了宝塔面板的。2、在文字识别控制台开通对应服务。3、在API Explorer查看健康码OCR API的示例

以下为腾讯云给到的 健康码 Python SDK

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
try:
    cred = credential.Credential("SecretId", "SecretKey")
    httpProfile = HttpProfile()
    httpProfile.endpoint = "ocr.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = ocr_client.OcrClient(cred, "", clientProfile)

    req = models.RecognizeTravelCardOCRRequest()
    params = {

    }
    req.from_json_string(json.dumps(params))

    resp = client.RecognizeTravelCardOCR(req)
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(err)

给到的代码不需要做太多更改,直接贴出改好后的(实际项目中用到了海外护照的OCR接口,海外护照的传入只能用base64,所以此处传入图片也用的是base64的 别问,问就是懒.....)。

import json
import base64
import requests
from flask import Flask
from flask import request
from flask import render_template
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models

app = Flask(__name__)
cred = credential.Credential("  ", " ")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, " ", clientProfile)

@app.route('/RecognizeTravelCardOCR', methods=['POST'])
def RecognizeTravelCardOCR():

    try:
        r = requests.get(json.loads(request.get_data(as_text=True))['image_url'])
        base64_str = base64.b64encode(r.content).decode()
        req = models.RecognizeTravelCardOCRRequest()
        params = {
            "ImageBase64": base64_str
        }
        req.from_json_string(json.dumps(params))
        resp = client.RecognizeTravelCardOCR(req)
        print(resp.to_json_string())
        return resp.to_json_string()
    except TencentCloudSDKException as err:
        print(err)
        return None

@app.route('/RecognizeHealthCodeOCR', methods=['POST'])
def RecognizeHealthCodeOCR():

    try:
        r = requests.get(json.loads(request.get_data(as_text=True))['image_url'])
        base64_str = base64.b64encode(r.content).decode()
        req = models.RecognizeHealthCodeOCRRequest()
        params = {
            "ImageBase64": base64_str
        }
        req.from_json_string(json.dumps(params))
        resp = client.RecognizeHealthCodeOCR(req)
        print(resp.to_json_string())
        return resp.to_json_string()
    except TencentCloudSDKException as err:
        print(err)
        return None

@app.route('/QrcodeOCR', methods=['POST'])
def QrcodeOCR():

    try:
        r = requests.get(json.loads(request.get_data(as_text=True))['image_url'])
        base64_str = base64.b64encode(r.content).decode()
        req = models.QrcodeOCRRequest()
        params = {
            "ImageBase64": base64_str
        }
        req.from_json_string(json.dumps(params))
        resp = client.QrcodeOCR(req)
        print(resp.to_json_string())
        return resp.to_json_string()
    except TencentCloudSDKException as err:
        print(err)
        return None
        
if __name__ == '__main__':

    app.run(debug=True, host="0.0.0.0", port=3400)

登录宝塔面板,将接口文档上传到服务器,在软件商店搜索“Python项目管理”安装,点击“添加项目”配置好文件路径、Python版本、端口号及配置开机启动。

启动项目如有报错,可以在日志文档查看报错内容。

实际项目中外部提交的都是粤康码,粤康码的二维码识别后可以返回部分脱敏的明文字段,为了防止有人更改截图信息,利用二维码识别的API,将健康码OCR返回的信息与健康码二维码识别的内容进行对比,可以在一定程度上防止提交伪造的健康码

配置到简道云表单后提交的效果:

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

0回帖数 1关注人数 941浏览人数
最后回复于:2022-7-20 09:37

Array ( [id] => 15 [code] => 4301 [product] => [title] => 帖子详情页右侧banner [demand] => 334px * 410px [img_url] => /uploads/20220804/a02aff02bb7da7d7c860aeaadac3b890.jpg [click_url] => https://bbs.fanruan.com/thread-141299-1-1.html [status] => 1 [end_time] => 1661308397 [username] => Joye [edit_time] => 1659580407 )

联系社区管理员|联系帆软|《帆软社区协议》|手机版|帆软社区|Copyright © 帆软软件有限公司 ( 苏ICP备18065767号-7 )

GMT+8, 2022-8-14 13:37 , Processed in 0.137362 second(s), 46 queries , Gzip On.

返回顶部 返回列表