OpenCV3.4.1运行官方例程出错

#OpenCV3.4.1运行官方例程出错#

错误

运行segmentation.py等文件出现以下错误

原因

Unite deep learning samples #10979

之前我下载的源码是opencv-master,为了对应版本,下载opencv-3.4.1版本的源码,运行googlenet_python.py结果如下:

from __future__ import print_function
import numpy as np
import cv2 as cv
from cv2 import dnn
import timeit

def timeit_forward(net):
    print("Runtime:", timeit.timeit(lambda: net.forward(), number=10))

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

blob = dnn.blobFromImage(cv.imread('space_shuttle.jpg'), 1, (224, 224), (104, 117, 123), False)
print("Input:", blob.shape, blob.dtype)

net = dnn.readNetFromCaffe('bvlc_googlenet.prototxt', 'bvlc_googlenet.caffemodel')
net.setInput(blob)
prob = net.forward()
#timeit_forward(net)        #Uncomment to check performance

print("Output:", prob.shape, prob.dtype)
classes = get_class_list()
print("Best match", classes[prob.argmax()])

无对应的文件,修改文件路径,并下载bvlc_googlenet.caffemodel模型

ps:

图片为

bvlc_googlenet.caffemodel模型下载地址

from __future__ import print_function
import numpy as np
import cv2 as cv
from cv2 import dnn
import timeit

def timeit_forward(net):
    print("Runtime:", timeit.timeit(lambda: net.forward(), number=10))

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

blob = dnn.blobFromImage(cv.imread('../data/dnn/space_shuttle.jpg'), 1, (224, 224), (104, 117, 123), False)
print("Input:", blob.shape, blob.dtype)

net = dnn.readNetFromCaffe('../data/dnn/bvlc_googlenet.prototxt', '../data/dnn/bvlc_googlenet.caffemodel')
net.setInput(blob)
prob = net.forward()
#timeit_forward(net)        #Uncomment to check performance

print("Output:", prob.shape, prob.dtype)
classes = get_class_list()
print("Best match", classes[prob.argmax()])

运行结果如下:

输出为

Best match space shuttle

预测结果正确

转载于:https://www.cnblogs.com/HuangJiaoZhu/p/9383487.html

你可能感兴趣的:(OpenCV3.4.1运行官方例程出错)