代码:
import json import base64 from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.iai.v20200303 import iai_client from tencentcloud.iai.v20200303 import models as models1 from tencentcloud.ft.v20200304 import ft_client from tencentcloud.ft.v20200304 import models as models2 sid = "" #填写自己申请的控制台sid skey = "" #填写自己申请的 try: filepath = 'img/xxb1.png' file = open(filepath, "rb") base64_data = base64.b64encode(file.read()) cred = credential.Credential(sid, skey) httpProfile1 = HttpProfile() httpProfile1.endpoint = "iai.tencentcloudapi.com" httpProfile2 = HttpProfile() httpProfile2.endpoint = "ft.tencentcloudapi.com" clientProfile1 = ClientProfile() clientProfile1.httpProfile = httpProfile1 clientProfile2 = ClientProfile() clientProfile2.httpProfile = httpProfile2 client1 = iai_client.IaiClient(cred, "ap-beijing", clientProfile1) client2 = ft_client.FtClient(cred, "ap-beijing", clientProfile2) req1 = models1.DetectFaceRequest() params1 = { "Action":"DetectFace", "Version":"2020-03-03", "Region":"ap-beijing", "MaxFaceNum":2, #"Image": base64_data.decode(), "Url": "https://images.cnblogs.com/cnblogs_com/blogs/779648/galleries/2264184/o_230217013139_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230217093013.jpg", } req1.from_json_string(json.dumps(params1)) resp1 = client1.DetectFace(req1) print(resp1.to_json_string(indent=2)) age = 50 req2 = models2.ChangeAgePicRequest() params2 = { "Action":"ChangeAgePic", "Version":"2020-03-04", "Region":"ap-beijing", "AgeInfos": [ { "Age": age, "FaceRect" : {"Y":resp1.FaceInfos[0].Y, "X":resp1.FaceInfos[0].X, "Width":resp1.FaceInfos[0].Width, "Height":resp1.FaceInfos[0].Height} }, { "Age": age, "FaceRect" : {"Y":resp1.FaceInfos[1].Y, "X":resp1.FaceInfos[1].X, "Width":resp1.FaceInfos[1].Width, "Height":resp1.FaceInfos[1].Height} } ], #"Image": base64_data.decode(), "Url": "https://images.cnblogs.com/cnblogs_com/blogs/779648/galleries/2264184/o_230217013139_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230217093013.jpg", } req2.from_json_string(json.dumps(params2)) resp2 = client2.ChangeAgePic(req2) image_base64 = resp2.ResultImage image_data = base64.b64decode(image_base64) file_path = 'img/xuxb1_%s.png' % age with open(file_path, 'wb') as f: f.write(image_data) print("年龄变换完成") except TencentCloudSDKException as err: print(err)
效果(挺好玩的):
标签:tencentcloud,base64,接口,照片,FaceInfos,腾讯,import,com,resp1 From: https://www.cnblogs.com/xuxiaobo/p/17146655.html