百度AI攻略:图像风格转换

1.功能描述:

将图像转换成卡通画或素描风格,可用于开展趣味活动或集成到美图应用

2.平台接入

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

3.调用攻略(Python3)及评测

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

http://ai.baidu.com/docs#/Auth/top

具体Python3代码如下:

# -*- 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。期待未来新的风格上线。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值