一个教程:调用腾讯云的人脸识别api和修改年龄api

在网上看到一个教程,调用腾讯云的人脸识别api修改年龄api模拟不同年龄人物的人脸

但是教程里大部分代码都是一样的,估计抄袭的关键是不能执行

正好周杰伦要出新专辑,做个小改动,拍下周杰伦的照片,做个实验

开始

1、先去腾讯云注册一个账号,打开API密钥管理页面()获取SecretId和SecretKey,这串值一定要保存,后面的脚本需要用到

2、 然后创建资源,到左上角的云产品入口,选择“人脸识别”和“人脸转换”创建资源,目前的规则是每个账号有1000个资源可以be used and created 资源不会在资源之后立即可用,所以先执行此步骤。

创建完成后,可以在左侧的资源目录中看到当前资源状态

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)

执行脚本,脚本会在同一个目录下生成各个年龄段的图片,哐当哐哐,然后就可以自由发挥了

注意文件目录,当然会改脚本,不能这样存放,我省事

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

© 版权声明
THE END
喜欢就支持一下吧
点赞200赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容