|
| 1 | +import json |
| 2 | +import base64 |
| 3 | +import time |
| 4 | +from tencentcloud.common import credential |
| 5 | +from tencentcloud.common.profile.client_profile import ClientProfile |
| 6 | +from tencentcloud.common.profile.http_profile import HttpProfile |
| 7 | +from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException |
| 8 | +from tencentcloud.iai.v20200303 import iai_client |
| 9 | +from tencentcloud.iai.v20200303 import models as models03 |
| 10 | + |
| 11 | +from tencentcloud.ft.v20200304 import ft_client,models |
| 12 | + |
| 13 | + |
| 14 | +sid = "xx" |
| 15 | +skey = "xx" |
| 16 | +try: |
| 17 | + |
| 18 | + filepath = '/Users/imeng/Downloads/face/face.png' |
| 19 | + file = open(filepath, "rb") |
| 20 | + |
| 21 | + base64_data = base64.b64encode(file.read()) |
| 22 | + |
| 23 | + cred = credential.Credential(sid, skey) |
| 24 | + httpProfile = HttpProfile() |
| 25 | + httpProfile.endpoint = "iai.tencentcloudapi.com" |
| 26 | + |
| 27 | + clientProfile = ClientProfile() |
| 28 | + clientProfile.httpProfile = httpProfile |
| 29 | + client = iai_client.IaiClient(cred, "ap-beijing", clientProfile) |
| 30 | + |
| 31 | + req = models03.DetectFaceAttributesRequest() |
| 32 | + params = { |
| 33 | + "MaxFaceNum":2, |
| 34 | + "Action":"DetectFace", |
| 35 | + "Version":"2018-03-01", |
| 36 | + "Image": base64_data.decode() |
| 37 | + } |
| 38 | + req.from_json_string(json.dumps(params)) |
| 39 | + |
| 40 | + resp = client.DetectFaceAttributes(req) |
| 41 | + faceDetailInfos = resp.FaceDetailInfos |
| 42 | + |
| 43 | + httpProfile.endpoint = "ft.tencentcloudapi.com" |
| 44 | + clientProfile.httpProfile = httpProfile |
| 45 | + client = ft_client.FtClient(cred, "ap-beijing", clientProfile) |
| 46 | + |
| 47 | + req = models.ChangeAgePicRequest() |
| 48 | + |
| 49 | + for age in range(70, 80): |
| 50 | + params = { |
| 51 | + "Image": base64_data.decode(), |
| 52 | + "AgeInfos": [ |
| 53 | + { |
| 54 | + "Age": age, |
| 55 | + "FaceRect": { |
| 56 | + "Y": faceDetailInfos[0].FaceRect.Y, |
| 57 | + "X": faceDetailInfos[0].FaceRect.X, |
| 58 | + "Width": faceDetailInfos[0].FaceRect.Width, |
| 59 | + "Height": faceDetailInfos[0].FaceRect.Height |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + "Age": age, |
| 64 | + "FaceRect": { |
| 65 | + "Y": faceDetailInfos[1].FaceRect.Y, |
| 66 | + "X": faceDetailInfos[1].FaceRect.X, |
| 67 | + "Width": faceDetailInfos[1].FaceRect.Width, |
| 68 | + "Height": faceDetailInfos[1].FaceRect.Height |
| 69 | + } |
| 70 | + } |
| 71 | + ], |
| 72 | + "RspImgType": "base64" |
| 73 | + } |
| 74 | + req.from_json_string(json.dumps(params)) |
| 75 | + resp = client.ChangeAgePic(req) |
| 76 | + image_base64 = resp.ResultImage |
| 77 | + image_data = base64.b64decode(image_base64) |
| 78 | + file_path = '/Users/imeng/Downloads/face/{}.png'.format(age) |
| 79 | + with open(file_path, 'wb') as f: |
| 80 | + f.write(image_data) |
| 81 | + time.sleep(1) |
| 82 | +except TencentCloudSDKException as err: |
| 83 | + print(err) |
0 commit comments