zhanganyi88 commited on
Commit
a763964
·
verified ·
1 Parent(s): ab4f73d

Add padding in readme

Browse files
Files changed (1) hide show
  1. README.md +22 -1
README.md CHANGED
@@ -17,14 +17,35 @@ The example below demonstrates how to obtain an image embedding with the [AutoMo
17
  ```python
18
  import torch
19
  import cv2
 
20
  from torchvision import transforms
21
  from transformers import AutoImageProcessor, AutoModel
22
- from transformers.image_utils import load_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  image_path = "your local image path"
25
  dim = 384
26
  image = cv2.imread(image_path)
27
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
28
  image = cv2.resize(image, (dim, dim))
29
  transform = transforms.ToTensor()
30
  image = transform(image)
 
17
  ```python
18
  import torch
19
  import cv2
20
+ import numpy as np
21
  from torchvision import transforms
22
  from transformers import AutoImageProcessor, AutoModel
23
+
24
+ def padding(img):
25
+ """
26
+ :param img: take the image as input, np.uint8 format, [0-255] range
27
+ :return: img: square image with white pixel padded along the shorter side.
28
+ """
29
+ h, w, _ = img.shape
30
+ if h > w:
31
+ new_img = 255 * np.ones((h, h, 3)).astype(np.uint8)
32
+ start_w = int((h-w)/2)
33
+ new_img[:, start_w:start_w+w, :] = img
34
+ return new_img
35
+
36
+ elif h < w:
37
+ new_img = 255 * np.ones((w, w, 3)).astype(np.uint8)
38
+ start_h = int((w - h) / 2)
39
+ new_img[start_h:start_h + h, :, :] = img
40
+ return new_img
41
+ else:
42
+ return img
43
 
44
  image_path = "your local image path"
45
  dim = 384
46
  image = cv2.imread(image_path)
47
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
48
+ image = padding(image)
49
  image = cv2.resize(image, (dim, dim))
50
  transform = transforms.ToTensor()
51
  image = transform(image)