温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中如何实现图像识别

发布时间:2021-07-10 16:03:52 来源:亿速云 阅读:604 作者:Leah 栏目:编程语言

Python中如何实现图像识别,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

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

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

2. 安装ImageAI依赖项

- 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

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.h6

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

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

Python中如何实现图像识别

代码结果:

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类和python os类。

execution_path = os.getcwd()

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

prediction = ImagePrediction()  prediction.setModelTypeAsResNet() prediction.setModelPath(execution_path +“ resnet50_weights_tf_dim_ordering_tf_kernels.h6”)

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

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

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

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

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

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

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

关于Python中如何实现图像识别问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI