Python调用腾讯API进行人像动漫化

1. 作者介绍

李犇,男, 西安工程大学电子信息学院,21级硕士研究生
研究方向:智能信息处理
电子邮箱:1135264546@qq.com

孟莉苹,女,西安工程大学电子信息学院,2021级硕士研究生,张宏伟人工智能课题组
研究方向:机器视觉与人工智能
电子邮件:2425613875@qq.com

2. API获取

1.确保电脑正确连接网络。
2.浏览器搜索“腾讯云”,或者输入网址:
https://cloud.tencent.com
3.进行简单的注册、实名认证。
4.新建一个密钥
在这里插入图片描述

5.在搜索框搜索人像变换,点击控制台入口下方的人像动漫画,扫码实名认证。
在这里插入图片描述

6.实名验证成功之后,就可在此页面查看API的调用情况。
在这里插入图片描述

7.搜索相关文档找到想要的API
在这里插入图片描述

8.勾选“只看必要参数”,随意选择一个地区,点击“发起调用”,右边框中选择自己熟悉的计算机语言,我选用的是python
在这里插入图片描述

3. 代码调试

3.1整体结构

在这里插入图片描述

3.2 代码解析

pip 安装腾讯云SDK:

pip install --upgrade tencentcloud-sdk-python  

导入必要的包,输入申请好的密钥和密码,读取图片,然后将图片转码。

import json
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 base64


SecretId = "自己的SecretId"
SecretKey = "自己的SecretKey"

img_dir = "C:/Users/Ben/Desktop/img/flower.jpg"
with open(img_dir, 'rb') as f:
    base64_data = base64.b64encode(f.read())
    base64_code = base64_data.decode()

问:图片在发送之前被编码成了啥? 答:一堆神秘的字符串
在这里插入图片描述
调用API处理图片, 接收数据,解码显示。
pip 安装腾讯云SDK:

try:
    cred = credential.Credential(SecretId, SecretKey)
    httpProfile = HttpProfile()
    httpProfile.endpoint = "ft.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = ft_client.FtClient(cred, "ap-beijing", clientProfile)
    req = models.FaceCartoonPicRequest()
    req.Image = base64_code
    resp = client.FaceCartoonPic(req)
    print(resp)
    json_data = resp.to_json_string()
    bb = json.loads(json_data)

    image = bb["ResultImage"]

    with open('C:/Users/Ben/Desktop/img/flower2.jpg', 'wb') as f:
        f.write(base64.b64decode(image))
        f.close()
 

问:API调用成功返回给电脑了什么?
答:返回一个字典类型的数据。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3完整代码

import json
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 base64


SecretId = "自己的SecretId"
SecretKey = "自己的SecretKey"

img_dir = "C:/Users/Ben/Desktop/img/flower.jpg"
with open(img_dir, 'rb') as f:
    base64_data = base64.b64encode(f.read())
    base64_code = base64_data.decode()

try:
    cred = credential.Credential(SecretId, SecretKey)
    httpProfile = HttpProfile()
    httpProfile.endpoint = "ft.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = ft_client.FtClient(cred, "ap-beijing", clientProfile)
    req = models.FaceCartoonPicRequest()
    req.Image = base64_code
    resp = client.FaceCartoonPic(req)
    print(resp)
    json_data = resp.to_json_string()
    bb = json.loads(json_data)

    image = bb["ResultImage"]

    with open('C:/Users/Ben/Desktop/img/flower2.jpg', 'wb') as f:
        f.write(base64.b64decode(image))
        f.close()



except TencentCloudSDKException as err:
    print(err)

3.4效果展示:

在这里插入图片描述

“浑元形意太极拳掌门人”马保国老师

4. 问题与分析

API返回的参数需要使用 “ .to_json_string() “像数据格式转换为json可读的形式否则如下图:
在这里插入图片描述

如果上传的照片没有人脸:
在这里插入图片描述

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 111
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 111
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值