ReyaLabColumbia commited on
Commit
2c6a1ea
·
verified ·
1 Parent(s): 9a0a054

Upload Colony_Analyzer_AI2_HF.py

Browse files
Files changed (1) hide show
  1. Colony_Analyzer_AI2_HF.py +22 -49
Colony_Analyzer_AI2_HF.py CHANGED
@@ -8,63 +8,37 @@ Created on Thu Mar 20 14:23:27 2025
8
 
9
  import os
10
  import cv2
11
- from PIL import Image
12
-
13
-
14
- def ensure_minimum_size(img, min_width=512, min_height=512):
15
- width, height = img.size
16
- if width < min_width or height < min_height:
17
- return img.resize((max(width, min_width), max(height, min_height)), Image.BICUBIC)
18
- return img
19
 
20
  #this is the huggingface version
21
- def cut_img(img, patch_size=512):
22
  img_map = {}
23
  width, height = img.size
24
- i_num = height // patch_size
25
- j_num = width // patch_size
26
  count = 1
27
  for i in range(i_num):
28
  for j in range(j_num):
29
- cropped_img = img.crop((
30
- patch_size * j,
31
- patch_size * i,
32
- patch_size * (j + 1),
33
- patch_size * (i + 1)
34
- ))
35
  img_map[count] = cropped_img
 
36
  count += 1
37
- return img_map, i_num, j_num # Return rows and cols for stitching
38
 
39
  import numpy as np
40
 
41
- import numpy as np
 
 
 
 
 
 
 
 
 
 
42
  from PIL import Image
43
 
44
- def stitch(img_map, i_num, j_num, min_width=2048, min_height=1536):
45
- tiles = []
46
- count = 1
47
- for i in range(i_num):
48
- row_tiles = []
49
- for j in range(j_num):
50
- tile = np.array(img_map[count])
51
- row_tiles.append(tile)
52
- count += 1
53
- row_img = np.hstack(row_tiles)
54
- tiles.append(row_img)
55
- stitched = np.vstack(tiles)
56
-
57
- # Pad the stitched image if it's less than min_width/min_height
58
- h, w = stitched.shape[:2]
59
- pad_h = max(0, min_height - h)
60
- pad_w = max(0, min_width - w)
61
- if pad_h > 0 or pad_w > 0:
62
- # Pad as (top, bottom), (left, right), (channels)
63
- if stitched.ndim == 3:
64
- stitched = np.pad(stitched, ((0, pad_h), (0, pad_w), (0, 0)), 'constant')
65
- else:
66
- stitched = np.pad(stitched, ((0, pad_h), (0, pad_w)), 'constant')
67
- return stitched
68
 
69
 
70
  import matplotlib.pyplot as plt
@@ -240,22 +214,21 @@ def main(args):
240
  min_circ = args[2]
241
  do_necrosis = args[3]
242
  colonies = {}
243
- img_map, i_num, j_num = cut_img(args[0])
244
  for z in img_map:
245
  img_map[z] = eval_img(img_map[z])
246
  del z
247
- p = stitch(img_map, i_num, j_num)
248
  colonies = analyze_colonies(p, min_size, min_circ, np.array(args[0]))
249
- img = np.array(ensure_minimum_size(args[0]))
250
  if len(colonies) <=0:
251
  caption = np.ones((150, 2048, 3), dtype=np.uint8) * 255 # Multiply by 255 to make it white
252
  cv2.putText(caption, 'No colonies detected.', (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
253
- cv2.imwrite('results.png', np.vstack((img, caption)))
254
  colonies = pd.DataFrame({"Colony Number":[], 'Colony volume':[], "colony_area":[],'mean_pixel_value':[], "centroid":[], "necrotic_area":[],"percent_necrotic":[]})
255
  with pd.ExcelWriter('results.xlsx') as writer:
256
  colonies.to_excel(writer, sheet_name="Colony data", index=False)
257
- return(np.vstack((img, caption)), 'results.png', 'results.xlsx')
258
-
259
  img = cv2.copyMakeBorder(img,top=0, bottom=10,left=0,right=10, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
260
  #print(colonies.to_string())
261
 
 
8
 
9
  import os
10
  import cv2
 
 
 
 
 
 
 
 
11
 
12
  #this is the huggingface version
13
+ def cut_img(img):
14
  img_map = {}
15
  width, height = img.size
16
+ i_num = height // 512
17
+ j_num = width // 512
18
  count = 1
19
  for i in range(i_num):
20
  for j in range(j_num):
21
+ cropped_img = img.crop((512*j, 512*i, 512*(j+1), 512*(i+1)))
 
 
 
 
 
22
  img_map[count] = cropped_img
23
+ #print(type(cropped_img))
24
  count += 1
25
+ return img_map
26
 
27
  import numpy as np
28
 
29
+ def stitch(img_map):
30
+ rows = [
31
+ np.hstack([img_map[1], img_map[2], img_map[3], img_map[4]]), # First row (images 0 to 3)
32
+ np.hstack([img_map[5], img_map[6], img_map[7], img_map[8]]), # Second row (images 4 to 7)
33
+ np.hstack([img_map[9], img_map[10], img_map[11], img_map[12]]) # Third row (images 8 to 11)
34
+ ]
35
+ # Stack rows vertically
36
+ return(np.vstack(rows))
37
+
38
+
39
+
40
  from PIL import Image
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
 
44
  import matplotlib.pyplot as plt
 
214
  min_circ = args[2]
215
  do_necrosis = args[3]
216
  colonies = {}
217
+ img_map = cut_img(args[0])
218
  for z in img_map:
219
  img_map[z] = eval_img(img_map[z])
220
  del z
221
+ p = stitch(img_map)
222
  colonies = analyze_colonies(p, min_size, min_circ, np.array(args[0]))
 
223
  if len(colonies) <=0:
224
  caption = np.ones((150, 2048, 3), dtype=np.uint8) * 255 # Multiply by 255 to make it white
225
  cv2.putText(caption, 'No colonies detected.', (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
226
+ cv2.imwrite('results.png', np.vstack((np.array(args[0]), caption)))
227
  colonies = pd.DataFrame({"Colony Number":[], 'Colony volume':[], "colony_area":[],'mean_pixel_value':[], "centroid":[], "necrotic_area":[],"percent_necrotic":[]})
228
  with pd.ExcelWriter('results.xlsx') as writer:
229
  colonies.to_excel(writer, sheet_name="Colony data", index=False)
230
+ return(np.vstack((args[0], caption)), 'results.png', 'results.xlsx')
231
+ img = np.array(args[0])
232
  img = cv2.copyMakeBorder(img,top=0, bottom=10,left=0,right=10, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
233
  #print(colonies.to_string())
234