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,人像动漫化,百度智能云
0
投稿
猜你喜欢
Python中super().__init__()测试以及理解
2023-08-25 02:22:04
详解Python中while无限迭代循环方法
2022-08-17 12:53:48
用AspJpeg调整文字水印透明,生成图片水印的效果
2008-12-29 19:43:00
python中内置函数ord()返回字符串的ASCII数值实例详解
2023-09-05 07:07:26
javascript过滤数组重复元素的实现方法
2023-09-08 00:41:21
分析与比较五种MySQL数据库可靠性方案
2009-07-30 08:25:00
Instr函数与InstrRev函数的区别
2008-07-07 16:53:00
手机验证设计感悟
2011-01-20 19:55:00
asp事务处理的另外一个方法
2010-05-27 12:18:00
Python使用Asyncio进行web编程方法详解
2022-08-11 17:03:56
Python 列表去重去除空字符的例子
2021-07-11 02:49:14
动态提示的select下拉框
2007-12-02 14:54:00
oracle 触发器 学习笔记
2009-05-24 19:57:00
解析array splice的移除数组中指定键的值,返回一个新的数组
2023-11-18 09:51:28
淘宝首页代码调整
2011-04-22 12:44:00
在asp中通过vbs类实现rsa加密与解密
2010-05-27 12:18:00
浅谈Python实现2种文件复制的方法
2021-09-24 00:25:02
Python随手笔记之标准类型内建函数
2022-10-23 11:00:45
CSS3 + HTML5 实现未来 Web 设计
2010-01-25 12:17:00
数据库性能优化之冗余字段的作用
2011-03-03 19:21:00