phoebehxf commited on
Commit
37bfb8b
·
1 Parent(s): 98c76d5
Files changed (1) hide show
  1. app.py +51 -16
app.py CHANGED
@@ -28,13 +28,13 @@ DATASET_REPO = "phoebe777777/celltool_feedback"
28
 
29
  # ===== 清理缓存目录 =====
30
  print("===== clearing cache =====")
31
- cache_path = os.path.expanduser("~/.cache/")
32
- # cache_path = os.path.expanduser("~/.cache/huggingface/gradio")
33
  if os.path.exists(cache_path):
34
  try:
35
  shutil.rmtree(cache_path)
36
- print("✅ Deleted ~/.cache/")
37
- # print("✅ Deleted ~/.cache/huggingface/gradio")
38
  except:
39
  pass
40
 
@@ -167,6 +167,22 @@ def parse_first_bbox(bboxes):
167
  return float(b[0]), float(b[1]), float(b[2]), float(b[3])
168
  return None
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  def colorize_mask(mask: np.ndarray, num_colors: int = 512) -> np.ndarray:
171
  """将实例掩码转换为彩色图像"""
172
  def hsv_to_rgb(h, s, v):
@@ -206,11 +222,16 @@ def segment_with_choice(use_box_choice, annot_value):
206
  print(f"🖼️ Image path: {img_path}")
207
  box_array = None
208
  if use_box_choice == "Yes" and bboxes:
209
- box = parse_first_bbox(bboxes)
 
 
 
 
 
210
  if box:
211
- xmin, ymin, xmax, ymax = map(int, box)
212
- box_array = [[xmin, ymin, xmax, ymax]]
213
- print(f"📦 Using bounding box: {box_array}")
214
 
215
  # 运行分割模型
216
  try:
@@ -291,11 +312,15 @@ def count_cells_handler(use_box_choice, annot_value):
291
  print(f"🖼️ Image path: {image_path}")
292
  box_array = None
293
  if use_box_choice == "Yes" and bboxes:
294
- box = parse_first_bbox(bboxes)
 
 
 
 
 
295
  if box:
296
- xmin, ymin, xmax, ymax = map(int, box)
297
- box_array = [[xmin, ymin, xmax, ymax]]
298
- print(f"📦 Using bounding box: {box_array}")
299
 
300
  try:
301
  print(f"🔢 Counting - Image: {image_path}")
@@ -364,6 +389,9 @@ def count_cells_handler(use_box_choice, annot_value):
364
 
365
 
366
  result_text = f"✅ Detected {round(count)} objects"
 
 
 
367
 
368
  print(f"✅ Counting done - Count: {count:.1f}")
369
 
@@ -703,11 +731,15 @@ def track_video_handler(use_box_choice, first_frame_annot, zip_file_obj):
703
  if isinstance(first_frame_annot, (list, tuple)) and len(first_frame_annot) > 1:
704
  bboxes = first_frame_annot[1]
705
  if bboxes:
706
- box = parse_first_bbox(bboxes)
 
 
 
 
 
707
  if box:
708
- xmin, ymin, xmax, ymax = map(int, box)
709
- box_array = [[xmin, ymin, xmax, ymax]]
710
- print(f"📦 Using bounding box: {box_array}")
711
 
712
  # Extract input ZIP
713
  temp_dir = tempfile.mkdtemp()
@@ -813,6 +845,9 @@ def track_video_handler(use_box_choice, first_frame_annot, zip_file_obj):
813
  - README.txt (Results description)
