TensorFlow实战 10 GoogleLeNet进阶(Python调用OpenCV库实现图像识别)

      在上一篇TensorFlow实战 9 GoogleLeNet神经网络(2014 ILSVRC 1st in Error(Top-5))文章中简单介绍GoogleLeNet的特点及V1-v4系列重要特点,通过使用slim库构建inception v3版本的神经网络,分析了前向传播的时间花费,本次借助已训练好googleLeNet网络实现不同大小尺寸图片的识别。

      文件列表:

        1)bvlc_googlenet.caffemodel:以训练好的GoogleLeNet网络;

        2)bvlc_googlenet.prototxt:网络描述文件,包括权重参数等;

        3)synset_words.txt:标签文件

      其中2)、3)分别在OpenCV文件的等dnn目录下可查找到,不同版本的OpenCV目录稍有差别,参数也不尽相同,这里(密码:x6kb)分享已经打包好的文件,供学习者使用。

        python代码实现图像分类代码:

import numpy as np
import cv2 as cv
 
# read names of classes
with open('synset_words.txt') as f:
	classes = [x[x.find(' ') + 1:] for x in f]
#read image
image = cv.imread('goldfish.jpg')
cv.imshow("image", image)
# create tensor with 224x224 spatial size and subtract mean values (104, 117, 123) 
# from corresponding channels (R, G, B)
input = cv.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))
 
# load model from caffe
net = cv.dnn.readNetFromCaffe('bvlc_googlenet.prototxt', 'bvlc_googlenet.caffemodel')
# feed input tensor to the model
net.setInput(input)
# perform inference and get output
out = net.forward() 
# get indices with the highest probability
indexes = np.argsort(out[0])[-5:]
# define font style
font = cv.FONT_HERSHEY_COMPLEX
#print the last five most likelihold result
for i in reversed(indexes):
	print('class:', classes[i], ' probability:', out[0][i])
#add txet into the result image
cv.putText(image, classes[indexes[-1]], (20, 20), font, 1.0, (0, 0, 255), 2)
cv.imshow("result", image)
#save the result image
cv.imwrite("./goldfish_res.jpg", image)
cv.waitKey(0)

  识别效果还不错,效果展示如下:

 practice makes perfect!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值