dwb2023 commited on
Commit
a791d53
1 Parent(s): 971650f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -9,7 +9,7 @@ dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation"
9
 
10
  def simplify_segmentation(segmentation, max_points=20):
11
  if not segmentation or len(segmentation) == 0:
12
- return []
13
  epsilon = 1.0
14
  simplified = rdp(np.array(segmentation), epsilon=epsilon)
15
  while len(simplified) > max_points:
@@ -38,17 +38,13 @@ def draw_annotations(index):
38
 
39
  # Draw original segmentation mask
40
  segmentation = record["segmentation"]
41
- original_points = 0
42
- simplified_points = 0
43
  for seg in segmentation:
44
- if seg: # Check if the segmentation is not empty
45
- draw.polygon(seg, outline="blue", width=2)
46
- original_points += len(seg)
47
-
48
- # Simplify and draw simplified segmentation
49
- simplified_seg = simplify_segmentation(seg)
50
- draw.polygon(simplified_seg, outline="green", width=2)
51
- simplified_points += len(simplified_seg)
52
 
53
  # Prepare additional information
54
  category_id = record["category_id"]
@@ -59,8 +55,8 @@ def draw_annotations(index):
59
  info += f"Image ID: {record['id']}\n"
60
  info += f"Category ID: {category_id}\n"
61
  info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n"
62
- info += f"Original Segmentation Points: {original_points}\n"
63
- info += f"Simplified Segmentation Points: {simplified_points}\n"
64
  info += f"Area: {area:.2f}"
65
 
66
  return img, info
 
9
 
10
  def simplify_segmentation(segmentation, max_points=20):
11
  if not segmentation or len(segmentation) == 0:
12
+ return segmentation # Return original if empty
13
  epsilon = 1.0
14
  simplified = rdp(np.array(segmentation), epsilon=epsilon)
15
  while len(simplified) > max_points:
 
38
 
39
  # Draw original segmentation mask
40
  segmentation = record["segmentation"]
 
 
41
  for seg in segmentation:
42
+ draw.polygon(seg, outline="blue", width=2)
43
+
44
+ # Draw simplified segmentation
45
+ simplified_segmentation = [simplify_segmentation(seg) for seg in segmentation]
46
+ for seg in simplified_segmentation:
47
+ draw.polygon(seg, outline="green", width=2)
 
 
48
 
49
  # Prepare additional information
50
  category_id = record["category_id"]
 
55
  info += f"Image ID: {record['id']}\n"
56
  info += f"Category ID: {category_id}\n"
57
  info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n"
58
+ info += f"Original Segmentation Points: {sum(len(seg) for seg in segmentation)}\n"
59
+ info += f"Simplified Segmentation Points: {sum(len(seg) for seg in simplified_segmentation)}\n"
60
  info += f"Area: {area:.2f}"
61
 
62
  return img, info