akhaliq HF Staff commited on
Commit
c12d225
·
1 Parent(s): fd3b091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -27,19 +27,24 @@ def get_image(path):
27
  img = np.array(img.convert('RGB'))
28
  return img
29
 
 
 
30
  def preprocess(img):
31
- img = img / 255.
32
- img = cv2.resize(img, (256, 256))
33
- h, w = img.shape[0], img.shape[1]
34
- y0 = (h - 224) // 2
35
- x0 = (w - 224) // 2
36
- img = img[y0 : y0+224, x0 : x0+224, :]
37
- img = (img - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
38
- img = np.transpose(img, axes=[2, 0, 1])
39
- img = img.astype(np.float32)
 
40
  img = np.expand_dims(img, axis=0)
 
41
  return img
42
 
 
43
  def predict(path):
44
  img = get_image(path)
45
  img = preprocess(img)
 
27
  img = np.array(img.convert('RGB'))
28
  return img
29
 
30
+
31
+
32
  def preprocess(img):
33
+ '''
34
+ Preprocessing required on the images for inference with mxnet gluon
35
+ The function takes loaded image and returns processed tensor
36
+ '''
37
+ img = np.array(Image.fromarray(img).resize((224, 224))).astype(np.float32)
38
+ img[:, :, 0] -= 123.68
39
+ img[:, :, 1] -= 116.779
40
+ img[:, :, 2] -= 103.939
41
+ img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
42
+ img = img.transpose((2, 0, 1))
43
  img = np.expand_dims(img, axis=0)
44
+
45
  return img
46
 
47
+
48
  def predict(path):
49
  img = get_image(path)
50
  img = preprocess(img)