dwb2023 commited on
Commit
7d4df53
·
verified ·
1 Parent(s): d47279a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -8,7 +8,10 @@ from rdp import rdp
8
  dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
9
 
10
  def simplify_segmentation(segmentation, max_points=20):
11
- simplified = rdp(np.array(segmentation), epsilon=1.0)
 
 
 
12
  while len(simplified) > max_points:
13
  epsilon *= 1.5
14
  simplified = rdp(np.array(segmentation), epsilon=epsilon)
@@ -30,15 +33,23 @@ def draw_annotations(index):
30
  bbox = record["bbox"]
31
  draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2)
32
 
33
- # Draw original segmentation mask
34
  segmentation = record["segmentation"]
35
- for seg in segmentation:
36
- draw.polygon(seg, outline="blue", width=2)
 
 
 
 
 
 
37
 
38
- # Simplify and draw simplified segmentation mask
39
- simplified_segmentation = [simplify_segmentation(seg) for seg in segmentation]
40
- for seg in simplified_segmentation:
41
- draw.polygon(seg, outline="green", width=2)
 
 
42
 
43
  category_id = record["category_id"]
44
  area = record["area"]
@@ -48,8 +59,8 @@ def draw_annotations(index):
48
  info += f"Image ID: {record['id']}\n"
49
  info += f"Category ID: {category_id}\n"
50
  info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n"
51
- info += f"Original Segmentation Points: {sum(len(seg) for seg in segmentation)}\n"
52
- info += f"Simplified Segmentation Points: {sum(len(seg) for seg in simplified_segmentation)}\n"
53
  info += f"Area: {area:.2f}"
54
 
55
  return img, info
@@ -61,7 +72,7 @@ def draw_annotations(index):
61
  with gr.Blocks() as demo:
62
  gr.Markdown("# Brain Tumor Image Dataset Viewer")
63
  gr.Markdown("## Refer to the [dwb2023/brain-tumor-image-dataset-semantic-segmentation](https://huggingface.co/datasets/dwb2023/brain-tumor-image-dataset-semantic-segmentation/viewer/default/test) dataset for more information")
64
- gr.Markdown("### Blue: Original Segmentation, Green: Simplified Segmentation (max 20 points)")
65
 
66
  with gr.Row():
67
  with gr.Column(scale=1):
 
8
  dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
9
 
10
  def simplify_segmentation(segmentation, max_points=20):
11
+ if not segmentation:
12
+ return []
13
+ epsilon = 1.0
14
+ simplified = rdp(np.array(segmentation), epsilon=epsilon)
15
  while len(simplified) > max_points:
16
  epsilon *= 1.5
17
  simplified = rdp(np.array(segmentation), epsilon=epsilon)
 
33
  bbox = record["bbox"]
34
  draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2)
35
 
36
+ # Draw segmentation mask
37
  segmentation = record["segmentation"]
38
+ original_points = 0
39
+ simplified_points = 0
40
+
41
+ if segmentation and len(segmentation) > 0 and len(segmentation[0]) > 0:
42
+ for seg in segmentation:
43
+ if len(seg) > 0:
44
+ draw.polygon(seg, outline="blue", width=2)
45
+ original_points += len(seg)
46
 
47
+ # Simplify and draw simplified segmentation mask
48
+ simplified_segmentation = [simplify_segmentation(seg) for seg in segmentation if len(seg) > 0]
49
+ for seg in simplified_segmentation:
50
+ if len(seg) > 0:
51
+ draw.polygon(seg, outline="green", width=2)
52
+ simplified_points += len(seg)
53
 
54
  category_id = record["category_id"]
55
  area = record["area"]
 
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
 
72
  with gr.Blocks() as demo:
73
  gr.Markdown("# Brain Tumor Image Dataset Viewer")
74
  gr.Markdown("## Refer to the [dwb2023/brain-tumor-image-dataset-semantic-segmentation](https://huggingface.co/datasets/dwb2023/brain-tumor-image-dataset-semantic-segmentation/viewer/default/test) dataset for more information")
75
+ gr.Markdown("### Red: Bounding Box, Blue: Original Segmentation, Green: Simplified Segmentation (max 20 points)")
76
 
77
  with gr.Row():
78
  with gr.Column(scale=1):