Python教程

Python调用腾讯云API,实现人脸年龄变化

本文主要是介绍Python调用腾讯云API,实现人脸年龄变化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

网上看到了一个教程,调用腾讯云的人脸识别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)

执行脚本,脚本同目录下就会生成各年龄时期的图片了,铛铛铛,后面就自由发挥了

 

 注意文件目录,当然,你会改脚本,可以不这么存放,我省事

 

 在上一张原图把,致敬!!!!

 

这篇关于Python调用腾讯云API,实现人脸年龄变化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!