Python SDK调用示例

本文介绍人脸静默活体检测API的Python SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。您可以单击以下接口链接进行调试:

调试LivenessFaceVerify接口

前提条件

  • 需要使用的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

调用示例

LivenessFaceVerify接口调用示例

关于参数的具体描述,请参见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工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.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地址,公网可访问。
            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)
        # 创建RuntimeObject实例并设置运行参数。
        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()