Python基于ImageAI實現(xiàn)圖像識別詳解
背景簡介
ImageAI是一個面向計算機視覺編程的Python庫,支持最先進的機器學習算法。主要圖像預測,物體檢測,視頻對象檢測與跟蹤等多個應(yīng)用領(lǐng)域。利用ImageAI,開發(fā)人員可用很少的代碼構(gòu)建出具有包含深度學習和計算機視覺功能的應(yīng)用系統(tǒng)。
ImageAI目前支持在ImageNet數(shù)據(jù)集上對多種不同機器算法進行圖像預測和訓練,ImageNet數(shù)據(jù)集項目始于2006年,它是一項持續(xù)的研究工作,旨在為世界各地的研究人員提供易于訪問的圖像數(shù)據(jù)庫。
圖像預測
算法引入
圖像預測(Image Prediction)是指利用由各種不同算法構(gòu)建而成的預測器對輸入圖像或視頻幀進行分析解構(gòu),并返回其中所包含的物體對象名及其相應(yīng)的百分比概率(Percentage Probabilities)的過程。
ImageAI提供了4種不同算法模型進行圖像預測,并在ImageNet數(shù)據(jù)集上進行了訓練。4種算法模型分別如下:
(1)由F.N.Iandola團隊提出了SqueezeNet(預測速度最快,正確率中等)。
(2)由Microsoft公司提供的ResNet50(預測速度快,正確率較高)。
(3)由Google公司提供的InceptionV3(預測速度較慢,正確率高)。
(4)由Facebook公司提供的DenseNet121(預測速度最慢,正確率最高)。
ImageAI可對一幅圖像或者多幅圖像進行預測。下面我們將分別用兩個簡單的示例來進行解釋和演示。
單圖像預測
單圖像預測主要是用到ImageAI中imagePrediction類中的predictImage()方法,其主要過程如下:
(1)定義一個imagePrediction()的實例。
(2)通過setMoTypeAsResNet()設(shè)置模型類型以及通過setModePath()設(shè)置模型路徑。
(3) 調(diào)用loadModel()函數(shù)模型載入模型。
(4) 利用predictImage()函數(shù)進行預測。該函數(shù)有兩個參數(shù),一個參數(shù)用于指定要進行預測的文件,另一個參數(shù)result_count則用于設(shè)置我們想要預測結(jié)果的數(shù)量(該參數(shù)的值1~100可選)。函數(shù)將返回預測的對象名及其相應(yīng)的百分比概率。
在以下示例中,我們將預測對象模型類型設(shè)置為ResNet,當然,我們也可以用其他的上幾篇的算法進行圖像預測?;贗mageAI的單圖像預測的示例代碼:
from imageai.Prediction import ImagePrediction
import os
import time
#開始計時
start_time=time.time()
execution_path=os.getcwd()
#對ImagePrediction類進行實例化
prediction=ImagePrediction()
#設(shè)置算法模型類型
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path,'resent50_weights_tf_dim_ordering_tf_kernels.h5'))
prediction.loadModel()
predictions,probabilities=prediction.predictioImage(os.path.join(execution_path,'sample.jpg'),result_count=5)
end_time=time.time()
for eachPrediction,eachProbability in zip(predictions,probabilities):
print(eachPrediction+":"+str(eachProbability))
print('Total time cost:',end_time-start_time)多圖像檢測
對于多圖像檢測,我們可以通過多次調(diào)用predictImage()函數(shù)的方式來進行。而更簡單的方法時一次性調(diào)用predicMultipleImages()。其主要工作流程為:
(1)定義一個ImagePrediction()的實例。
(2)通過setModelTypeAsResNet()設(shè)置模型類型以及通過setModelPath()設(shè)置模型路徑。
(3)調(diào)用loadModel()函數(shù)載入模型。
(4)創(chuàng)建一個數(shù)組并將所有要預測的圖像的路徑添加到數(shù)組。
(5)通過調(diào)用predictMultiple Images()函數(shù)解析包含圖像路徑的數(shù)組并執(zhí)行圖像預測,通過分析result_count_per_image(默認值為2)的值來設(shè)定每個圖像需要預測多少種可能。
#多圖像預測
from image.Prediction import ImagePrediction
import os
execution_path=os.getcwd()
#初始化預測器
multiple_prediction=ImagePrediction()
multiple_prediction.setModelTypeAsResNet()
#設(shè)置模型文件路徑
multiple_prediction.setModelPath(os.path.join(execution_path,'resent50_weights_tf_ordering_tf_kernels.h5'))
#加載模型
multiple_prediction.loadModel()
all_images_array=[]
all_files=os.listdir(execution_path)
for each_file in all_files:
if(each_file.endswith('.jpg') or each_file.endswith('.png')):
all_images_array.append(each_file)
results_array=multiple_prediction.predictMultipleImages(all_images_array,result_count_per_image=3)
for each_result in results_array:
predictions,percentage_probanlities=each_result['predictions'],each_result['percentage_probabilities']
for index in range(len(predictions)):
print(predictions[index]+':'+str(percentage_probanlities[index]))
print('-----------')目標檢測
ImageAI提供了非常方便和強大的方法來對圖像執(zhí)行對象檢測并從中提取每個識別出的對象。
圖像目標檢測
基于ImageAI的圖像目標檢測主要是用到了ObjectDetection類中的detectObjectFromImage()方法。
示例代碼:
#目標檢測
from imageai.Detection import ObjectDetection
import os
import time
start_time=time.time()
#execution_path=os.getcwd()#獲取當前目錄
detector=ObjectDetection() #實例化一個ObjectDetection類
detector.setModelTypeAsRetinaNet() #設(shè)置算法模型類型為RetinaNet
#etector.setModelPath()
detector.loadModel() #加載模型
#圖像目標檢測,百分比概率閾值設(shè)置為30可檢測出更多的物體(默認值為30)
detections=detector.detectObjectsFromImage(input_image="D:\Image\\four.jpg",output_image_path='D:\Image\\fourr.jpg',minimum_percentage_probability=30)
end_time=time.time()
for eachObject in detections:
print(eachObject['name'],":",eachObject['percentage_probability'],":",eachObject['box_points'])
print('Total Time cost:',end_time-start_time)視頻目標檢測
視頻目標檢測應(yīng)用范圍非常廣泛,包括動態(tài)目標跟蹤,自動無人體步態(tài)識別等各種場景,由于視頻中包含大量的時間和空間冗余信息,對視頻中的目標檢測是非常消耗硬件資源的,所以博主建議使用安裝了GPU硬件和CPU版的tensorflow深度學習框架的硬件設(shè)備來執(zhí)行相關(guān)任務(wù),而在CPU設(shè)備上進行視頻目標檢測會很慢。
視頻目標檢測需要用到ImageAI中VideoObjectDetection類的detectObjectsFromVideo()方法。
示例代碼如下:
#視頻目標檢測
from imageai.Detection import VideoObjectDetection
import os
import time
start_time=time.time()
detector=VideoObjectDetection() #初始化視頻檢測類
detector.setModelTypeAsRetinaNet()
#detector.setModelPath('D:\Image:\haha.mp4')
detector.loadModel() #加載模型
video_path=detector.detectObjectsFromVideo(input_file_path='D:\Image\haha.mp4',output_file_path='D:Image:\hahaha.mp4',frames_per_second=20,log_progress=True)
print(video_path)
end_time=time.time()
print('Total time cost:',end_time-start_time)以上就是Python基于ImageAI實現(xiàn)圖像識別詳解的詳細內(nèi)容,更多關(guān)于Python ImageAI圖像識別的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pytorch中的模型訓練(以CIFAR10數(shù)據(jù)集為例)
這篇文章主要介紹了pytorch中的模型訓練(以CIFAR10數(shù)據(jù)集為例),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
基于Python實現(xiàn)定時自動給微信好友發(fā)送天氣預報
這篇文章主要介紹了基于Python實現(xiàn)定時自動給微信好友發(fā)送天氣預報的實現(xiàn)代碼,,需要的朋友可以參考下2018-10-10
Python利用Selenium實現(xiàn)自動觀看學習通視頻
Selenium是一個用于Web應(yīng)用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。本文主要介紹了利用Selenium實現(xiàn)自動觀看學習通視頻,需要的同學可以參考一下2021-12-12

