Spaces:
Sleeping
Sleeping
Upload Colony_Analyzer_AI2_HF.py
Browse files- Colony_Analyzer_AI2_HF.py +17 -9
Colony_Analyzer_AI2_HF.py
CHANGED
|
@@ -38,28 +38,36 @@ def pad(img_np, tw=2048, th=1536):
|
|
| 38 |
|
| 39 |
|
| 40 |
#this is the huggingface version
|
|
|
|
|
|
|
|
|
|
| 41 |
def cut_img(img, patch_size=512):
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if isinstance(img, Image.Image):
|
| 44 |
-
width, height = img.size
|
| 45 |
img_np = np.array(img)
|
|
|
|
| 46 |
else:
|
| 47 |
img_np = img
|
| 48 |
height, width = img_np.shape[:2]
|
|
|
|
| 49 |
i_num = height // patch_size
|
| 50 |
j_num = width // patch_size
|
|
|
|
| 51 |
count = 1
|
| 52 |
for i in range(i_num):
|
| 53 |
for j in range(j_num):
|
| 54 |
-
cropped_img =
|
| 55 |
-
patch_size *
|
| 56 |
-
patch_size *
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
))
|
| 60 |
img_map[count] = cropped_img
|
| 61 |
count += 1
|
| 62 |
-
return img_map, i_num, j_num
|
| 63 |
|
| 64 |
import numpy as np
|
| 65 |
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
#this is the huggingface version
|
| 41 |
+
import numpy as np
|
| 42 |
+
from PIL import Image
|
| 43 |
+
|
| 44 |
def cut_img(img, patch_size=512):
|
| 45 |
+
"""
|
| 46 |
+
Cuts an image (PIL or numpy array) into non-overlapping patch_size x patch_size tiles.
|
| 47 |
+
Returns img_map, number of rows, number of cols.
|
| 48 |
+
"""
|
| 49 |
+
# If it's a PIL image, convert to numpy array
|
| 50 |
if isinstance(img, Image.Image):
|
|
|
|
| 51 |
img_np = np.array(img)
|
| 52 |
+
height, width = img_np.shape[:2]
|
| 53 |
else:
|
| 54 |
img_np = img
|
| 55 |
height, width = img_np.shape[:2]
|
| 56 |
+
|
| 57 |
i_num = height // patch_size
|
| 58 |
j_num = width // patch_size
|
| 59 |
+
img_map = {}
|
| 60 |
count = 1
|
| 61 |
for i in range(i_num):
|
| 62 |
for j in range(j_num):
|
| 63 |
+
cropped_img = img_np[
|
| 64 |
+
patch_size * i: patch_size * (i + 1),
|
| 65 |
+
patch_size * j: patch_size * (j + 1),
|
| 66 |
+
...
|
| 67 |
+
]
|
|
|
|
| 68 |
img_map[count] = cropped_img
|
| 69 |
count += 1
|
| 70 |
+
return img_map, i_num, j_num
|
| 71 |
|
| 72 |
import numpy as np
|
| 73 |
|