准备: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返回的信息与健康码二维码识别的内容进行对比,可以在一定程度上防止提交伪造的健康码
配置到简道云表单后提交的效果:
|