Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -8,7 +8,7 @@ def RGB2gray(rgb):
|
|
| 8 |
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
| 9 |
return gray
|
| 10 |
|
| 11 |
-
def img_to_patches(img: PIL.Image.Image)
|
| 12 |
patch_size = 16
|
| 13 |
img = img.convert("RGB")
|
| 14 |
|
|
@@ -43,7 +43,7 @@ def get_pixel_var_degree_for_patch(patch: np.array) -> int:
|
|
| 43 |
l1 = get_l1(patch)
|
| 44 |
l2 = get_l2(patch)
|
| 45 |
l3l4 = get_l3l4(patch)
|
| 46 |
-
return l1 + l2 + l3l4
|
| 47 |
|
| 48 |
def get_rich_poor_patches(img: PIL.Image.Image, coloured=True):
|
| 49 |
gray_scale_patches, color_patches, coordinates, img_size = img_to_patches(img)
|
|
@@ -81,17 +81,14 @@ def azimuthalAverage(image, center=None):
|
|
| 81 |
def azimuthal_integral(img, epsilon=1e-8, N=50):
|
| 82 |
if len(img.shape) == 3 and img.shape[2] == 3:
|
| 83 |
img = RGB2gray(img)
|
| 84 |
-
|
| 85 |
f = np.fft.fft2(img)
|
| 86 |
fshift = np.fft.fftshift(f)
|
| 87 |
fshift += epsilon
|
| 88 |
magnitude_spectrum = 20 * np.log(np.abs(fshift))
|
| 89 |
psd1D = azimuthalAverage(magnitude_spectrum)
|
| 90 |
-
|
| 91 |
points = np.linspace(0, N, num=psd1D.size)
|
| 92 |
xi = np.linspace(0, N, num=N)
|
| 93 |
interpolated = griddata(points, psd1D, xi, method="cubic")
|
| 94 |
-
|
| 95 |
interpolated = (interpolated - np.min(interpolated)) / (np.max(interpolated) - np.min(interpolated) + 1e-8)
|
| 96 |
return interpolated.astype(np.float32)
|
| 97 |
|
|
@@ -100,18 +97,15 @@ def positional_emb(coor, im_size, N):
|
|
| 100 |
center_y, center_x = coor
|
| 101 |
normalized_y = center_y / img_height
|
| 102 |
normalized_x = center_x / img_width
|
| 103 |
-
|
| 104 |
pos_emb = np.zeros(N, dtype=np.float32)
|
| 105 |
indices = np.arange(N)
|
| 106 |
div_term = 10000 ** (2 * (indices // 2) / N)
|
| 107 |
-
|
| 108 |
pos_emb[0::2] = np.sin(normalized_y / div_term[0::2]) + np.sin(normalized_x / div_term[0::2])
|
| 109 |
pos_emb[1::2] = np.cos(normalized_y / div_term[1::2]) + np.cos(normalized_x / div_term[1::2])
|
| 110 |
return pos_emb
|
| 111 |
|
| 112 |
def azi_diff(img: PIL.Image.Image, patch_num, N):
|
| 113 |
r, p, im_size = get_rich_poor_patches(img)
|
| 114 |
-
|
| 115 |
r_len = len(r)
|
| 116 |
p_len = len(p)
|
| 117 |
|
|
@@ -140,8 +134,8 @@ def azi_diff(img: PIL.Image.Image, patch_num, N):
|
|
| 140 |
coor_p.append(tmp_coor2)
|
| 141 |
|
| 142 |
output = {
|
| 143 |
-
"total_emb": [patch_emb_r + positional_emb_r / 5
|
| 144 |
-
"positional_emb": [positional_emb_r / 5
|
| 145 |
"coor": [coor_r, coor_p],
|
| 146 |
"image_size": im_size,
|
| 147 |
}
|
|
|
|
| 8 |
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
| 9 |
return gray
|
| 10 |
|
| 11 |
+
def img_to_patches(img: PIL.Image.Image):
|
| 12 |
patch_size = 16
|
| 13 |
img = img.convert("RGB")
|
| 14 |
|
|
|
|
| 43 |
l1 = get_l1(patch)
|
| 44 |
l2 = get_l2(patch)
|
| 45 |
l3l4 = get_l3l4(patch)
|
| 46 |
+
return int(l1 + l2 + l3l4)
|
| 47 |
|
| 48 |
def get_rich_poor_patches(img: PIL.Image.Image, coloured=True):
|
| 49 |
gray_scale_patches, color_patches, coordinates, img_size = img_to_patches(img)
|
|
|
|
| 81 |
def azimuthal_integral(img, epsilon=1e-8, N=50):
|
| 82 |
if len(img.shape) == 3 and img.shape[2] == 3:
|
| 83 |
img = RGB2gray(img)
|
|
|
|
| 84 |
f = np.fft.fft2(img)
|
| 85 |
fshift = np.fft.fftshift(f)
|
| 86 |
fshift += epsilon
|
| 87 |
magnitude_spectrum = 20 * np.log(np.abs(fshift))
|
| 88 |
psd1D = azimuthalAverage(magnitude_spectrum)
|
|
|
|
| 89 |
points = np.linspace(0, N, num=psd1D.size)
|
| 90 |
xi = np.linspace(0, N, num=N)
|
| 91 |
interpolated = griddata(points, psd1D, xi, method="cubic")
|
|
|
|
| 92 |
interpolated = (interpolated - np.min(interpolated)) / (np.max(interpolated) - np.min(interpolated) + 1e-8)
|
| 93 |
return interpolated.astype(np.float32)
|
| 94 |
|
|
|
|
| 97 |
center_y, center_x = coor
|
| 98 |
normalized_y = center_y / img_height
|
| 99 |
normalized_x = center_x / img_width
|
|
|
|
| 100 |
pos_emb = np.zeros(N, dtype=np.float32)
|
| 101 |
indices = np.arange(N)
|
| 102 |
div_term = 10000 ** (2 * (indices // 2) / N)
|
|
|
|
| 103 |
pos_emb[0::2] = np.sin(normalized_y / div_term[0::2]) + np.sin(normalized_x / div_term[0::2])
|
| 104 |
pos_emb[1::2] = np.cos(normalized_y / div_term[1::2]) + np.cos(normalized_x / div_term[1::2])
|
| 105 |
return pos_emb
|
| 106 |
|
| 107 |
def azi_diff(img: PIL.Image.Image, patch_num, N):
|
| 108 |
r, p, im_size = get_rich_poor_patches(img)
|
|
|
|
| 109 |
r_len = len(r)
|
| 110 |
p_len = len(p)
|
| 111 |
|
|
|
|
| 134 |
coor_p.append(tmp_coor2)
|
| 135 |
|
| 136 |
output = {
|
| 137 |
+
"total_emb": [patch_emb_r + positional_emb_r / 5, patch_emb_p + positional_emb_p / 5],
|
| 138 |
+
"positional_emb": [positional_emb_r / 5, positional_emb_p / 5],
|
| 139 |
"coor": [coor_r, coor_p],
|
| 140 |
"image_size": im_size,
|
| 141 |
}
|