網上看到了一個教程,調用騰訊雲的人臉識別api和修改年齡api來實現模擬人物不同年齡的面貌
但是大多數教程的代碼都是想同的,估計是抄襲哪個人的關鍵是執行不了
剛好周傑倫馬上要發新專輯了,小改一下,拿傑倫的照,做個實驗咯
開始了
1、首先到騰訊雲上注冊賬號,打開 API 密鑰管理頁面(https://console.cloud.tencent.com/cam/capi)獲取到 SecretId 和 SecretKey,這串值要保存好,後面的腳本需要用上
2、然後創建資源,左上角雲產品入口,依次選擇“人臉識別”和“人臉變換”創建資源,現在的規則是每個賬號有1000個資源可以使用,而且創建資源後並不會馬上可用,所以這一步先做好。
創建完成後,在左側資源目錄下就可以看到當前資源情況
3、 安裝騰訊雲的 SDK,
pip3 install tencentcloud-sdk-python
4、接下來就是腳本,實現人臉變化需要先後調用“人臉識別”和“人臉變換”兩個api,原計劃是打算把兩個腳本合二為一,有空再折騰把。
首先是執行“人臉識別”步驟,獲取面部屬性值,直接上代碼了
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 models03 sid = "xxxxx"#第一步獲取的SecretId skey = "xxxxxx"#第一步獲取的SecretKey try: filepath = 'test.jpg'#需要變臉的圖片,最好是正面照 file = open(filepath, "rb") base64_data = base64.b64encode(file.read()) cred = credential.Credential(sid, skey) httpProfile = HttpProfile() httpProfile.endpoint = "iai.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = iai_client.IaiClient(cred, "ap-beijing", clientProfile) req = models03.DetectFaceAttributesRequest() params = { "MaxFaceNum":2, "Action":"DetectFace", "Version":"2018-03-01", "Image": base64_data.decode() } req.from_json_string(json.dumps(params)) resp = client.DetectFaceAttributes(req) faceDetailInfos = resp.FaceDetailInfos for faceDetailInfo in faceDetailInfos: faceRect = faceDetailInfo.FaceRect print(faceRect) except TencentCloudSDKException as err: print(err)
執行後,獲取返回信息,記錄返回值裡的x、y等值
5,執行修改年齡的腳本,將上一步獲取的X、Y、Width等值填到腳本對應內容
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.ft.v20200304 import ft_client, models import time sid = "xxxx"#第一步獲取的SecretId skey = "xxxx"#第一步獲取的SecretKey cred = credential.Credential(sid, skey) httpProfile = HttpProfile() httpProfile.endpoint = "ft.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = ft_client.FtClient(cred, "ap-beijing", clientProfile) filepath = 'test.jpg' file = open(filepath, "rb") base64_data = base64.b64encode(file.read()) req = models.ChangeAgePicRequest() for age in range(10, 80):#這裡設置年齡,比如(10,30)意味著生成10歲到79歲的圖片,一共71張 params = { "Image": base64_data.decode(), "AgeInfos": [{ "Age": age, "FaceRect": { "Y": 120, #注意第一個和第二個X、Y、Width、Height值都需要修改 "X": 198, "Width": 150, "Height": 201 }}, { "Age": age, "FaceRect": { "Y": 120, "X": 198, "Width": 150, "Height": 201 }}], "RspImgType": "base64" } req.from_json_string(json.dumps(params)) resp = client.ChangeAgePic(req) image_base64 = resp.ResultImage image_data = base64.b64decode(image_base64) file_path = '{}.png'.format(age) with open(file_path, 'wb') as f: f.write(image_data) time.sleep(1)
執行腳本,腳本同目錄下就會生成各年齡時期的圖片了,铛铛铛,後面就自由發揮了
注意文件目錄,當然,你會改腳本,可以不這麼存放,我省事
在上一張原圖把,致敬!!!!