用10行Python代码进行图像识别

开发 后端 深度学习
随着深度学习算法的兴起和普及,人工智能领域取得了令人瞩目的进步,特别是在计算机视觉领域。ImageAI是一个Python库,旨在帮助开发人员构建具有自包含计算机视觉功能的应用程序和系统。

 [[226981]]

随着深度学习算法的兴起和普及,人工智能领域取得了令人瞩目的进步,特别是在计算机视觉领域。21世纪的第二个十年迅速采用卷积神经网络,发明了***进的算法,大量训练数据的可用性以及高性能和高性价比计算的发明。计算机视觉中的一个关键概念是图像分类; 这是软件系统正确标记图像中主导对象的能力。

ImageAI是一个Python库,旨在帮助开发人员构建具有自包含计算机视觉功能的应用程序和系统。

1. 安装Python 3.5.1或更高版本和pip

(如果您已经安装了Python 3.5.1或更高版本,请跳过本节)

https://www.python.org/downloads/

2. 安装ImageAI依赖项

- Tensorflow 

  1. pip3 install --upgrade tensorflow 

- Numpy 

  1. pip3 install numpy 

- SciPy 

  1. pip3 install scipy 

- OpenCV 

  1. pip3 install opencv-python 

- Matplotlib 

  1. pip3 install matplotlib 

- h5py 

  1. pip3 install h5py 

- Keras 

  1. pip3 install keras 

3. 安装ImageAI库

pip3 install https://github.com/OlafenwaMoses/ImageAI/raw/master/dist/imageai-1.0.2-py3-none-any.whl

4. 下载经过ImageNet-1000数据集训练的ResNet Model文件,并将文件复制到您的python项目文件夹。

https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

5. 创建一个名称为python的文件(例如“FirstPrediction.py”),并将下面的代码写入其中。 

  1. from imageai.Prediction import ImagePrediction  
  2. import os  
  3. execution_path = os.getcwd()  
  4. prediction = ImagePrediction()  
  5. prediction.setModelTypeAsResNet()  
  6. prediction.setModelPath( execution_path + " esnet50_weights_tf_dim_ordering_tf_kernels.h5" 
  7. prediction.loadModel()  
  8. predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)  
  9. for index in range(len(predictions)):  
  10. print(predictions[index] + " : " + percentage_probabilities[index])  

sample.jpg

[[226982]]

代码结果: 

  1. sports_car : 90.61029553413391  
  2. car_wheel : 5.9294357895851135  
  3. racer : 0.9972884319722652  
  4. convertible : 0.8457873947918415  
  5. grille : 0.581052340567112  

代码说明

现在让我们分解代码,以便了解它是如何工作的。上面的代码工作如下: 

  1. from imageai.Prediction import ImagePrediction  
  2. import os  

上面的代码导入了ImageAI ImagePrediction类和python os类。 

  1. execution_path = os.getcwd() 

上面的代码创建一个变量,它保存对包含python文件(在本例中为FirstPrediction.py)和ResNet模型文件的路径的引用。 

  1. prediction = ImagePrediction()  
  2. prediction.setModelTypeAsResNet() 
  3. prediction.setModelPath(execution_path +“ resnet50_weights_tf_dim_ordering_tf_kernels.h5”)  

在上面的代码中,我们在***行创建了一个ImagePrediction()类的实例,然后通过在第二行中调用.setModelTypeAsResNet(),将预测对象的模型类型设置为ResNet ,然后设置模型路径将预测对象复制到模型文件(resnet50_weights_tf_dim_ordering_tf_kernels.h5)的路径中,并将其复制到第三行的项目文件夹文件夹中。 

  1. predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5) 

在上面的行中,我们定义了2个变量,它等于被调用来预测图像的函数,这个函数是 .predictImage()函数,我们在其中解析了图像的路径,并且还指出了我们想要的预测结果的数量有(从1到1000的值)解析result_count = 5 。所述 .predictImage()函数将返回与所述***(2级阵列的对象的预测)是预测和所述第二(阵列percentage_probabilities)是相应的百分比概率为每个预测的阵列。 

  1. for index in range(len(predictions)):  
  2. print(predictions[index] + " : " + percentage_probabilities[index])  

上述行获取中的每个对象的预测阵列,并且还获得从相应百分比概率percentage_probabilities,***打印二者的结果到控制台。

该 .predictImage()函数将在路径中的图像,也可以说明我们预计函数返回预测的数量(可选,默认为5)。ImageNet-1000数据集中有1000个项目,ResNet模型在该数据集上进行了训练,这意味着 .predictImage函数将返回1000个可能的预测值,并按其概率排列。

借助ImageAI,您可以轻松方便地将图像预测代码集成到您在python中构建的任何应用程序,网站或系统中。ImageAI库支持其他算法和模型类型,其中一些针对速度进行了优化,另一些针对精度进行了优化。借助ImageAI,我们希望支持计算机视觉的更多专业方面,包括但不限于特殊环境和特殊领域的图像识别以及自定义图像预测。 

责任编辑:庞桂玉 来源: 今日头条
相关推荐

2021-04-09 20:49:44

PythonOCR图像

2022-10-20 09:33:35

2017-09-08 13:30:32

深度学习图像识别卷积神经网络

2020-01-07 11:30:50

图像识别AI人工智能

2022-05-25 07:11:13

Python人脸识别代码

2022-10-11 23:35:28

神经网络VGGNetAlexNet

2023-11-24 09:26:29

Java图像

2022-10-19 07:42:41

图像识别神经网络

2016-12-01 14:23:32

iosandroid

2023-11-30 09:55:27

鸿蒙邻分类器

2019-06-10 00:45:01

谷歌开源图像识别

2021-07-22 08:16:02

人工智能AI

2014-01-14 17:43:37

NEC图像识别

2015-12-03 16:01:18

Google人像识别API

2020-04-24 12:16:48

Python 图像分类实战

2023-09-25 10:13:59

Java识别

2022-09-09 14:42:17

应用开发ETS

2015-07-29 14:26:52

Win10

2017-10-17 13:30:32

Python人脸识别

2019-11-20 12:30:21

Python编程语言语音识别
点赞
收藏

51CTO技术栈公众号