- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Python中如何實(shí)現圖像識別
Python中如何實(shí)現圖像識別,針對這個(gè)問(wèn)題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
(如果您已經(jīng)安裝了Python 3.5.1或更高版本,請跳過(guò)本節)
https://www.python.org/downloads/
- Tensorflow
pip3 install --upgrade tensorflow
- Numpy
pip3 install numpy
- SciPy
pip3 install scipy
- OpenCV
pip3 install opencv-python
- Matplotlib
pip3 install matplotlib
- h6py
pip3 install h6py
- Keras
pip3 install keras
pip3 install https://github.com/OlafenwaMoses/ImageAI/raw/master/dist/imageai-1.0.2-py3-none-any.whl
https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h6
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( execution_path + " esnet50_weights_tf_dim_ordering_tf_kernels.h6") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5) for index in range(len(predictions)): print(predictions[index] + " : " + percentage_probabilities[index])
sample.jpg
代碼結果:
sports_car : 90.61029553413391 car_wheel : 5.9294357895851135 racer : 0.9972884319722652 convertible : 0.8457873947918415 grille : 0.581052340567112
現在讓我們分解代碼,以便了解它是如何工作的。上面的代碼工作如下:
from imageai.Prediction import ImagePrediction import os
上面的代碼導入了ImageAI ImagePrediction類(lèi)和python os類(lèi)。
execution_path = os.getcwd()
上面的代碼創(chuàng )建一個(gè)變量,它保存對包含python文件(在本例中為FirstPrediction.py)和ResNet模型文件的路徑的引用。
prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(execution_path +“ resnet50_weights_tf_dim_ordering_tf_kernels.h6”)
在上面的代碼中,我們在***行創(chuàng )建了一個(gè)ImagePrediction()類(lèi)的實(shí)例,然后通過(guò)在第二行中調用.setModelTypeAsResNet(),將預測對象的模型類(lèi)型設置為ResNet ,然后設置模型路徑將預測對象復制到模型文件(resnet50_weights_tf_dim_ordering_tf_kernels.h6)的路徑中,并將其復制到第三行的項目文件夾文件夾中。
predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)
在上面的行中,我們定義了2個(gè)變量,它等于被調用來(lái)預測圖像的函數,這個(gè)函數是 .predictImage()函數,我們在其中解析了圖像的路徑,并且還指出了我們想要的預測結果的數量有(從1到1000的值)解析result_count = 5 。所述 .predictImage()函數將返回與所述***(2級陣列的對象的預測)是預測和所述第二(陣列percentage_probabilities)是相應的百分比概率為每個(gè)預測的陣列。
for index in range(len(predictions)): print(predictions[index] + " : " + percentage_probabilities[index])
上述行獲取中的每個(gè)對象的預測陣列,并且還獲得從相應百分比概率percentage_probabilities,***打印二者的結果到控制臺。
該 .predictImage()函數將在路徑中的圖像,也可以說(shuō)明我們預計函數返回預測的數量(可選,默認為5)。ImageNet-1000數據集中有1000個(gè)項目,ResNet模型在該數據集上進(jìn)行了訓練,這意味著(zhù) .predictImage函數將返回1000個(gè)可能的預測值,并按其概率排列。
借助ImageAI,您可以輕松方便地將圖像預測代碼集成到您在python中構建的任何應用程序,網(wǎng)站或系統中。ImageAI庫支持其他算法和模型類(lèi)型,其中一些針對速度進(jìn)行了優(yōu)化,另一些針對精度進(jìn)行了優(yōu)化。借助ImageAI,我們希望支持計算機視覺(jué)的更多專(zhuān)業(yè)方面,包括但不限于特殊環(huán)境和特殊領(lǐng)域的圖像識別以及自定義圖像預測。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站