# -*- coding: utf-8 -*- # 人脸融合模板上传 # This file is auto-generated, don't edit it. Thanks. import os from alibabacloud_facebody20191230.client import Client as facebody20191230Client from alibabacloud_facebody20191230.models import MergeImageFaceRequest, FaceBeautyRequest, FaceTidyupRequest, FaceMakeupRequest, LiquifyFaceRequest, RetouchSkinRequest from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_facebody20191230 import models as facebody_20191230_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_console.client import Client as ConsoleClient from alibabacloud_tea_util.client import Client as UtilClient from alibabacloud_tea_util.models import RuntimeOptions from django.conf import settings class VI: def __init__(self): self.client = VI.create_client() @staticmethod def create_client() -> facebody20191230Client: """ 使用AK&SK初始化账号Client @return: Client @throws Exception """ # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 # 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。 # Endpoint 请参考 https://api.aliyun.com/product/facebody # 访问的域名对应的region config = open_api_models.Config( access_key_id=settings.ALIYUN_VI_ACCESS_KEY_ID, # os.getenv("ALIYUN_VI_ACCESS_KEY_ID"), access_key_secret=settings.ALIYUN_VI_ACCESS_KEY_SECRET, # os.getenv("ALIYUN_VI_ACCESS_KEY_SECRET"), endpoint=settings.ALIYUN_VI_ENDPOINT, # os.getenv("ALIYUN_VI_ENDPOINT"), region_id=settings.ALIYUN_VI_REGION # os.getenv("ALIYUN_VI_REGION") ) return facebody20191230Client(config) def add_template(self, image_url) -> None: add_face_image_template_request = facebody_20191230_models.AddFaceImageTemplateRequest( image_url=image_url ) runtime = util_models.RuntimeOptions() try: resp = self.client.add_face_image_template_with_options(add_face_image_template_request, runtime) ConsoleClient.log(UtilClient.to_jsonstring(resp)) return resp except Exception as error: # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 # 错误 message print(error.message) # 诊断地址 print(error.data.get("Recommend")) UtilClient.assert_as_string(error.message) return error def merge_image_face(self, template_id, image_url): runtime_option = RuntimeOptions() merge_image_face_request = MergeImageFaceRequest( template_id=template_id, image_url=image_url ) try: response = self.client.merge_image_face_with_options(merge_image_face_request, runtime_option) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 def face_beauty(self, image_url: str, sharp: float = 1, smooth: float = 0.1, white: float = 0.3): """人脸美颜 Sharp Float 是 1 锐化(清晰度)程度,取值范围0~1。越接近1锐化程度越高。 Smooth Float 是 0.1 平滑程度,取值范围0~1。越接近1平滑程度越高。 White Float 是 0.3 美白程度,取值范围0~1。越接近1美白程度越高。 """ # https://help.aliyun.com/zh/viapi/developer-reference/api-la7495?spm=a2c4g.11186623.0.i1 # Sharp Float 是 1 # 锐化(清晰度)程度,取值范围0~1。越接近1锐化程度越高。 # Smooth Float 是 0.1 # 平滑程度,取值范围0~1。越接近1平滑程度越高。 # White 是 0.3 # 美白程度,取值范围0~1。越接近1美白程度越高。 face_beauty_request = FaceBeautyRequest( image_url=image_url, sharp=sharp, smooth=smooth, white=white ) try: response = self.client.face_beauty(face_beauty_request) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 def face_tidyup(self, image_url: str, shape_type: int = 0, strength: float = 1): """人脸美型 ShapeType Integer 是 0 表示美型部位,包括:0(颧骨)、1(削脸)、2(瘦脸)、3(脸长)、4(下巴缩短)、5(下巴拉长)、6(瘦下巴)、7(瘦下颌)、8(大眼)、9(眼角1)、10(眼距)、11(拉宽眼距)、12(眼角2)、13(眼睛高度)、14(瘦鼻)、15(鼻翼)、16(鼻长)、17(鼻头长)、18(唇宽)、19(嘴唇大小)、20(唇高)、21(人中)。 Strength Float 是 1 美型强度,取值范围0~1。 https://help.aliyun.com/zh/viapi/developer-reference/api-g1j8tt """ # https://help.aliyun.com/zh/viapi/developer-reference/api-la7495?spm=a2c4g.11186623.0.i1 face_tidyup_request = FaceTidyupRequest( image_url=image_url, shape_type=shape_type, strength=strength ) try: response = self.client.face_tidyup(face_tidyup_request) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 def face_makeup(self, image_url: str, resource_type: int = 0, strength: float = 1): """人脸美妆 MakeupType String 是 whole 美妆类型,当前支持whole(整妆)。 ResourceType String 是 0 美妆使用的风格,具体包括:0(whole 整妆)、1(基础妆)、2(少女妆)、3(活力妆)、4(优雅妆)、5(魅惑妆)、6(梅子妆)。 Strength Float 是 1 美妆强度,取值范围0~1。 https://help.aliyun.com/zh/viapi/developer-reference/api-k3468h """ face_makeup_request = FaceMakeupRequest( image_url=image_url, makeup_type="whole", resource_type=resource_type, strength=strength ) try: response = self.client.face_makeup(face_makeup_request) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 def liquify_face(self, image_url: str, slim_degree: float = 1): """智能瘦脸 SlimDegree Float 否 1.0 标准力度。取值范围为[0,2.0],默认为1.0。数值越大,瘦脸效果越明显。 https://help.aliyun.com/zh/viapi/developer-reference/api-smart-face-thinning """ liquify_face_request = LiquifyFaceRequest( image_url=image_url, slim_degree=slim_degree ) try: response = self.client.liquify_face(liquify_face_request) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 def retouch_skin(self, image_url: str, retouch_degree: float = 1, whitening_degree: float = 1): """智能美肤 RetouchDegree Float 否 1.0 磨皮力度。取值范围为[0,1.5],默认为1.0。数值越大皮肤纹理越不明显。 WhiteningDegree Float 否 1.0 美白力度。取值范围为[0,1.5],默认为1.0。数值越大皮肤越白。 https://help.aliyun.com/zh/viapi/developer-reference/api-intelligent-skin-beautifying """ retouch_skin_request = RetouchSkinRequest( image_url=image_url, retouch_degree=retouch_degree, whitening_degree=whitening_degree ) try: response = self.client.retouch_skin(retouch_skin_request) return response.body.data.image_url except Exception as error: # 获取整体报错信息 print(error) # 获取单个字段 print(error.code) # tips: 可通过error.__dict__查看属性名称 if __name__ == '__main__': vi = VI() # 创建人脸模板 # template_url = 'https://face-test.oss-cn-shanghai.aliyuncs.com/face/1.jpg' # resp = vi.add_template(template_url) # print(resp) # 人脸融合 # template_id = "c2f7b93b-05c5-4def-b4f3-1627681ca9ef" # image_url = 'https://face-test.oss-cn-shanghai.aliyuncs.com/people/w.png' # merge_image_url = vi.merge_image_face(template_id, image_url) # print(merge_image_url) # 人脸美颜 image_url = 'https://face-test.oss-cn-shanghai.aliyuncs.com/people/w.png' res_url = vi.face_beauty(image_url) print(res_url)