5 import skimage.transform
8 def crop_center(img,cropx,cropy):
10 startx = x//2-(cropx//2)
11 starty = y//2-(cropy//2)
12 return img[starty:starty+cropy,startx:startx+cropx]
14 def rescale(img, input_height, input_width):
17 aspect = img.shape[1]/float(img.shape[0])
21 res = int(aspect * input_height)
22 imgScaled = skimage.transform.resize(img, (input_width, res))
25 res = int(input_width/aspect)
26 imgScaled = skimage.transform.resize(img, (res, input_height))
28 imgScaled = skimage.transform.resize(img, (input_width, input_height))
33 img = skimage.img_as_float(skimage.io.imread(img)).astype(np.float32)
38 img = img.swapaxes(1, 2).swapaxes(0, 1)
43 img = img[(2, 1, 0), :, :]
46 def removeMean(img, mean):
48 img = img * 255 - mean
53 img = img[np.newaxis, :, :, :].astype(np.float32)
56 def parseResults(results):
57 results = np.asarray(results)
58 results = np.delete(results, 1)
61 arr = np.empty((0,2), dtype=object)
64 for i, r
in enumerate(results):
67 arr = np.append(arr, np.array([[i,r]]), axis=0)
73 print "Raw top 3 results:", sorted(arr, key=
lambda x: x[1], reverse=
True)[:3]
76 with open(
'inference_codes.txt',
'r') as f: for line
in f:
77 code, result = line.partition(
":")[::2]
78 if (code.strip() == str(index)):
79 answer =
"The image contains a %s with a %s percent probability." % \
80 (result.strip()[1:-2], highest*100)
84 def loadToNCHW(img, mean, input_size):
86 img = rescale(img, input_size, input_size)
87 img = crop_center(img, input_size, input_size)
90 img = removeMean(img, mean)