网络编程
位置:首页>> 网络编程>> Python编程>> python实战之百度智能云使人像动漫化

python实战之百度智能云使人像动漫化

作者:Linkage interrupt  发布时间:2021-01-19 07:15:11 

标签:python,人像动漫化,百度智能云

一、目标

之前无意中看到有某位博主写过人像动漫化这样的文章,看着还挺好玩,所以我也想尝试一下。

利用百度智能云中的人工智能,对图片进行处理达到人像动漫化的效果。

二、准备工作

1.百度云智能账号创建

2.图像特效应用

3.开发环境python3.7+pycharm

首先要注册一个百度智能云账号,并创建这个图像特效应用

python实战之百度智能云使人像动漫化

三、操作流程

3.1 阅读官方文档

当我们要使用一个我们不太了解的东西时,阅读官方文档无疑是最重要的,官方文档一般都写的特别详细,对每一个功能描述的很细节,我们先来看一下

python实战之百度智能云使人像动漫化
python实战之百度智能云使人像动漫化

而且这里有案例,这里我使用的是python

3.2 开始实现鉴权

因为调用这么个接口api要进行鉴权,就是官方文档说得到access_token,如何鉴权呢?

python实战之百度智能云使人像动漫化
python实战之百度智能云使人像动漫化


import requests
import pprint
def get_access_token(id,secret):
   get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
   response=requests.get(get_access_token_url)
   pprint.pprint(response.json())
id='*******************'
secret='******************'
get_access_token(id,secret)

这里的id和secret就是创建应用的appkey和secretkey:

python实战之百度智能云使人像动漫化

上述代码打印结果有很多,阅读官网文档得知,我们这里只需要得到access_token就OK了

python实战之百度智能云使人像动漫化

修改上述代码以获取access_token


import requests
def get_access_token(id,secret):
   get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
   response=requests.get(get_access_token_url)
   content=response.json()
   access_token=content['access_token']
   print(access_token)
id='*******************'
secret='******************'
get_access_token(id,secret)

3.3 人像动漫化实现

正片开始

python实战之百度智能云使人像动漫化

python实战之百度智能云使人像动漫化

修改代码


import requests
import pprint
import base64
def get_access_token(id,secret):
   get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
   response=requests.get(get_access_token_url)
   content=response.json()
   access_token=content['access_token']
   return access_token

def Animation(img_file,access_token):
   request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
   f=open(img_file,'rb')
   image=base64.b64encode(f.read())
   params = {"image":image}
   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)
   pprint.pprint(response.json())
def main():
   img_file = '1.jpg'#图片地址
   id = '**************************'
   secret = '**************************'
   access_token = get_access_token(id, secret)
   Animation(img_file, access_token)
if __name__ == '__main__':
   main()

这时可以得到一系列的返回值

python实战之百度智能云使人像动漫化

我们这里只要image

获取image值

修改代码


import requests
import pprint
import base64
def get_access_token(id,secret):
   get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
   response=requests.get(get_access_token_url)
   content=response.json()
   access_token=content['access_token']
   return access_token

def Animation(img_file,access_token):
   request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
   f=open(img_file,'rb')
   image=base64.b64encode(f.read())
   params = {"image":image}
   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)
   image_content=response.json()
   image=image_content['image']
   print(image)
def main():
   img_file = '1.jpg'#图片地址
   id = '**************************'
   secret = '**************************'
   access_token = get_access_token(id, secret)
   Animation(img_file, access_token)
if __name__ == '__main__':
   main()

获取到一串base64编码的图片,这显然快得到我们想要的东西了


with open('result.jpg','wb') as f:
       f.write(base64.b64decode(image))

保存到本地

看一下对比

python实战之百度智能云使人像动漫化
python实战之百度智能云使人像动漫化

呃呃呃,这。。。。还好吧,哈哈哈

四、完整代码如下


import requests
import pprint
import base64
def get_access_token(id,secret):
   get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
   response=requests.get(get_access_token_url)
   content=response.json()
   access_token=content['access_token']
   return access_token

def Animation(img_file,access_token):
   request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
   f=open(img_file,'rb')
   image=base64.b64encode(f.read())
   params = {"image":image}
   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)
   image_content=response.json()
   image=image_content['image']
   with open('result.jpg','wb') as f:
       f.write(base64.b64decode(image))  
def main():
   img_file = '1.jpg'#图片地址
   id = '**************************'
   secret = '**************************'
   access_token = get_access_token(id, secret)
   Animation(img_file, access_token)
if __name__ == '__main__':
   main()

五、还能这么玩?

python实战之百度智能云使人像动漫化

厉害了,还能加口罩,试一下

修改代码


params =
{
"image":image,"type":'anime_mask',"mask_id":1
}
#mask_id 1-8的整数,就用个1吧

看一下效果
python实战之百度智能云使人像动漫化

啧啧啧

来源:https://blog.csdn.net/qq_44862120/article/details/115869644

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com