網站首頁 編程語言 正文
背景簡介
ImageAI是一個面向計算機視覺編程的Python庫,支持最先進的機器學習算法。主要圖像預測,物體檢測,視頻對象檢測與跟蹤等多個應用領域。利用ImageAI,開發人員可用很少的代碼構建出具有包含深度學習和計算機視覺功能的應用系統。
ImageAI目前支持在ImageNet數據集上對多種不同機器算法進行圖像預測和訓練,ImageNet數據集項目始于2006年,它是一項持續的研究工作,旨在為世界各地的研究人員提供易于訪問的圖像數據庫。?
圖像預測
算法引入
圖像預測(Image Prediction)是指利用由各種不同算法構建而成的預測器對輸入圖像或視頻幀進行分析解構,并返回其中所包含的物體對象名及其相應的百分比概率(Percentage Probabilities)的過程。
ImageAI提供了4種不同算法模型進行圖像預測,并在ImageNet數據集上進行了訓練。4種算法模型分別如下:
(1)由F.N.Iandola團隊提出了SqueezeNet(預測速度最快,正確率中等)。
(2)由Microsoft公司提供的ResNet50(預測速度快,正確率較高)。
(3)由Google公司提供的InceptionV3(預測速度較慢,正確率高)。
(4)由Facebook公司提供的DenseNet121(預測速度最慢,正確率最高)。
ImageAI可對一幅圖像或者多幅圖像進行預測。下面我們將分別用兩個簡單的示例來進行解釋和演示。
單圖像預測
單圖像預測主要是用到ImageAI中imagePrediction類中的predictImage()方法,其主要過程如下:
(1)定義一個imagePrediction()的實例。
(2)通過setMoTypeAsResNet()設置模型類型以及通過setModePath()設置模型路徑。
(3) 調用loadModel()函數模型載入模型。
(4) 利用predictImage()函數進行預測。該函數有兩個參數,一個參數用于指定要進行預測的文件,另一個參數result_count則用于設置我們想要預測結果的數量(該參數的值1~100可選)。函數將返回預測的對象名及其相應的百分比概率。
在以下示例中,我們將預測對象模型類型設置為ResNet,當然,我們也可以用其他的上幾篇的算法進行圖像預測。基于ImageAI的單圖像預測的示例代碼:
from imageai.Prediction import ImagePrediction
import os
import time
#開始計時
start_time=time.time()
execution_path=os.getcwd()
#對ImagePrediction類進行實例化
prediction=ImagePrediction()
#設置算法模型類型
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)
多圖像檢測
對于多圖像檢測,我們可以通過多次調用predictImage()函數的方式來進行。而更簡單的方法時一次性調用predicMultipleImages()。其主要工作流程為:
(1)定義一個ImagePrediction()的實例。
(2)通過setModelTypeAsResNet()設置模型類型以及通過setModelPath()設置模型路徑。
(3)調用loadModel()函數載入模型。
(4)創建一個數組并將所有要預測的圖像的路徑添加到數組。
(5)通過調用predictMultiple Images()函數解析包含圖像路徑的數組并執行圖像預測,通過分析result_count_per_image(默認值為2)的值來設定每個圖像需要預測多少種可能。
#多圖像預測
from image.Prediction import ImagePrediction
import os
execution_path=os.getcwd()
#初始化預測器
multiple_prediction=ImagePrediction()
multiple_prediction.setModelTypeAsResNet()
#設置模型文件路徑
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提供了非常方便和強大的方法來對圖像執行對象檢測并從中提取每個識別出的對象。
圖像目標檢測
基于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() #設置算法模型類型為RetinaNet
#etector.setModelPath()
detector.loadModel() #加載模型
#圖像目標檢測,百分比概率閾值設置為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)
視頻目標檢測
視頻目標檢測應用范圍非常廣泛,包括動態目標跟蹤,自動無人體步態識別等各種場景,由于視頻中包含大量的時間和空間冗余信息,對視頻中的目標檢測是非常消耗硬件資源的,所以博主建議使用安裝了GPU硬件和CPU版的tensorflow深度學習框架的硬件設備來執行相關任務,而在CPU設備上進行視頻目標檢測會很慢。
視頻目標檢測需要用到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)
原文鏈接:https://blog.csdn.net/qq_59931372/article/details/128759728
相關推薦
- 2022-02-28 ./node_modules/taro-ui/dist/weapp/index.ts Module
- 2022-10-28 一文帶你了解Python列表生成式應用的八重境界_python
- 2022-07-06 Android中ViewFlipper和AdapterViewFlipper使用的方法實例_Andr
- 2022-07-11 UVM中uvm_config_db非直線的設置與獲取
- 2022-04-27 Python腳本后臺運行的五種方式_python
- 2022-05-21 python?判斷文件或文件夾是否存在_python
- 2022-06-09 Nginx速查手冊及常見問題_nginx
- 2022-04-14 C#可變參數params示例詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支