百度AI圖像處理—圖像主體識別調用教程(基於Python3-附Demo)

首先來看一下識別的效果:這裏需要完整代碼以及SDK的請點擊此處下載:百度AI圖像主體識別Demo

首先需要註冊百度賬號並且創建對應的應用,這裏具體方法如圖:

訪問:http://ai.baidu.com/  點擊控制檯

登錄後創建應用:

此處注意:圖像識別中的各項功能共用的是一個SDK包,只是不同功能實現的時候使用的函數以及返回參數不同,點擊完創建應用後就可以生成三個我們後期識別過程中必須使用的參數:AppID,API Key和secert key,這裏我們可以點擊查看應用詳情來獲取

至此,前期的準備工作就完成了,這時我們通過Pip或者官網直接下載SDK包,pip下載指令爲:

這裏支持Python版本:2.7.+ ,3.+

如果已安裝pip,執行pip install baidu-aip即可。
如果已安裝setuptools,執行python setup.py install即可。

接下來,在下載的SDK文檔下新建Python文件,當然你也可以使用導入包的模式:

然後創建一個AipImageClassify(亦可以簡單的理解爲一個和百度的一個連接),這裏代碼爲:

from aip import AipImageClassify

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

以上的三個參數即爲我們開始調用的時候所獲得的三個值,這裏需要填入操作,即可,緊接着調用我們需要使用的具體函數就可以了(比如圖像主體識別功能)

""" 讀取圖片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')

""" 調用圖像主體檢測 """
client.objectDetect(image);

""" 如果有可選參數 """
options = {}
options["with_face"] = 0

""" 帶參數調用圖像主體檢測 """
client.objectDetect(image, options)

 修改圖片路徑後我們就可以實現圖像主體的識別功能了,但是百度這個SDK比較麻煩的就是他的返回值是隨機排序的,有時是

{'log_id': 7346640957855264074, 'result': {'width': 382, 'height': 299, 'top': 50, 'left': 178}}

有時又是這樣的:

{'result': {'height': 299, 'width': 382, 'left': 178, 'top': 50}, 'log_id': 2876165888495195498}

這時對於我們獲取某個數值就帶來了一定的麻煩,這裏筆者採用一種字符串切割的方法來解決這一問題,解決完成後分別將,left,top,width和height三個數值保存到文件中(或者直接使用),這裏附上完整的程序代碼,如果你有需要也可以在文章的開頭出下載整個示例Demo,填入您的AppID等信息後即可實現:

import json
from aip import AipImageClassify
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

""" 讀取圖片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('1.bmp')

""" 調用圖像主體檢測 """
client.objectDetect(image);

""" 如果有可選參數 """
options = {}
options["with_face"] = 0

""" 帶參數調用圖像主體檢測 """
result=client.objectDetect(image, options)

print(result)
result_txt=json.dumps(result,ensure_ascii=False)

"""left切割寫文件"""
left_cut = 'left'
left_position = result_txt.index(left_cut)
left_value=result_txt[left_position+7:left_position+13]
left_space_position=left_value.find(',')
left_value_compare=left_value[0:left_space_position]
left_sign_position=left_value_compare.find('}')
if left_sign_position == -1:
    left_value = left_value[0:left_space_position]
else:
    left_value = left_value[0:left_space_position-1]
file = open('left.ini', 'w')  # 打開文件
file.write(left_value)  # 寫入
file.close()  # 關閉文件
left_value='left:'+left_value
print(left_value)

"""width切割寫文件"""
width_cut = 'width'
width_position = result_txt.index(width_cut)
width_value=result_txt[width_position+8:width_position+13]
width_space_position=width_value.find(',')
width_value_compare=width_value[0:width_space_position]
width_sign_position=width_value_compare.find('}')
if width_sign_position == -1:
    width_value = width_value[0:width_space_position]
else:
    width_value = width_value[0:width_space_position-1]
file = open('width.ini', 'w')  # 打開文件
file.write(width_value)  # 寫入
file.close()  # 關閉文件
width_value='width:'+width_value
print(width_value)


"""top切割寫文件"""
top_cut = 'top'
top_position = result_txt.index(top_cut)
top_value=result_txt[top_position+6:top_position+13]
top_space_position=top_value.find(',')
top_value_compare=top_value[0:top_space_position]
top_sign_position=top_value_compare.find('}')
if top_sign_position == -1:
    top_value = top_value[0:top_space_position]
else:
    top_value = top_value[0:top_space_position-1]
file = open('top.ini', 'w')  # 打開文件
file.write(top_value)  # 寫入
file.close()  # 關閉文件
top_value='top:'+top_value
print(top_value)


"""height切割寫文件"""
height_cut = 'height'
height_position = result_txt.index(height_cut)
height_value=result_txt[height_position+9:height_position+13]
height_space_position=height_value.find(',')
height_value_compare=height_value[0:height_space_position]
height_sign_position=height_value_compare.find('}')
if height_sign_position == -1:
    height_value = height_value[0:height_space_position]
else:
    height_value = height_value[0:height_space_position-1]
file = open('height.ini', 'w')  # 打開文件
file.write(height_value)  # 寫入
file.close()  # 關閉文件
height_value='height:'+height_value
print(height_value)


file = open('test.txt','w')			# 打開文件
file.write(result_txt)			# 寫入
file.close()			    		# 關閉文件

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章