前几天看到百度邮件说提供了人物动漫化的接口,一上来描(kai)述(chui):运用对抗生成网络技术,结合人脸检测、头发分割、人像分割等技术,为用户量身定制千人千面的二次元动漫形象。
不管用什么高科技技术,先看看效果怎么样,太差建议回炉重造。
https://ai.baidu.com/tech/imageprocess/selfie_anime
打开网址,首先我们先新建一个"图像效果增强"的应用,然后拷贝一下 APIKey 和 SecretKey 的密钥。
然后 python3.6 代码如下:
#coding:utf-8
import base64
import requests
def getToken(ak, sk):
# client_id 为官网获取的APIKey, client_secret 为官网获取的SecretKey
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ak + '&client_secret=' + sk
response = requests.get(host)
if response:
return response.json()
# 人像动漫化
def selfieAnime(filePath, access_token):
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
f = open(filePath, 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
with open('ttt.jpg','wb') as f:
f.write(base64.b64decode(response.json()['image']))
filePath = 'luer.jpg'
client_id = 'r********************H'
client_secret = 'R**************************p'
token = getToken(client_id, client_secret)
selfie_anime(filePath, token['access_token'])
第一张效果还行吧
关于java的调用代码:
代码较多,存放在码云git仓库中,请自取。
https://gitee.com/mjTree/MyPython/tree/master/有趣代码/百度云API/人物动漫化
至于其他照片,丑到不行。。。。