Skip to content

Commit b8b8047

Browse files
committed
Fixed python sample for googlenet in dnn
1 parent dcf3d98 commit b8b8047

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

samples/dnn/googlenet_python.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,21 @@
44
from cv2 import dnn
55
import timeit
66

7-
def prepare_image(img):
8-
img = cv2.resize(img, (224, 224))
9-
#convert interleaved image (RGBRGB) to planar(RRGGBB)
10-
blob = np.moveaxis(img, 2, 0)
11-
blob = np.reshape(blob.astype(np.float32), (-1, 3, 224, 224))
12-
return blob
13-
147
def timeit_forward(net):
15-
print("OpenCL:", cv2.ocl.useOpenCL())
168
print("Runtime:", timeit.timeit(lambda: net.forward(), number=10))
179

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

22-
blob = prepare_image(cv2.imread('space_shuttle.jpg'))
14+
blob = dnn.blobFromImage(cv2.imread('space_shuttle.jpg'), 1, (224, 224), (104, 117, 123))
2315
print("Input:", blob.shape, blob.dtype)
2416

25-
cv2.ocl.setUseOpenCL(True) #Disable OCL if you want
2617
net = dnn.readNetFromCaffe('bvlc_googlenet.prototxt', 'bvlc_googlenet.caffemodel')
27-
net.setBlob(".data", blob)
28-
net.forward()
18+
net.setInput(blob)
19+
prob = net.forward()
2920
#timeit_forward(net) #Uncomment to check performance
3021

31-
prob = net.getBlob("prob")
3222
print("Output:", prob.shape, prob.dtype)
3323
classes = get_class_list()
3424
print("Best match", classes[prob.argmax()])

0 commit comments

Comments
 (0)