4
4
from cv2 import dnn
5
5
import timeit
6
6
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
-
14
7
def timeit_forward (net ):
15
- print ("OpenCL:" , cv2 .ocl .useOpenCL ())
16
8
print ("Runtime:" , timeit .timeit (lambda : net .forward (), number = 10 ))
17
9
18
10
def get_class_list ():
19
11
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 ]
21
13
22
- blob = prepare_image (cv2 .imread ('space_shuttle.jpg' ))
14
+ blob = dnn . blobFromImage (cv2 .imread ('space_shuttle.jpg' ), 1 , ( 224 , 224 ), ( 104 , 117 , 123 ))
23
15
print ("Input:" , blob .shape , blob .dtype )
24
16
25
- cv2 .ocl .setUseOpenCL (True ) #Disable OCL if you want
26
17
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 ()
29
20
#timeit_forward(net) #Uncomment to check performance
30
21
31
- prob = net .getBlob ("prob" )
32
22
print ("Output:" , prob .shape , prob .dtype )
33
23
classes = get_class_list ()
34
24
print ("Best match" , classes [prob .argmax ()])
0 commit comments