美文网首页Python新世界python热爱者
python使用opencv基于GoogLeNet 模型识别图片

python使用opencv基于GoogLeNet 模型识别图片

作者: 48e0a32026ae | 来源:发表于2018-12-22 13:54 被阅读1次

2017年8月发布的OpenCV 3.3正式带来了高度改进的深度学习dnn模块。 该模块现在持许多深度学习框架,包括Caffe,TensorFlow和Torch/PyTorch。该API可C ++可在Python中调用,很容易磁盘加载模型;预处理输入图像;通过网络传递图像并获取输出分类。

本文演示如何在ImageNet数据集上使用预先训练的深度学习网络并将其应用于输入图像。

使用OpenCV进行深度学习

我们将使用OpenCV和GoogleLeNet(在ImageNet上预先训练)来对图像进行分类。

OpenCV 3.3中的深度学习

自版本v3.1起,OpenCV的dnn模块已成为opencv_contrib库的一部分。在OpenCV 3.3中,它包含在主库中。使用OpenCV 3.3,我们可以利用流行的深度学习框架预先训练好的网络。与OpenCV 3.3兼容的流行网络架构包括:GoogleLeNet、AlexNet、SqueezeNet和VGGNet、RESNET。

参考:

https://github.com/opencv/opencv/tree/master/modules/dnn

发布文档 https://habr.com/company/intel/blog/333612/ 俄语

模型下载: https://itbooks.pipipan.com/fs/18113597-326734543

python测试开发项目实战-目录

python工具书籍下载-持续更新

python 3.7极速入门教程 - 目录

OpenCV深度学习功能和框架

OpenCV 3.3支持 Caffe, TensorFlow和 Torch/PyTorch

框架。目前不支持Keras(因为Keras实际上是TensorFlow和Theano等包装器),但是由于深度学习库的普及,Keras 直接支持只是时间问题。

使用OpenCV 3.3,我们可以使用dnn中的以下函数从磁盘加载图像:

cv2.dnn.blobFromImage

cv2.dnn.blobFromImages

我们可以通过create方法直接从各种框架导入模型:

cv2.dnn.createCaffeImporter

cv2.dnn.createTensorFlowImporter

cv2.dnn.createTorchImporter

使用read方法直接从磁盘加载序列化模型更容易:

cv2.dnn.readNetFromCaffe

cv2.dnn.readNetFromTensorFlow

cv2.dnn.readNetFromTorch

cv2.dnn.readhTorchBlob

加载模型后,.forward方法用于向前传播我们的图像并获得实际的分类。

使用深度学习和OpenCV对图像进行分类

GoogLeNet 的参考: Going deeper with convolutions

加载预先训练好的Caffe模型,并使用它来使用OpenCV对图像进行分类。

代码参见: https://github.com/china-testing/python-api-tesing/blob/master/practices/cv/deep_learning_with_opencv.py

执行示例

$ python deep_learning_with_opencv.py --image images/jemma.png

--prototxt bvlc_googlenet.prototxt

--model bvlc_googlenet.caffemodel --labels synset_words.txt

[INFO] loading model...

[INFO] classification took 0.075035 seconds

[INFO] 1. label: beagle, probability: 0.81137

[INFO] 2. label: Labrador retriever, probability: 0.031416

[INFO] 3. label: bluetick, probability: 0.023929

[INFO] 4. label: EntleBucher, probability: 0.017507

[INFO] 5. label: Greater Swiss Mountain dog, probability: 0.0144

图片.png

$ python deep_learning_with_opencv.py --image images/traffic_light.png

--prototxt bvlc_googlenet.prototxt

--model bvlc_googlenet.caffemodel --labels synset_words.txt

[INFO] loading model...

[INFO] classification took 0.080521 seconds

[INFO] 1. label: traffic light, probability: 1.0

[INFO] 2. label: pole, probability: 4.9961e-07

[INFO] 3. label: spotlight, probability: 3.4974e-08

[INFO] 4. label: street sign, probability: 3.3623e-08

[INFO] 5. label: loudspeaker, probability: 2.0235e-08

图片.png

$ python deep_learning_with_opencv.py --image images/eagle.png

--prototxt bvlc_googlenet.prototxt

--model bvlc_googlenet.caffemodel --labels synset_words.txt

[INFO] loading model...

[INFO] classification took 0.087207 seconds

[INFO] 1. label: bald eagle, probability: 0.96768

[INFO] 2. label: kite, probability: 0.031964

[INFO] 3. label: vulture, probability: 0.00023595

[INFO] 4. label: albatross, probability: 6.3653e-05

[INFO] 5. label: black grouse, probability: 1.6147e-05

图片.png

$ python deep_learning_with_opencv.py --image images/vending_machine.png

--prototxt bvlc_googlenet.prototxt

--model bvlc_googlenet.caffemodel --labels synset_words.txt

[INFO] loading model...

[INFO] classification took 0.099602 seconds

[INFO] 1. label: vending machine, probability: 0.99269

[INFO] 2. label: cash machine, probability: 0.0023691

[INFO] 3. label: pay-phone, probability: 0.00097005

[INFO] 4. label: ashcan, probability: 0.00092097

[INFO] 5. label: mailbox, probability: 0.00061188

相关文章

网友评论

    本文标题:python使用opencv基于GoogLeNet 模型识别图片

    本文链接:https://www.haomeiwen.com/subject/yjuikqtx.html