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

SDK使用说明

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

调试LivenessFaceVerify接口

前提条件

  • 需要使用的Python版本在3.0版本及以上。
  • 已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景

引入依赖包

pip install alibabacloud_tea_openapi==0.2.5

安装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


class LivenessFaceVerify:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        config = open_api_models.Config(
            # 您的AccessKey ID。
            access_key_id='您的AccessKey ID',
            # 您的AccessKey Secret。
            access_key_secret='您的AccessKey Secret',
            # 设置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()