1.功能描述:python
将图像转换成卡通画或素描风格,可用于开展趣味活动或集成到美图应用json
2.平台接入app
具体接入方式比较简单,能够参考个人另外一个帖子,这里就不重复了:测试
http://ai.baidu.com/forum/topic/show/943327编码
3.调用攻略(Python3)及评测url
3.1首先认证受权:rest
在开始调用任何API以前须要先进行认证受权,具体的说明请参考:code
http://ai.baidu.com/docs#/Auth/toporm
具体Python3代码以下:blog
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】
#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
#print (token_content)
if token_content:
token_info = json.loads(token_content)
token_key = token_info['access_token']
return token_key
3.2图像风格转换分析接口调用:
详细说明请参考: https://ai.baidu.com/docs#/ImageProcessing-API/824a761a
说明的比较清晰,这里就不重复了。
你们须要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans
输入图像:base64编码后大小不超过4M,像素乘积不超过2000*2000,最短边至少50px,最长边最大4096px,长宽比3:1之内。注意:图片的base64编码是不包含图片头的,如(data:image/jpg;base64,)
支持的风格:cartoon:卡通画风格 pencil:素描风格
返回示例:
{
"log_id": "6876747463538438254",
"image": "处理后图片的Base64编码"
}
Python3调用代码以下:
def style_trans(filename,resultfilename,option):
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans"
# 二进制方式打开图片文件
f = open(filename, 'rb')
img = base64.b64encode(f.read())
params = dict()
params['image'] = img
params['option'] = option
params = urllib.parse.urlencode(params).encode("utf-8")
#params = json.dumps(params).encode('utf-8')
access_token = get_token()
begin = time.perf_counter()
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
end = time.perf_counter()
print('处理时长:'+'%.2f'%(end-begin)+'秒')
if content:
#print(content)
content=content.decode('utf-8')
#print (content)
#print(content)
data = json.loads(content)
img_str=data['image']
save_base_image(img_str,resultfilename)
style_trans('../img/pose2.jpg','../img/pose2_cartoon.jpg','cartoon')
4.功能评测:
选用不一样的数据对效果进行测试,具体效果以下(如下例子均来自网上):
先来一张植物的照片:
再看看人物的效果:
5.测试结论和建议
测试下来,总体识别效果不错,效果颇有意思。百度应该是采用的SEQ2SEQ的模型,总体作的很不错,能够用来开发不少有意思的APP。期待将来新的风格上线。