百度AI攻略:图像去雾

图像去雾:对浓雾天气下拍摄,导致细节无法辨认的图像进行去雾处理,还原更清晰真实的图像

调用攻略(Python3)

首先认证授权:

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

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

获取Access Token

向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:

grant_type:?必须参数,固定为client_credentials;

client_id:?必须参数,应用的API Key;

client_secret:?必须参数,应用的Secret Key;

例如:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&

具体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')

功能评测:

选用不同的数据对图片去雾的效果进行测试,具体效果如下:

1240

1240

1240

1240

整体感觉效果很不错。

应用前景:

图片去雾有很广阔的应用前景,包括:

视频监控,在安防监控/车载系统场景下,对受浓雾天气影响拍摄的视频/图像进行优化处理,重建更可辨析的监控材料。

在图片处理过程中,作为预处理,提高后续图片分析的准确性。


猜你喜欢

转载自blog.51cto.com/14664861/2467403
今日推荐