图像去雾:对浓雾天气下拍摄,致使细节没法辨认的图像进行去雾处理,还原更清晰真实的图像python
调用攻略(Python3)json
首先认证受权:app
在开始调用任何API以前须要先进行认证受权,具体的说明请参考:测试
http://ai.baidu.com/docs#/Auth/top优化
获取Access Tokenui
向受权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上如下参数:编码
grant_type:?必须参数,固定为client_credentials;url
client_id:?必须参数,应用的API Key;rest
client_secret:?必须参数,应用的Secret Key;code
例如:
具体Python3代码以下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
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()
if token_content:
token_info = json.loads(token_content)
token_key = token_info['access_token']
return token_key
图像去雾分析接口调用:
详细说明请参考:https://ai.baidu.com/docs#/ImageProcessing-API/b9ff120b
接口描述
对浓雾天气下拍摄,致使细节没法辨认的图像进行去雾处理,还原更清晰真实的图像。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/image-process/v1/dehaze
URL参数:
参数 值
access_token 经过API Key和Secret Key获取的access_token,参考”Access Token获取”
Header以下:
参数 值
Content-Type application/x-www-form-urlencoded
Body中放置请求参数,参数详情以下:
请求参数
image : base64编码后大小不超过4M,最短边至少200px,最长边最大4096px,长宽比3:1之内。注意:图片的base64编码是不包含图片头的,如(data:image/jpg;base64,)
返回说明
字段 是否必选 类型 说明
log_id 是 uint64 惟一的log id,用于问题定位
image 否 string 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:原图片名(本地存储包括路径);dehazedfilename:处理后的文件保存名称
def test_dehaze(filename,dehazedfilename):
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/dehaze"
# 二进制方式打开图片文件
f = open(filename, 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
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_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()
if content:
#print(content)
content=content.decode('utf-8')
#print(content)
data = json.loads(content)
#print(data)
img_str=data['image']
save_base_image(img_str,dehazedfilename)
test_dehaze('haze_lake_1.jpg','clear_lake_1.jpg')
功能评测:
选用不一样的数据对图片去雾的效果进行测试,具体效果以下:
总体感受效果很不错。
应用前景:
图片去雾有很广阔的应用前景,包括:
视频监控,在安防监控/车载系统场景下,对受浓雾天气影响拍摄的视频/图像进行优化处理,重建更可辨析的监控材料。
在图片处理过程当中,做为预处理,提升后续图片分析的准确性。