python实战之百度智能云使人像动漫化
作者:Linkage interrupt 时间:2021-01-19 07:15:11
一、目标
之前无意中看到有某位博主写过人像动漫化这样的文章,看着还挺好玩,所以我也想尝试一下。
利用百度智能云中的人工智能,对图片进行处理达到人像动漫化的效果。
二、准备工作
1.百度云智能账号创建
2.图像特效应用
3.开发环境python3.7+pycharm
首先要注册一个百度智能云账号,并创建这个图像特效应用
三、操作流程
3.1 阅读官方文档
当我们要使用一个我们不太了解的东西时,阅读官方文档无疑是最重要的,官方文档一般都写的特别详细,对每一个功能描述的很细节,我们先来看一下
而且这里有案例,这里我使用的是python
3.2 开始实现鉴权
因为调用这么个接口api要进行鉴权,就是官方文档说得到access_token,如何鉴权呢?
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:
上述代码打印结果有很多,阅读官网文档得知,我们这里只需要得到access_token就OK了
修改上述代码以获取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 人像动漫化实现
正片开始
修改代码
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()
这时可以得到一系列的返回值
我们这里只要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))
保存到本地
看一下对比
呃呃呃,这。。。。还好吧,哈哈哈
四、完整代码如下
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()
五、还能这么玩?
厉害了,还能加口罩,试一下
修改代码
params =
{
"image":image,"type":'anime_mask',"mask_id":1
}
#mask_id 1-8的整数,就用个1吧
看一下效果
啧啧啧
来源:https://blog.csdn.net/qq_44862120/article/details/115869644
标签:python,人像动漫化,百度智能云


猜你喜欢
Python 多进程和数据传递的理解
2021-06-01 02:30:07
ASP连接MSSQL2005 数据库
2009-03-08 19:20:00
详谈vue中router-link和传统a链接的区别
2024-04-09 10:46:05
python类中的self和变量用法及说明
2022-05-27 10:33:12
Python实现将绝对URL替换成相对URL的方法
2023-08-28 13:40:08
javascript框架设计之框架分类及主要功能
2024-04-18 09:33:40
MySQL 线上日志库迁移实例
2024-01-25 12:40:22

mybatis统计每条SQL的执行时间的方法示例
2024-01-28 12:54:43
基于python3实现倒叙字符串
2023-02-02 17:41:28

matplotlib事件处理基础(事件绑定、事件属性)
2023-02-02 19:34:32
python计算机视觉opencv图像金字塔轮廓及模板匹配
2021-10-10 09:15:33

python使用cookielib库示例分享
2022-09-22 13:53:37
python中的 zip函数详解及用法举例
2023-04-16 15:31:36
解析PyCharm集成GitLab代码仓的问题
2022-06-08 07:36:15

Golang json 库中的RawMessage功能原理
2024-04-30 10:07:27
树型结构列出指定目录里所有文件的PHP类
2023-11-17 04:49:04
MySQL创建高性能索引的全步骤
2024-01-26 16:56:58

Python读取表格类型文件代码实例
2023-08-24 22:04:16
Ubuntu下mysql安装和操作图文教程
2024-01-25 15:53:30

python根据用户需求输入想爬取的内容及页数爬取图片方法详解
2022-12-21 16:31:19
