云服务器内容精选

  • 响应参数 状态码:200 表4 响应Body参数 参数 参数类型 描述 face_set_id String 人脸库ID。 调用失败时无此字段。 face_set_name String 人脸库名称。 调用失败时无此字段。 faces Array of FaceSetFace objects 人脸库当中的人脸结构,详见FaceSetFace。 调用失败时无此字段。 表5 FaceSetFace 参数 参数类型 描述 bounding_box BoundingBox object 人脸在图像中的位置。 BoundingBox结构见BoundingBox。 external_fields Object 用户添加的额外字段。 external_image_id String 人脸所在的外部图片ID。 face_id String 人脸ID,由系统内部生成的唯一ID。 表6 BoundingBox 参数 参数类型 描述 width Integer 矩形框宽度。 top_left_y Integer 矩形框左上角纵坐标。 top_left_x Integer 矩形框左上角横坐标。 height Integer 矩形框高度。 状态码: 400 表7 响应Body参数 参数 参数类型 描述 error_code String 调用失败时的错误码,具体请参考错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。
  • 功能介绍 添加人脸到人脸库中。将单张图片中的人脸添加至人脸库中,支持添加最大人脸或所有人脸。 前提条件: 请确保您已开通人脸搜索服务。 约束限制: 只支持识别JPG、PNG、JPEG、BMP格式的图片。 application/json请求的body中,请使用标准Json格式。 Base64编码中请勿使用回车换行。 系统不保存用户图片。 图片大小小于8MB,由于过大图片会导致时延较长,并且图片信息量不大,建议小于1MB。 图片分辨率小于4096*2160,图片中人脸像素大于80*80,建议120*120以上。 为保证识别效果,人脸图片建议要求如下: 光照大于200lux、无反光强光阴影现象。 人脸无遮挡、整体清晰无拖尾抖动等运动模糊。 侧脸不超过30°、俯仰角小于15°、偏转角小于15°、图片中人脸保持竖置正脸。 其他的约束限制信息请参见约束与限制章节。 建议: 由于过大图片对识别算法精度无明显提升,同时会导致时延较长,建议传入图片小于1MB,一般500KB左右足够。 OBS上存储的图片也建议小于1MB。
  • 请求参数 表2 请求Header参数 参数 是否必选 参数类型 描述 X-Auth-Token 是 String 用户Token。 用于获取操作API的权限。获取方法请参见认证鉴权。 Content-Type 是 String 发送的实体的MIME类型,参数值为“application/json”。 Enterprise-Project-Id 否 String 企业项目ID。FRS支持通过企业项目管理(EPS)对不同用户组和用户的资源使用,进行分账,当前仅支持按需计费模式。 获取方法:进入“企业项目管理”页面,单击企业项目名称,在企业项目详情页获取Enterprise-Project-Id(企业项目ID)。 说明: 创建企业项目后,在传参时,有以下三类场景。 携带正确的ID,正常使用FRS服务,账单的企业项目会被分类到企业ID对应的企业项目中。 携带格式正确但不存在的ID,正常使用FRS服务,账单的企业项目会显示对应不存在的企业项目ID。 不携带ID或格式错误ID(包含特殊字符等),正常使用FRS服务,账单的企业项目会被分类到"default"中。 表3 请求Body参数 参数名 参数类型 是否必选 说明 image_url String 与image_file、image_base64三选一 图片的URL路径,目前仅支持华为云上OBS的URL,使用时只需保证FRS有权限读取该OBS桶的数据。开通读取权限的操作请参见服务授权。 image_file File 与image_url、image_base64三选一 本地图片文件,图片不能超过8MB,建议小于1MB。上传文件时,请求格式为multipart。 image_base64 String 与image_file、image_url三选一 图像数据,Base64编码,要求: Base64编码后大小不超过8MB,建议小于1MB。 图片为JPG/JPEG/BMP/PNG格式。 external_image_id String 否 用户指定的图片外部ID,与当前图像绑定。用户没提供,系统会生成一个。 该ID长度范围为1~36位,可以包含字母、数字、中划线或者下划线,不包含其他的特殊字符。 external_fields Object 否 根据用户自定义数据类型,填入相应的数值。 需在创建人脸库时定义external_fields字段,才可以在添加人脸时使用该字段,参考自定义字段。 single boolean 否 是否将图片中的最大人脸添加至人脸库。可选值包括: true:传入的单张图片中如果包含多张人脸,则只将最大人脸添加到人脸库中。 false:默认为false。传入的单张图片中如果包含多张人脸,则将所有人脸添加至人脸库中。
  • 参数说明 表1 结果格式说明表 名称 类型 说明 project_id String 项目ID,获取方法请参见获取项目ID/账号名/AK/SK。 image_url String 图片的URL路径,目前仅支持华为云上OBS的URL,且人脸识别服务有权限读取该OBS桶的数据。开通读取权限的操作请参见服务授权。 image_file File 本地图片文件,图片不能超过8MB,建议小于1MB。上传文件时,请求格式为multipart。 image_base64 String 图像数据,Base64编码,要求: Base64编码后大小不超过8MB,建议。 图片为JPG/JPEG/BMP/PNG格式。 similarity Double 人脸相似度,1表示最大,0表示最小,值越大表示越相似。一般情况下超过0.93即可认为是同一个人。 face_set_name String 人脸库名称,1位到64位之间,可以包含字母、数字、中划线或者下划线,不能包含其他的特殊字符。 face_set_capacity Integer 人脸库最大的容量,填写1万整数倍的数字,例如30000。默认为100000,最大值为100000,可通过创建新的人脸库进行扩容,每个用户可使用10个人脸库,每个人脸库容量为10万个人脸特征。如需扩容单个人脸库规模,请联系华为云客服确认扩容规模与价格。 face_id String 导入人脸时,系统返回的人脸编号,为8个随机生成的大小写字母组成。 external_image_id String 用户指定的图片外部ID,与当前图像绑定。用户没提供,系统会生成一个。该ID长度范围为1~36位,可以包含字母、数字、中划线或者下划线,不包含其他的特殊字符。 external_fields Json 根据用户自定义数据类型,填入相应的数值。创建人脸库时,定义该字段。具体参见自定义字段。 top_n Integer 返回查询到最相似的N张人脸,N默认为10。如果返回前5个,则该变量N的值为5。 取值范围1~1000。 threshold Double 人脸相似度阈值,低于这个阈值则不返回,取值范围[0,1],一般情况下建议取值0.93,默认为0。 offset Integer 从第几条数据读起,默认为0。 limit Integer 读取多少条,默认为5。 video_url String 视频的URL路径,目前仅支持华为云上OBS的URL,且人脸识别服务有权限读取该OBS桶的数据。开通读取权限的操作请参见服务授权。视频要求: 视频Base64编码后大小不超过8MB。 限制视频时长1~15秒。 建议帧率10fps~30fps。 封装格式:mp4、avi、flv、webm、asf、mov。 视频编码格式: h261、h263、h264、hevc、vc1、vp8、vp9、wmv3。 video_file File 本地视频文件。上传文件时,请求格式为multipart。视频要求: 视频文件大小不超过8MB,建议客户端压缩到200KB~2MB。 限制视频时长1~15秒。 建议帧率10fps~30fps。 封装格式:mp4、avi、flv、webm、asf、mov。 视频编码格式: h261、h263、h264、hevc、vc1、vp8、vp9、wmv3。 video_base64 String 视频数据,Base64编码,要求: Base64编码后大小不超过8MB,建议客户端压缩到200KB~2MB。 限制视频时长1~15秒。 建议帧率10fps~30fps。 封装格式:mp4、avi、flv、webm、asf、mov。 视频编码格式: h261、h263、h264、hevc、vc1、vp8、vp9、wmv3。 actions String 动作代码顺序列表,英文逗号(,)分隔。建议单动作,目前支持的动作有: 1:左摇头 2:右摇头 3:点头 4:嘴部动作 action_time String 该参数为动作时间数组拼接的字符串,数组的长度和actions的数量一致,每一项代表了对应次序动作的起始时间和结束时间,单位为距视频开始的毫秒数。 error_code String 调用失败时的错误码。 error_msg String 调用失败时的错误信息。 attributes String 是否返回人脸属性,希望获取的属性列表,多个属性用逗号隔开。目前支持的属性有: 0:人脸姿态 2:年龄 3:人脸关键点 4:装束(帽子、眼镜) 5:笑脸
  • 响应参数 状态码:200 表5 响应Body参数 参数 参数类型 描述 face_set_info FaceSetInfo object 人脸库信息,详见FaceSetInfo。 调用失败时无此字段。 表6 FaceSetInfo 参数 参数类型 描述 face_number Integer 人脸库中已有的人脸特征的数量。 external_fields Object 用户的自定义字段。 face_set_id String 人脸库ID,随机生成的包含八个字符的字符串。 face_set_name String 人脸库名称。 create_date String 创建时间。 face_set_capacity Integer 人脸库最大的容量。创建人脸库时,请求参数如果不设置face_set_capacity参数,默认每个人脸库最大容量为10万个人脸特征。 状态码: 400 表7 响应Body参数 参数 参数类型 描述 error_code String 调用失败时的错误码,具体请参考错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。
  • 功能介绍 创建用于存储人脸特征的人脸库。您最多可以创建10个人脸库,每个人脸库最大容量为10万个人脸特征。如有更大规格的需求请联系客服。 前提条件: 请确保您已开通人脸搜索服务。 默认情况下,一个人脸库最大可支持10万个人脸特征,一个用户最多可创建10个人脸库,最多可支持10*10万(100万)个人脸特征。 如您的需求超出100万个人脸特征,可通过工单或者服务热线( 4000-955-988或950808 转1)与我们联系,咨询具体解决方案。
  • 请求参数 表2 请求Header参数 参数 是否必选 参数类型 描述 X-Auth-Token 是 String 用户Token。 用于获取操作API的权限。获取方法请参见认证鉴权。 Content-Type 是 String 发送的实体的MIME类型,参数值为“application/json”。 Enterprise-Project-Id 否 String 企业项目ID。FRS支持通过企业项目管理(EPS)对不同用户组和用户的资源使用,进行分账,当前仅支持按需计费模式。 获取方法:进入“企业项目管理”页面,单击企业项目名称,在企业项目详情页获取Enterprise-Project-Id(企业项目ID)。 说明: 创建企业项目后,在传参时,有以下三类场景。 携带正确的ID,正常使用FRS服务,账单的企业项目会被分类到企业ID对应的企业项目中。 携带格式正确但不存在的ID,正常使用FRS服务,账单的企业项目会显示对应不存在的企业项目ID。 携带正确的ID,正常使用FRS服务,账单的企业项目会被分类到企业ID对应的企业项目中。 表3 请求Body参数 参数名 参数类型 是否必选 说明 face_id String 是 人脸ID,由系统内部生成的唯一ID。 external_image_id String 否 用户指定的图片外部ID,与当前图像绑定。用户不设置该参数时,系统会自动生成一个。该ID长度范围为[1,36],可以包含字母、数字、中划线或者下划线,不包含其他的特殊字符。 这里是待修改的参数,external_image_id和external_fields至少选一个。 external_fields Object 否 自定义字段的key值长度范围为[1,36],string类型的value长度范围为[1,256],具体参见自定义字段。 这里是待修改的参数,external_image_id和external_fields至少选一个。
  • 响应参数 状态码: 200 表4 响应Body参数 参数 参数类型 描述 face_number Integer 更新的人脸数量。 调用失败时无此字段。 face_set_id String 人脸库ID。 调用失败时无此字段。 face_set_name String 人脸库名称。 调用失败时无此字段。 状态码: 400 表5 响应Body参数 参数 参数类型 描述 error_code String 调用失败时的错误码,具体请参考错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。
  • APIG.0101 在调用人脸识别接口时,报错“APIG.0101”,“The API does not exist or has not been published in the environment”。该报错表示调用接口时使用的请求地址或者URL不存在或没有发布。 参考API使用说明获取正确的请求样例,并检查请求样例中需要修改的参数“{endpoint}”、{project_id},是否按规范进行替换。同时检查url中填写的endpoint区域时否和开通服务区域一致。 参考API使用说明获取正确的请求样例。 # 查询服务状态 https://{endpoint}/v1/{project_id}/subscribe 将请求样例中的“{endpoint}”进行替换。人脸识别服务的请求地请参考终端节点终端节点章节进行获取。 例如,如果您的服务部署在华北-北京四区域,则该区域的endpoint为“face.cn-north-4.myhuaweicloud.com”。 将请求样例中的“{project_id}”进行替换。项目ID的获取方法请参见获取项目ID。 URL请参考构造请求构造请求章节。
  • 操作场景 本节内容介绍了调用API时,常见的报错及处理办法。 表1 调用API高频报错处理办法 常见报错 处理办法 FRS.0020子服务未开通 开通该服务,检查开通服务的区域(或账号)与调用服务的区域(或账号)是否一致,检查API的URL是否拼写正确。 APIG.0101 检查请求地址或者URL是否填写正确。 账密报错The username or password is wrong 请确认近期华为云账号是否有升级为华为账号。如果进行过升级,建议您创建一个IAM账户,使用该账户获取Token。
  • FRS.0020子服务未开通 在调用人脸识别接口时,报错“FRS.0020”,子服务未开通。该报错表示您的子服务没有开通,或url填写错误。 如未开通服务请登录人脸识别服务控制台人脸识别服务控制台选择“开通服务”。 如已经开通服务,请检查调用接口的请求地址是否正确。 您还可以通过这个视频教程了解如何构造请求调用API:https://bbs.huaweicloud.com/videos/102987。 图1 检查区域信息
  • 账密报错The username or password is wrong 获取Token时出现“The username or password is wrong.”。 请确认近期华为云账号是否有升级为华为账号。当前,如果您通过华为账号入口登录华为云账号,就会指引升级。华为云账号若已升级为华为账号,将不支持获取账号Token。 建议您创建一个IAM账户,使用该账户获取Token。 详细处理步骤请参见如何处理账密报错。
  • 开始使用 详细的SDK介绍,使用异步客户端,配置日志请参见SDK中心、Python SDK使用指导、Python SDK使用视频。 导入依赖模块 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions # 导入v2版本sdk from huaweicloudsdkfrs.v2.region.frs_region import FrsRegion from huaweicloudsdkfrs.v2 import * import os 配置认证信息 配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作。 def GetCredential(ak, sk): return BasicCredentials(ak, sk) 初始化认证信息: ak = os.environ.get("HUAWEICLOUD_SDK_AK") sk = os.environ.get("HUAWEICLOUD_SDK_SK") credentials = GetCredential(ak, sk) 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 图3 Windows环境新建环境变量 初始化客户端 指定region方式 # 初始化人脸识别服务的客户端,并选择服务部署区域 def GetClient(): client = FrsClient.new_builder(FrsClient) \ .with_credentials(credentials) \ .with_region(FrsRegion.value_of("cn-north-4")) \ .build() return client 服务部署区域请参见终端节点。 发送请求并查看响应 # 以调用人脸检测 DetectFaceByBase64 接口为例 request = DetectFaceByBase64Request() request.body = FaceDetectBase64Req( image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.detect_face_by_base64(request) print(response) 使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。 异常处理 表1 异常处理 一级分类 一级分类说明 二级分类 二级分类说明 ConnectionException 连接类异常 HostUnreachableException 网络不可达、被拒绝。 SslHandShakeException SSL认证异常。 RequestTimeoutException 响应超时异常 CallTimeoutException 单次请求,服务器处理超时未返回。 RetryOutageException 在重试策略消耗完成后,仍无有效的响应。 ServiceResponseException 服务器响应异常 ServerResponseException 服务端内部错误,Http响应码:[500,]。 ClientRequestException 请求参数不合法,Http响应码:[400, 500) // 捕获和处理不同类型的异常 try: request = DetectFaceByBase64Request() request.body = FaceDetectBase64Req( image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.detect_face_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
  • SDK代码解析 人脸检测 # detect face by base64 def detectFaceByBase64(): try: request = DetectFaceByBase64Request() request.body = FaceDetectBase64Req( image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD...", attributes="2,4" ) response = client.detect_face_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # detect face by file def detectFaceByFile(): try: request = DetectFaceByFileRequest() with open("/root/picture.jpg", "rb") as f: request.body = DetectFaceByFileRequestBody(image_file=FormFile(f)) response = client.detect_face_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 人脸比对 # compare face by base64 def compareFaceByBase64(): try: request = CompareFaceByBase64Request() request.body = FaceCompareBase64Req( image1_base64="/9j/4AAQSkZJRgABAQAAAQABAAD...", image2_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.compare_face_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # compare face by file def compareFaceByFile(): try: request = CompareFaceByFileRequest() with open("/root/picture1.jpg", "rb") as f1: with open("/root/picture2.jpg", "rb") as f2: request.body = CompareFaceByFileRequestBody(image1_file=FormFile(f1), image2_file=FormFile(f2)) response = client.compare_face_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 人脸搜索 # search face by base64 def searchFaceByBase64(): try: request = SearchFaceByBase64Request() request.face_set_name = "face_set_name" listFaceSearchBase64ReqReturnFieldsbody = [ "timestamp" ] request.body = FaceSearchBase64Req( return_fields=listFaceSearchBase64ReqReturnFieldsbody, image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.search_face_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # search face by file def searchFaceByFile(): try: request = SearchFaceByFileRequest() request.face_set_name = "face_set_name" with open("/root/picture.jpg", "rb") as f: request.body = SearchFaceByFileRequestBody( return_fields="[\"timestamp\"]", filter="timestamp:10", top_n=10, image_file=FormFile(f) ) response = client.search_face_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 创建人脸库 def createFaceSet(): try: request = CreateFaceSetRequest() request.body = CreateFaceSetReq( face_set_name="face_set_name", external_fields={"timestamp": {"type": "long"}} ) response = client.create_face_set(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 查询人脸库 def showFaceSet(): try: request = ShowFaceSetRequest() request.face_set_name = "face_set_name" response = client.show_face_set(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 查询所有人脸库 def showAllFaceSet(): try: request = ShowAllFaceSetsRequest() response = client.show_all_face_sets(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 删除人脸库 def deleteFaceSet(): try: request = DeleteFaceSetRequest() request.face_set_name = "face_set_name" response = client.delete_face_set(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 添加人脸 # add face by base64 def addFacesByBase64(): try: request = AddFacesByBase64Request() request.face_set_name = "face_set_name" request.body = AddFacesBase64Req( external_fields="{\"timestamp\":12}", image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.add_faces_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # add face by file def addFacesByFile(): try: request = AddFacesByFileRequest() request.face_set_name = "face_set_name" with open("/root/picture.jpg", "rb") as f: request.body = AddFacesByFileRequestBody( external_fields="{\"timestamp\":12}", image_file=FormFile(f) ) response = client.add_faces_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 删除人脸 def deleteFace(): # Delete Face By FaceId try: request = DeleteFaceByFaceIdRequest() request.face_set_name = "face_set_name" request.face_id = "LkPJblq6" response = client.delete_face_by_face_id(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # Delete Face By ExternalImageId try: request = DeleteFaceByExternalImageIdRequest() request.face_set_name = "face_set_name" request.external_image_id = "external_image_id" response = client.delete_face_by_external_image_id(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 批量删除人脸 def batchDeleteFaces(): try: request = BatchDeleteFacesRequest() request.face_set_name = "face_set_name" request.body = DeleteFacesBatchReq( filter="age:[20 TO 30]" ) response = client.batch_delete_faces(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 更新人脸 def updateFace(): try: request = UpdateFaceRequest() request.face_set_name = "face_set_name" request.body = UpdateFaceReq(face_id="LkPJblq6") response = client.update_face(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 查询人脸 def showFaces(): # Show Faces By FaceId try: request = ShowFacesByFaceIdRequest() request.face_set_name = "face_set_name" request.face_id = "LkPJblq6" response = client.show_faces_by_face_id(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # Show Faces By Limit try: request = ShowFacesByLimitRequest() request.face_set_name = "face_set_name" request.offset = 0 request.limit = 10 response = client.show_faces_by_limit(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 动作活体检测 # detect live by base64 def detectLiveByBase64(): try: request = DetectLiveByBase64Request() request.body = LiveDetectBase64Req( actions="1,2,3,4", video_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.detect_live_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # detect live by file def detectLiveByFile(): try: request = DetectLiveByFileRequest() with open("/root/video.mp4", "rb") as f: request.body = DetectLiveByFileRequestBody( video_file=FormFile(f), actions="1,2,3,4" ) response = client.detect_live_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) 静默活体检测 # detect live face by base64 def detectLiveFaceByBase64(): try: request = DetectLiveFaceByBase64Request() request.body = LiveDetectFaceBase64Req( image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..." ) response = client.detect_live_face_by_base64(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # detect live face by file def detectLiveFaceByFile(): try: request = DetectLiveFaceByFileRequest() with open("/root/picture.jpg", "rb") as f: request.body = DetectLiveFaceByFileRequestBody( image_file=FormFile(f) ) response = client.detect_live_face_by_file(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
  • 开始使用 详细的SDK介绍,使用异步客户端,配置日志请参见SDK中心、Java SDK使用指导、Java SDK使用视频。 导入依赖模块 import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; //导入v2版本sdk import com.huaweicloud.sdk.frs.v2.region.FrsRegion; import com.huaweicloud.sdk.frs.v2.*; import com.huaweicloud.sdk.frs.v2.model.*; 配置认证信息 配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作。 public static ICredential getCredential(String ak, String sk) { return new BasicCredentials() .withAk(ak) .withSk(sk); } 初始化认证信息: String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); ICredential credential = getCredential(ak, sk); 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 图3 Windows环境新建环境变量 初始化客户端 指定region方式 public static FrsClient getClient(Region region, ICredential auth) { // 初始化人脸识别服务的客户端 FrsClient client = FrsClient.newBuilder() .withCredential(auth) .withRegion(FrsRegion.valueOf("cn-north-4")) // 选择服务所在区域 FrsRegion.valueOf("cn-north-4") .build(); return client; } 服务部署区域请参见终端节点。 发送请求并查看响应 // 以调用人脸检测 DetectFaceByBase64 接口为例 DetectFaceByBase64Request request = new DetectFaceByBase64Request(); FaceDetectBase64Req body = new FaceDetectBase64Req(); body.withImageBase64("/9j/4AAQSkZJRgABAQAAAQABAAD..."); request.withBody(body); DetectFaceByBase64Response response = client.detectFaceByBase64(request); System.out.println(response.toString()); 使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。 异常处理 表1 异常处理 一级分类 一级分类说明 二级分类 二级分类说明 ConnectionException 连接类异常 HostUnreachableException 网络不可达、被拒绝。 SslHandShakeException SSL认证异常。 RequestTimeoutException 响应超时异常 CallTimeoutException 单次请求,服务器处理超时未返回。 RetryOutageException 在重试策略消耗完成后,仍无有效的响应。 ServiceResponseException 服务器响应异常 ServerResponseException 服务端内部错误,Http响应码:[500,]。 ClientRequestException 请求参数不合法,Http响应码:[400, 500) // 捕获和处理不同类型的异常 DetectFaceByBase64Request request = new DetectFaceByBase64Request(); try { DetectFaceByBase64Response response = client.detectFaceByBase64(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); }