網(wǎng)站首頁 編程語言 正文
背景簡(jiǎn)介
ImageAI是一個(gè)面向計(jì)算機(jī)視覺編程的Python庫,支持最先進(jìn)的機(jī)器學(xué)習(xí)算法。主要圖像預(yù)測(cè),物體檢測(cè),視頻對(duì)象檢測(cè)與跟蹤等多個(gè)應(yīng)用領(lǐng)域。利用ImageAI,開發(fā)人員可用很少的代碼構(gòu)建出具有包含深度學(xué)習(xí)和計(jì)算機(jī)視覺功能的應(yīng)用系統(tǒng)。
ImageAI目前支持在ImageNet數(shù)據(jù)集上對(duì)多種不同機(jī)器算法進(jìn)行圖像預(yù)測(cè)和訓(xùn)練,ImageNet數(shù)據(jù)集項(xiàng)目始于2006年,它是一項(xiàng)持續(xù)的研究工作,旨在為世界各地的研究人員提供易于訪問的圖像數(shù)據(jù)庫。?
圖像預(yù)測(cè)
算法引入
圖像預(yù)測(cè)(Image Prediction)是指利用由各種不同算法構(gòu)建而成的預(yù)測(cè)器對(duì)輸入圖像或視頻幀進(jìn)行分析解構(gòu),并返回其中所包含的物體對(duì)象名及其相應(yīng)的百分比概率(Percentage Probabilities)的過程。
ImageAI提供了4種不同算法模型進(jìn)行圖像預(yù)測(cè),并在ImageNet數(shù)據(jù)集上進(jìn)行了訓(xùn)練。4種算法模型分別如下:
(1)由F.N.Iandola團(tuán)隊(duì)提出了SqueezeNet(預(yù)測(cè)速度最快,正確率中等)。
(2)由Microsoft公司提供的ResNet50(預(yù)測(cè)速度快,正確率較高)。
(3)由Google公司提供的InceptionV3(預(yù)測(cè)速度較慢,正確率高)。
(4)由Facebook公司提供的DenseNet121(預(yù)測(cè)速度最慢,正確率最高)。
ImageAI可對(duì)一幅圖像或者多幅圖像進(jìn)行預(yù)測(cè)。下面我們將分別用兩個(gè)簡(jiǎn)單的示例來進(jìn)行解釋和演示。
單圖像預(yù)測(cè)
單圖像預(yù)測(cè)主要是用到ImageAI中imagePrediction類中的predictImage()方法,其主要過程如下:
(1)定義一個(gè)imagePrediction()的實(shí)例。
(2)通過setMoTypeAsResNet()設(shè)置模型類型以及通過setModePath()設(shè)置模型路徑。
(3) 調(diào)用loadModel()函數(shù)模型載入模型。
(4) 利用predictImage()函數(shù)進(jìn)行預(yù)測(cè)。該函數(shù)有兩個(gè)參數(shù),一個(gè)參數(shù)用于指定要進(jìn)行預(yù)測(cè)的文件,另一個(gè)參數(shù)result_count則用于設(shè)置我們想要預(yù)測(cè)結(jié)果的數(shù)量(該參數(shù)的值1~100可選)。函數(shù)將返回預(yù)測(cè)的對(duì)象名及其相應(yīng)的百分比概率。
在以下示例中,我們將預(yù)測(cè)對(duì)象模型類型設(shè)置為ResNet,當(dāng)然,我們也可以用其他的上幾篇的算法進(jìn)行圖像預(yù)測(cè)?;贗mageAI的單圖像預(yù)測(cè)的示例代碼:
from imageai.Prediction import ImagePrediction
import os
import time
#開始計(jì)時(shí)
start_time=time.time()
execution_path=os.getcwd()
#對(duì)ImagePrediction類進(jìn)行實(shí)例化
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)
多圖像檢測(cè)
對(duì)于多圖像檢測(cè),我們可以通過多次調(diào)用predictImage()函數(shù)的方式來進(jìn)行。而更簡(jiǎn)單的方法時(shí)一次性調(diào)用predicMultipleImages()。其主要工作流程為:
(1)定義一個(gè)ImagePrediction()的實(shí)例。
(2)通過setModelTypeAsResNet()設(shè)置模型類型以及通過setModelPath()設(shè)置模型路徑。
(3)調(diào)用loadModel()函數(shù)載入模型。
(4)創(chuàng)建一個(gè)數(shù)組并將所有要預(yù)測(cè)的圖像的路徑添加到數(shù)組。
(5)通過調(diào)用predictMultiple Images()函數(shù)解析包含圖像路徑的數(shù)組并執(zhí)行圖像預(yù)測(cè),通過分析result_count_per_image(默認(rèn)值為2)的值來設(shè)定每個(gè)圖像需要預(yù)測(cè)多少種可能。
#多圖像預(yù)測(cè)
from image.Prediction import ImagePrediction
import os
execution_path=os.getcwd()
#初始化預(yù)測(cè)器
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('-----------')
目標(biāo)檢測(cè)
ImageAI提供了非常方便和強(qiáng)大的方法來對(duì)圖像執(zhí)行對(duì)象檢測(cè)并從中提取每個(gè)識(shí)別出的對(duì)象。
圖像目標(biāo)檢測(cè)
基于ImageAI的圖像目標(biāo)檢測(cè)主要是用到了ObjectDetection類中的detectObjectFromImage()方法。
示例代碼:
#目標(biāo)檢測(cè)
from imageai.Detection import ObjectDetection
import os
import time
start_time=time.time()
#execution_path=os.getcwd()#獲取當(dāng)前目錄
detector=ObjectDetection() #實(shí)例化一個(gè)ObjectDetection類
detector.setModelTypeAsRetinaNet() #設(shè)置算法模型類型為RetinaNet
#etector.setModelPath()
detector.loadModel() #加載模型
#圖像目標(biāo)檢測(cè),百分比概率閾值設(shè)置為30可檢測(cè)出更多的物體(默認(rèn)值為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)
視頻目標(biāo)檢測(cè)
視頻目標(biāo)檢測(cè)應(yīng)用范圍非常廣泛,包括動(dòng)態(tài)目標(biāo)跟蹤,自動(dòng)無人體步態(tài)識(shí)別等各種場(chǎng)景,由于視頻中包含大量的時(shí)間和空間冗余信息,對(duì)視頻中的目標(biāo)檢測(cè)是非常消耗硬件資源的,所以博主建議使用安裝了GPU硬件和CPU版的tensorflow深度學(xué)習(xí)框架的硬件設(shè)備來執(zhí)行相關(guān)任務(wù),而在CPU設(shè)備上進(jìn)行視頻目標(biāo)檢測(cè)會(huì)很慢。
視頻目標(biāo)檢測(cè)需要用到ImageAI中VideoObjectDetection類的detectObjectsFromVideo()方法。
示例代碼如下:
#視頻目標(biāo)檢測(cè)
from imageai.Detection import VideoObjectDetection
import os
import time
start_time=time.time()
detector=VideoObjectDetection() #初始化視頻檢測(cè)類
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)
原文鏈接:https://blog.csdn.net/qq_59931372/article/details/128759728
相關(guān)推薦
- 2022-05-24 python中對(duì)列表的相關(guān)操作你知道嗎_python
- 2022-05-16 C++STL之vector模板類詳解_C 語言
- 2022-09-03 Go語言中的變量和常量_Golang
- 2022-04-04 scrapy框架中用ssh連接遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)_python
- 2022-05-31 Python中的?if?語句及使用方法_python
- 2023-08-30 Linux下查找和刪除7天以前的文件
- 2022-09-26 ASP.NET?Core?6最小API中使用日志和DI示例詳解_ASP.NET
- 2021-10-25 C語言編寫漢諾塔游戲_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支