andy-wyx commited on
Commit
1c93d1b
·
1 Parent(s): 53a6c0e

update image padding for xai

Browse files
Files changed (1) hide show
  1. explanations.py +18 -1
explanations.py CHANGED
@@ -8,9 +8,26 @@ import numpy as np
8
  import matplotlib.pyplot as plt
9
  from inference_resnet import inference_resnet_finer, preprocess, _clever_crop
10
  from labels import lookup_140
11
- from app import preprocess_image
12
  BATCH_SIZE = 1
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def show(img, output_size,p=False, **kwargs):
15
  img = np.array(img, dtype=np.float32)
16
 
 
8
  import matplotlib.pyplot as plt
9
  from inference_resnet import inference_resnet_finer, preprocess, _clever_crop
10
  from labels import lookup_140
11
+ import cv2
12
  BATCH_SIZE = 1
13
 
14
+ def preprocess_image(image, output_size=(300, 300)):
15
+ #shape (height, width, channels)
16
+ h, w = image.shape[:2]
17
+
18
+ #padding
19
+ if h > w:
20
+ padding = (h - w) // 2
21
+ image_padded = cv2.copyMakeBorder(image, 0, 0, padding, padding, cv2.BORDER_CONSTANT, value=[0, 0, 0])
22
+ else:
23
+ padding = (w - h) // 2
24
+ image_padded = cv2.copyMakeBorder(image, padding, padding, 0, 0, cv2.BORDER_CONSTANT, value=[0, 0, 0])
25
+
26
+ # resize
27
+ image_resized = cv2.resize(image_padded, output_size, interpolation=cv2.INTER_AREA)
28
+
29
+ return image_resized
30
+
31
  def show(img, output_size,p=False, **kwargs):
32
  img = np.array(img, dtype=np.float32)
33