当前位置:   article > 正文

基于百度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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/376902
推荐阅读
相关标签
  

闽ICP备14008679号