基于百度AI平台Python实现人像动漫画

二话不说,先上图!!!
人像动漫图

图一   人像动漫图

带口罩的人像动漫图

图二   戴口罩的人像动漫图

一、实现步骤

首先在 百度智能云注册账号,然后在产品服务 → \rightarrow 人工智能 → \rightarrow 人脸识别 → \rightarrow 创建应用 → \rightarrow 填写“应用名称” → \rightarrow 图像增强与特效 下 → \rightarrow 勾选“人像动漫画” → \rightarrow 填写“应用归属”(依个人情况填写公司还是个人) → \rightarrow 填写“应用描述”
注: 带 *为必填选项,其他的可以不填。

创建完应用后,平台会给你提供API Key 、 Secret Key,把这两个复制下来到下面的程序中修改成自己的即可。
在这里插入图片描述

二、实现代码

2.1人像动漫画代码

import requests, base64
# 这个函数的操作是为了获取access_token参数
def get_access_token():
    url = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',  # 固定值
        'client_id': '3j8EWb6rgg..SPY2X693LBy' ,  # 在开放平台注册后所建应用的API Key
        'client_secret': 'Px9KZuU0Gl...jTKktoCopnIWEiF57gf'  # 所建应用的Secret Key
    }
    res = requests.post(url, data=data)
    res = res.json()
    #print(res)
    access_token = res['access_token']
    return access_token
    
# 下面的代码就是API文档中的代码,直接搬过来使用即可。
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
f = open('原图.jpg', 'rb')       # 二进制方式打开图片文件
img = base64.b64encode(f.read()) # 图像转为base64的格式,这是百度API文档中要求的
 
params = {"image":img}
access_token = '24.11731cd1f0...9f9b3a930f917f3681b.2592000.1596894747.282335-21221990'
request_url = request_url + "?access_token=" + get_access_token()
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
res = response.json()
# 前面我们讲述了这个请求返回的是一个字典,其中一个键就是image,代表的是处理后的图像信息。
# 将这个图像信息写入,得到最终的效果图。
if res:
    f = open("生成的动漫图.jpg", 'wb')
    after_img = res['image']
    after_img = base64.b64decode(after_img)
    f.write(after_img)
    f.close()

2.2戴口罩的人像动漫画代码

import requests, base64
# 这个函数的操作是为了获取access_token参数
def get_access_token():
    url = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',  # 固定值
        'client_id': '3j8EWb6rgg...SPY2X693LBy',  # 在开放平台注册后所建应用的API Key
        'client_secret': 'Px9KZuU0Gl...jTKktoCopnIWEiF57gf'  # 所建应用的Secret Key
    }
    res = requests.post(url, data=data)
    res = res.json()
    #print(res)
    access_token = res['access_token']
    return access_token
    
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 二进制方式打开图片文件
f = open('原图.jpg', 'rb')
img = base64.b64encode(f.read())
# 注意:这里就是多了type参数和mask_id参数,都是在源文档中可以查看的参数。
# type的值为anime或者anime_mask。前者生成二次元动漫图,后者生成戴口罩的二次元动漫人像。
# 18之间的整数,用于指定所使用的口罩的编码。大家可以自行下去尝试。
params = {"image":img,"type":'anime_mask',"mask_id":"2"}
access_token = '24.11731cd1f0...9f9b3a930f917f3681b.2592000.1596894747.282335-21221990'
request_url = request_url + "?access_token=" + get_access_token()
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
res = response.json()
# print(res)
if res:
    f = open("生成的动漫图.jpg", 'wb')
    after_img = res['image']
    after_img = base64.b64decode(after_img)
    f.write(after_img)
    f.close()

代码来源链接: 太牛逼了!用 Python 实现抖音上的“人像动漫化”特效,原来这么简单!.

笔记:在运行代码的时候,发现报错,报内容如下:

Traceback (most recent call last):
  File "D:/python/person/人像动漫画.py", line 34, in <module>
    after_img = res['image']
KeyError: 'image'

结果发现是因为没有领人像动漫画资源,导致没有权限调用,还有个因素也会报这种错误就是图片大小超过规定大小,图片要求具体如下所示:
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值