1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > access百度翻译 get_百度AI攻略:智能上色

access百度翻译 get_百度AI攻略:智能上色

时间:2022-03-26 10:46:02

相关推荐

access百度翻译 get_百度AI攻略:智能上色

1.功能描述:

想必大家家里都有很多黑白的老照片,里面有着满满的回忆。百度智能识别黑白图像内容并填充色彩,使黑白图像变得鲜活,让老照片重新焕发活力。说干就干,攻略和代码奉上。

2.平台接入

黑白图像上色接入网址:https://console./ai/#/ai/imageprocess/overview/index

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

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

3.1首先认证授权:

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

/docs#/Auth/top

具体Python3代码如下:

# -*- coding: utf-8 -*-#!/usr/bin/env pythonimport urllibimport base64import json#client_id 为官网获取的AK, client_secret 为官网获取的SKclient_id =【百度云应用的AK】client_secret =【百度云应用的SK】#获取tokendef get_token():host = '/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secretrequest = urllib.request.Request(host)request.add_header('Content-Type', 'application/json; charset=UTF-8')response = urllib.request.urlopen(request)token_content = response.read()if token_content:token_info = json.loads(token_content)token_key = token_info['access_token']return token_key

3.2黑白图像上色分析接口调用:

详细说明请参考:/docs#/ImageProcessing-API/27271a5c

说明的比较清晰,这里就不重复了。

大家需要注意的是:

API访问URL:/rest/2.0/image-process/v1/colourize

图片base64编码后大小不超过4M,最短边至少64px,最长边最大800px,长宽比3:1以内。注意:图片的base64编码是不包含图片头的。

Python3调用代码如下:

#保存图片def save_base_image(img_str,filename):img_data = base64.b64decode(img_str)with open(filename, 'wb') as f:f.write(img_data)#黑白图片上色#filename:原图片名(本地存储包括路径);resultfilename:处理后的文件保存名称def colourize(filename,resultfilename):request_url = "/rest/2.0/image-process/v1/colourize"# 二进制方式打开图片文件f = open(filename, 'rb')img = base64.b64encode(f.read())params = dict()params['image'] = imgparams['show'] = 'true'params = urllib.parse.urlencode(params).encode("utf-8")#params = json.dumps(params).encode('utf-8')access_token = get_token()request_url = request_url + "?access_token=" + access_tokenrequest = 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()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)colourize('black6.jpg','color6.jpg')

4.功能评测:

选用不同的数据对图片效果进行测试,包括人物、风景、卡通,具体效果如下:

人物:

风景:

卡通:

说实话,我被这个结果惊艳到了,感觉所有的图片处理的都非常的漂亮。这个功能真的可以用于让老照片重新焕发青春。我准备用这个程序把小时候的老照片都跑一遍。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。