Python SDK調(diào)用示例
更新時間:
本文介紹人臉靜默活體檢測API的Python SDK調(diào)用示例。
SDK使用說明
如果需要其他語言的SDK示例代碼,您可以通過OpenAPI 開發(fā)者門戶在線調(diào)試工具調(diào)試API接口,該工具會自動生成相應API的SDK調(diào)用示例代碼。您可以單擊以下接口鏈接進行調(diào)試:
前提條件
需要使用的Python為3.0及以上版本。
已添加認證場景,并獲取對應的場景ID。具體操作,請參見添加認證場景。
引入依賴包
pip install alibabacloud_tea_openapi==0.2.5
pip install alibabacloud_credentials==0.3.2
安裝SDK
關于Python SDK源代碼,請參見Python SDK包。
pip install alibabacloud_cloudauth20190307==2.0.3
調(diào)用示例
LivenessFaceVerify接口調(diào)用示例
關于參數(shù)的具體描述,請參見LivenessFaceVerify。
from alibabacloud_cloudauth20190307.client import Client as CloudauthClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20190307 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models
from alibabacloud_credentials.client import Client as CredClient
class LivenessFaceVerify:
def __init__(self):
pass
@staticmethod
def create_client(
endpoint: str,
) -> CloudauthClient:
# 阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
# 強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
# 本示例通過阿里云Credentials工具從環(huán)境變量中讀取AccessKey,來實現(xiàn)API訪問的身份驗證。如何配置環(huán)境變量,請參見 http://bestwisewords.com/zh/sdk/developer-reference/configure-credentials。
cred=CredClient()
config = open_api_models.Config(
credential=cred,
# 設置HTTP代理。
# http_proxy='http://xx.xx.xx.xx:xxxx',
# 設置HTTPS代理。
# https_proxy='https://username:password@xxx.xxx.xxx.xxx:9999',
endpoint=endpoint
)
return CloudauthClient(config)
@staticmethod
def main() -> None:
request = cloudauth_models.LivenessFaceVerifyRequest(
# 請輸入場景ID。
scene_id=0,
outer_order_no='商戶請求的唯一標識',
# 固定值。
product_code='LR_FR_MIN',
model='FRONT_CAMERA_LIVENESS',
# ip='用戶IP地址',
# user_id='用戶ID',
# mobile='130xxxxxxxx',
# device_token='通過設備指紋獲取的deviceToken',
# 請在以下四種方式中選擇其一。
# 1.照片Base64編碼。
# 2.照片OSS Bucket名和文件名。
# 3.照片OSS的URL地址。
# 方式一:照片Base64。
# face_contrast_picture='人臉照片Base64編碼'
# 方式二:照片OSS Bucket名和文件名。
# oss_bucket_name='cn-shanghai-aliyun-cloudauth-xxxxx',
# oss_object_name='verify/xxxxx/xxxxxx.jpeg',
# 方式三:照片OSS的URL地址,公網(wǎng)可訪問。
face_contrast_picture_url='https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg'
)
response = LivenessFaceVerify.liveness_face_verify_auto_route(request)
# 不支持服務自動路由。
# response = LivenessFaceVerify.liveness_face_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
print(response.body.request_id)
print(response.body.code)
print(response.body.message)
print(response.body.result_object.passed)
print(response.body.result_object.sub_code)
print(response.body.result_object.material_info)
print(response.body.result_object.certify_id)
@staticmethod
def liveness_face_verify_auto_route(
request: cloudauth_models.LivenessFaceVerifyRequest,
) -> cloudauth_models.LivenessFaceVerifyResponse:
endpoints = [
'cloudauth.cn-shanghai.aliyuncs.com',
'cloudauth.cn-beijing.aliyuncs.com'
]
for endpoint in endpoints:
try:
response = LivenessFaceVerify.liveness_face_verify(endpoint, request)
if UtilClient.equal_number(500, response.status_code):
continue
if response is not None and response.body is not None:
if UtilClient.equal_string("500", response.body.code):
continue
return response
except Exception as err:
continue
return None
@staticmethod
def liveness_face_verify(
endpoint: str,
request: cloudauth_models.LivenessFaceVerifyRequest,
) -> cloudauth_models.LivenessFaceVerifyResponse:
client = LivenessFaceVerify.create_client(endpoint)
# 創(chuàng)建RuntimeObject實例并設置運行參數(shù)。
runtime = util_models.RuntimeOptions()
runtime.read_timeout = 10000
runtime.connect_timeout = 10000
return client.liveness_face_verify_with_options(request, runtime)
if __name__ == '__main__':
LivenessFaceVerify.main()
文檔內(nèi)容是否對您有幫助?