ZhengPeng7 commited on
Commit
327742a
1 Parent(s): 7dd89ba

Adapt the resize to cv2 version to keep consistent with that used in training.

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -15,10 +15,17 @@ config = Config()
15
  device = config.device
16
 
17
 
 
 
 
 
 
 
 
18
  class ImagePreprocessor():
19
  def __init__(self, resolution=(1024, 1024)) -> None:
20
  self.transform_image = transforms.Compose([
21
- transforms.Resize(resolution),
22
  transforms.ToTensor(),
23
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
24
  ])
@@ -43,11 +50,12 @@ model.eval()
43
  # def predict(image_1, image_2):
44
  # images = [image_1, image_2]
45
  def predict(image, resolution='1024x1024'):
 
 
46
  images = [image]
47
  image_shapes = [image.shape[:2] for image in images]
48
- images = [Image.fromarray(image) for image in images]
49
 
50
- resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
51
  image_preprocessor = ImagePreprocessor(resolution=resolution)
52
  images_proc = []
53
  for image in images:
 
15
  device = config.device
16
 
17
 
18
+
19
+ def array_to_pil_image(image, size=(1024, 1024)):
20
+ image = cv2.resize(image, size, interpolation=cv2.INTER_LINEAR)
21
+ image = Image.fromarray(image).convert('RGB')
22
+ return image
23
+
24
+
25
  class ImagePreprocessor():
26
  def __init__(self, resolution=(1024, 1024)) -> None:
27
  self.transform_image = transforms.Compose([
28
+ # transforms.Resize(resolution), # 1. keep consistent with the cv2.resize used in training 2. redundant with that in path_to_image()
29
  transforms.ToTensor(),
30
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
31
  ])
 
50
  # def predict(image_1, image_2):
51
  # images = [image_1, image_2]
52
  def predict(image, resolution='1024x1024'):
53
+ # Image is a RGB numpy array.
54
+ resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
55
  images = [image]
56
  image_shapes = [image.shape[:2] for image in images]
57
+ images = [array_to_pil_image(image, resolution) for image in images]
58
 
 
59
  image_preprocessor = ImagePreprocessor(resolution=resolution)
60
  images_proc = []
61
  for image in images: