百度AI攻略:黑白圖像上色

【使用攻略】【評測報告】【黑白圖像上色】python

1.功能描述:json

百度智能識別黑白圖像內容並填充色彩,使黑白圖像變得鮮活,讓老照片從新煥發活力。本文針對黑白圖像上色功能開發了使用攻略,提供全套代碼,並與其餘廠商的產品進行對比評測,爲你們使用提供依據。app

2.平臺接入學習

黑白圖像上色接入網址:https://console.bce.baidu.com/ai/#/ai/imageprocess/overview/index測試

具體接入方式比較簡單,能夠參考個人另外一個帖子,這裏就不重複了:編碼

http://ai.baidu.com/forum/topic/show/943327url

3.調用攻略(Python3)及評測3d

3.1首先認證受權:rest

在開始調用任何API以前須要先進行認證受權,具體的說明請參考:code

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()

    if token_content:

        token_info = json.loads(token_content)

        token_key = token_info['access_token']

    return token_key

3.2黑白圖像上色分析接口調用:

詳細說明請參考:http://ai.baidu.com/docs#/ImageProcessing-API/27271a5c

說明的比較清晰,這裏就不重複了。

你們須要注意的是:

API訪問URL:https://aip.baidubce.com/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 = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize"

 

    # 二進制方式打開圖片文件

    f = open(filename, 'rb')

    img = base64.b64encode(f.read())

 

    params = dict()

    params['image'] = img

    params['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_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)

        #print(content)

        data = json.loads(content)

        img_str=data['image']

        save_base_image(img_str,resultfilename)

 

colourize('black1.jpg','color1.jpg')

4.功能評測:

使用百度的黑白圖像上色功能以外對不一樣的數據對圖片效果進行測試,包括人物、風景、卡通。同時使用用了algorithmia的圖像上色功能進行對比,algorithmia的網址以下:

https://demos.algorithmia.com/colorize-photos/ 它也是經過深度學習對圖片進行上色。(Use Deep Learning to Automatically Colorize Black and White Photos)

處理速度比較:

在全部的6個案例左右,百度處理速度平均2秒左右(最快1.62,最慢3.26秒),algorithmia平均15秒左右(12~18秒區間)。

上色效果比較以下, 上色後的效果都是百度在前,algorithmia在後,同時algorithmia處理的圖片右下角有一個圖標:

人物:

 

 

風景:

 

卡通:

評測結果:

速度上:在全部的6個案例左右,百度處理速度平均2秒左右(最快1.62,最慢3.26秒),algorithmia平均15秒左右(12~18秒區間),因此速度上百度明顯佔優。

處理後的效果比較,我感受百度的效果更好一些,主要體如今顏色更加天然,尤爲是在風景的處理上,感受與實際更加相符。

 

5.結果分析及建議:

百度圖片上色速度很快,效果也很不錯,你們能夠放心使用。

後續建議提供色調或者喜愛顏色等方面的參數,好比冷色調,暖色調,紅色主導等,提供更高的靈活性,方便客戶使用。

相關文章
相關標籤/搜索