akhaliq HF Staff commited on
Commit
a7f13a4
·
1 Parent(s): 4b13559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -9,6 +9,34 @@ from matplotlib.colors import hsv_to_rgb
9
  import cv2
10
  import gradio as gr
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  preprocess = transforms.Compose([
13
  transforms.ToTensor(),
14
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
 
9
  import cv2
10
  import gradio as gr
11
 
12
+
13
+ def preprocess(image):
14
+ # Resize
15
+ ratio = 800.0 / min(image.size[0], image.size[1])
16
+ image = image.resize((int(ratio * image.size[0]), int(ratio * image.size[1])), Image.BILINEAR)
17
+
18
+ # Convert to BGR
19
+ image = np.array(image)[:, :, [2, 1, 0]].astype('float32')
20
+
21
+ # HWC -> CHW
22
+ image = np.transpose(image, [2, 0, 1])
23
+
24
+ # Normalize
25
+ mean_vec = np.array([102.9801, 115.9465, 122.7717])
26
+ for i in range(image.shape[0]):
27
+ image[i, :, :] = image[i, :, :] - mean_vec[i]
28
+
29
+ # Pad to be divisible of 32
30
+ import math
31
+ padded_h = int(math.ceil(image.shape[1] / 32) * 32)
32
+ padded_w = int(math.ceil(image.shape[2] / 32) * 32)
33
+
34
+ padded_image = np.zeros((3, padded_h, padded_w), dtype=np.float32)
35
+ padded_image[:, :image.shape[1], :image.shape[2]] = image
36
+ image = padded_image
37
+
38
+ return image
39
+
40
  preprocess = transforms.Compose([
41
  transforms.ToTensor(),
42
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),