814
  """
815
 
 
 
 
816
  print(f"\n✅ Tracking completed")
817
 
818
  # Clean up input temp directory (keep output temp for download)
 
28
 
29
  # ===== 清理缓存目录 =====
30
  print("===== clearing cache =====")
31
+ # cache_path = os.path.expanduser("~/.cache/")
32
+ cache_path = os.path.expanduser("~/.cache/huggingface/gradio")
33
  if os.path.exists(cache_path):
34
  try:
35
  shutil.rmtree(cache_path)
36
+ # print("✅ Deleted ~/.cache/")
37
+ print("✅ Deleted ~/.cache/huggingface/gradio")
38
  except:
39
  pass
40
 
 
167
  return float(b[0]), float(b[1]), float(b[2]), float(b[3])
168
  return None
169
 
170
+ def parse_bboxes(bboxes):
171
+ """解析所有边界框"""
172
+ if not bboxes:
173
+ return None
174
+
175
+ result = []
176
+ for b in bboxes:
177
+ if isinstance(b, dict):
178
+ x, y = float(b.get("x", 0)), float(b.get("y", 0))
179
+ w, h = float(b.get("width", 0)), float(b.get("height", 0))
180
+ result.append([x, y, x + w, y + h])
181
+ elif isinstance(b, (list, tuple)) and len(b) >= 4:
182
+ result.append([float(b[0]), float(b[1]), float(b[2]), float(b[3])])
183
+
184
+ return result
185
+
186
  def colorize_mask(mask: np.ndarray, num_colors: int = 512) -> np.ndarray:
187
  """将实例掩码转换为彩色图像"""
188
  def hsv_to_rgb(h, s, v):
 
222
  print(f"🖼️ Image path: {img_path}")
223
  box_array = None
224
  if use_box_choice == "Yes" and bboxes:
225
+ # box = parse_first_bbox(bboxes)
226
+ # if box:
227
+ # xmin, ymin, xmax, ymax = map(int, box)
228
+ # box_array = [[xmin, ymin, xmax, ymax]]
229
+ # print(f"📦 Using bounding box: {box_array}")
230
+ box = parse_bboxes(bboxes)
231
  if box:
232
+ box_array = box
233
+ print(f"📦 Using bounding boxes: {box_array}")
234
+
235
 
236
  # 运行分割模型
237
  try:
 
312
  print(f"🖼️ Image path: {image_path}")
313
  box_array = None
314
  if use_box_choice == "Yes" and bboxes:
315
+ # box = parse_first_bbox(bboxes)
316
+ # if box:
317
+ # xmin, ymin, xmax, ymax = map(int, box)
318
+ # box_array = [[xmin, ymin, xmax, ymax]]
319
+ # print(f"📦 Using bounding box: {box_array}")
320
+ box = parse_bboxes(bboxes)
321
  if box:
322
+ box_array = box
323
+ print(f"📦 Using bounding boxes: {box_array}")
 
324
 
325
  try:
326
  print(f"🔢 Counting - Image: {image_path}")
 
389
 
390
 
391
  result_text = f"✅ Detected {round(count)} objects"
392
+ if use_box_choice == "Yes" and box:
393
+ result_text += f"\n📦 Using bounding box: {box_array}"
394
+
395
 
396
  print(f"✅ Counting done - Count: {count:.1f}")
397
 
 
731
  if isinstance(first_frame_annot, (list, tuple)) and len(first_frame_annot) > 1:
732
  bboxes = first_frame_annot[1]
733
  if bboxes:
734
+ # box = parse_first_bbox(bboxes)
735
+ # if box:
736
+ # xmin, ymin, xmax, ymax = map(int, box)
737
+ # box_array = [[xmin, ymin, xmax, ymax]]
738
+ # print(f"📦 Using bounding box: {box_array}")
739
+ box = parse_bboxes(bboxes)
740
  if box:
741
+ box_array = box
742
+ print(f"📦 Using bounding boxes: {box_array}")
 
743
 
744
  # Extract input ZIP
745
  temp_dir = tempfile.mkdtemp()
 
845
  - README.txt (Results description)
846
  """
847
 
848
+ if use_box_choice == "Yes" and box:
849
+ result_text += f"\n📦 Using bounding box: {box_array}"
850
+
851
  print(f"\n✅ Tracking completed")
852
 
853
  # Clean up input temp directory (keep output temp for download)