Spaces:
Runtime error
Runtime error
Upload utils.py
Browse files- utils/utils.py +140 -0
utils/utils.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import math
|
4 |
+
|
5 |
+
def f(r, T=0.6, beta=0.1):
|
6 |
+
return np.where(r < T, beta + (1 - beta) / T * r, 1)
|
7 |
+
|
8 |
+
# Get the bounding box of the mask
|
9 |
+
def get_bbox_from_mask(mask):
|
10 |
+
h,w = mask.shape[0],mask.shape[1]
|
11 |
+
|
12 |
+
if mask.sum() < 10:
|
13 |
+
return 0,h,0,w
|
14 |
+
rows = np.any(mask,axis=1)
|
15 |
+
cols = np.any(mask,axis=0)
|
16 |
+
y1,y2 = np.where(rows)[0][[0,-1]]
|
17 |
+
x1,x2 = np.where(cols)[0][[0,-1]]
|
18 |
+
return (y1,y2,x1,x2)
|
19 |
+
|
20 |
+
# Expand the bounding box
|
21 |
+
def expand_bbox(mask, yyxx, ratio, min_crop=0):
|
22 |
+
y1,y2,x1,x2 = yyxx
|
23 |
+
H,W = mask.shape[0], mask.shape[1]
|
24 |
+
|
25 |
+
yyxx_area = (y2-y1+1) * (x2-x1+1)
|
26 |
+
r1 = yyxx_area / (H * W)
|
27 |
+
r2 = f(r1)
|
28 |
+
ratio = math.sqrt(r2 / r1)
|
29 |
+
|
30 |
+
xc, yc = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
|
31 |
+
h = ratio * (y2-y1+1)
|
32 |
+
w = ratio * (x2-x1+1)
|
33 |
+
h = max(h,min_crop)
|
34 |
+
w = max(w,min_crop)
|
35 |
+
|
36 |
+
x1 = int(xc - w * 0.5)
|
37 |
+
x2 = int(xc + w * 0.5)
|
38 |
+
y1 = int(yc - h * 0.5)
|
39 |
+
y2 = int(yc + h * 0.5)
|
40 |
+
|
41 |
+
x1 = max(0,x1)
|
42 |
+
x2 = min(W,x2)
|
43 |
+
y1 = max(0,y1)
|
44 |
+
y2 = min(H,y2)
|
45 |
+
return (y1,y2,x1,x2)
|
46 |
+
|
47 |
+
# Pad the image to a square shape
|
48 |
+
def pad_to_square(image, pad_value = 255, random = False):
|
49 |
+
H,W = image.shape[0], image.shape[1]
|
50 |
+
if H == W:
|
51 |
+
return image
|
52 |
+
|
53 |
+
padd = abs(H - W)
|
54 |
+
if random:
|
55 |
+
padd_1 = int(np.random.randint(0,padd))
|
56 |
+
else:
|
57 |
+
padd_1 = int(padd / 2)
|
58 |
+
padd_2 = padd - padd_1
|
59 |
+
|
60 |
+
if len(image.shape) == 2:
|
61 |
+
if H > W:
|
62 |
+
pad_param = ((0, 0), (padd_1, padd_2))
|
63 |
+
else:
|
64 |
+
pad_param = ((padd_1, padd_2), (0, 0))
|
65 |
+
elif len(image.shape) == 3:
|
66 |
+
if H > W:
|
67 |
+
pad_param = ((0, 0), (padd_1, padd_2), (0, 0))
|
68 |
+
else:
|
69 |
+
pad_param = ((padd_1, padd_2), (0, 0), (0, 0))
|
70 |
+
|
71 |
+
image = np.pad(image, pad_param, 'constant', constant_values=pad_value)
|
72 |
+
|
73 |
+
return image
|
74 |
+
|
75 |
+
# Expand the image and mask
|
76 |
+
def expand_image_mask(image, mask, ratio=1.4):
|
77 |
+
h,w = image.shape[0], image.shape[1]
|
78 |
+
H,W = int(h * ratio), int(w * ratio)
|
79 |
+
h1 = int((H - h) // 2)
|
80 |
+
h2 = H - h - h1
|
81 |
+
w1 = int((W -w) // 2)
|
82 |
+
w2 = W -w - w1
|
83 |
+
|
84 |
+
pad_param_image = ((h1,h2),(w1,w2),(0,0))
|
85 |
+
pad_param_mask = ((h1,h2),(w1,w2))
|
86 |
+
image = np.pad(image, pad_param_image, 'constant', constant_values=255)
|
87 |
+
mask = np.pad(mask, pad_param_mask, 'constant', constant_values=0)
|
88 |
+
return image, mask
|
89 |
+
|
90 |
+
# Convert the bounding box to a square shape
|
91 |
+
def box2squre(image, box):
|
92 |
+
H,W = image.shape[0], image.shape[1]
|
93 |
+
y1,y2,x1,x2 = box
|
94 |
+
cx = (x1 + x2) // 2
|
95 |
+
cy = (y1 + y2) // 2
|
96 |
+
h,w = y2-y1, x2-x1
|
97 |
+
|
98 |
+
if h >= w:
|
99 |
+
x1 = cx - h//2
|
100 |
+
x2 = cx + h//2
|
101 |
+
else:
|
102 |
+
y1 = cy - w//2
|
103 |
+
y2 = cy + w//2
|
104 |
+
x1 = max(0,x1)
|
105 |
+
x2 = min(W,x2)
|
106 |
+
y1 = max(0,y1)
|
107 |
+
y2 = min(H,y2)
|
108 |
+
return (y1,y2,x1,x2)
|
109 |
+
|
110 |
+
# Crop the predicted image back to the original image
|
111 |
+
def crop_back( pred, tar_image, extra_sizes, tar_box_yyxx_crop):
|
112 |
+
H1, W1, H2, W2 = extra_sizes
|
113 |
+
y1,y2,x1,x2 = tar_box_yyxx_crop
|
114 |
+
pred = cv2.resize(pred, (W2, H2))
|
115 |
+
m = 2 # maigin_pixel
|
116 |
+
|
117 |
+
if W1 == H1:
|
118 |
+
if m != 0:
|
119 |
+
tar_image[y1+m :y2-m, x1+m:x2-m, :] = pred[m:-m, m:-m]
|
120 |
+
else:
|
121 |
+
tar_image[y1 :y2, x1:x2, :] = pred[:, :]
|
122 |
+
return tar_image
|
123 |
+
|
124 |
+
if W1 < W2:
|
125 |
+
pad1 = int((W2 - W1) / 2)
|
126 |
+
pad2 = W2 - W1 - pad1
|
127 |
+
pred = pred[:,pad1: -pad2, :]
|
128 |
+
else:
|
129 |
+
pad1 = int((H2 - H1) / 2)
|
130 |
+
pad2 = H2 - H1 - pad1
|
131 |
+
pred = pred[pad1: -pad2, :, :]
|
132 |
+
|
133 |
+
gen_image = tar_image.copy()
|
134 |
+
if m != 0:
|
135 |
+
gen_image[y1+m :y2-m, x1+m:x2-m, :] = pred[m:-m, m:-m]
|
136 |
+
else:
|
137 |
+
gen_image[y1 :y2, x1:x2, :] = pred[:, :]
|
138 |
+
|
139 |
+
return gen_image
|
140 |
+
|