使用OpenCV_python中的DNN调用CaffeModel识别图像只需30行代码

OpenCV3.3开始正式支持DNN,有C++,Python的Sample,只要下载CaffeModel,识别图像的代码非常简洁。

#coding:utf-8
from __future__ import print_function
import numpy as np
import cv2
from cv2 import dnn
import sys


cm_path='caffemodel/'


def get_class_list():
    with open(cm_path+'bvlc_googlenet_synset_words_cn1.txt', 'rt') as f:
        return [x[x.find(" ") + 1:] for x in f]


def dnn_show_result(prob,classes,n):
    y = sorted(prob[0],reverse = True) #从大到小排序
    z=range(n)  #前n名
    for i in range(0, n):
        z[i]=np.where(prob[0] == y[i])[0][0]
        print(u"第",i+1,u"匹配:", classes[z[i]],end='')    
        print(u"类所在行:", z[i]+1,"  ",u"可能性:", y[i])


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print ("USAGE: googlenet_z.py images/tiger.jpg")
        sys.exit()
        fn=sys.argv[1]
    blob = dnn.blobFromImage(cv2.imread(fn), 1, (224, 224), (104, 117, 123))
    print("Input:", blob.shape, blob.dtype)


    net = dnn.readNetFromCaffe(cm_path+'bvlc_googlenet.prototxt', cm_path+'bvlc_googlenet.caffemodel')
    net.setInput(blob)
    prob = net.forward()
    print("Output:", prob.shape, prob.dtype)

    classes = get_class_list()
    dnn_show_result(prob,classes,3)



使用OpenCV_python中的DNN调用CaffeModel识别图像只需30行代码_第1张图片

使用OpenCV_python中的DNN调用CaffeModel识别图像只需30行代码_第2张图片

使用OpenCV_python中的DNN调用CaffeModel识别图像只需30行代码_第3张图片

OpenCV-Python 下载:https://pypi.python.org/pypi/opencv-python

代码下载:http://download.csdn.net/download/zw010101/10109152  (含bvlc_googlenet.caffemodel,bvlc_googlenet.prototxt,bvlc_googlenet_synset_words.txt)


你可能感兴趣的:(opencv)