dwb2023 commited on
Commit
e9de875
1 Parent(s): e36cc5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -2,36 +2,44 @@ import gradio as gr
2
  from datasets import load_dataset
3
  from PIL import Image, ImageDraw
4
  import numpy as np
 
5
 
6
  # Load the dataset
7
  dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
8
- # print(f"Dataset loaded successfully. Number of images: {len(dataset)}")
 
 
 
 
 
 
9
 
10
  def draw_annotations(index):
11
  try:
12
- # Fetch the image and annotations from the dataset
13
  record = dataset[index]
14
 
15
- # Convert image to PIL Image if it's a numpy array
16
  if isinstance(record['image'], np.ndarray):
17
  img = Image.fromarray(record['image'])
18
  else:
19
  img = record['image']
20
 
21
- img = img.convert("RGB") # Ensure the image is in RGB mode
22
-
23
  draw = ImageDraw.Draw(img)
24
 
25
  # Draw bounding box
26
  bbox = record["bbox"]
27
  draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2)
28
 
29
- # Draw segmentation mask
30
  segmentation = record["segmentation"]
31
  for seg in segmentation:
32
  draw.polygon(seg, outline="blue", width=2)
33
 
34
- # Prepare additional information
 
 
 
 
35
  category_id = record["category_id"]
36
  area = record["area"]
37
  file_name = record["file_name"]
@@ -40,7 +48,8 @@ def draw_annotations(index):
40
  info += f"Image ID: {record['id']}\n"
41
  info += f"Category ID: {category_id}\n"
42
  info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n"
43
- info += f"Segmentation: {segmentation}\n"
 
44
  info += f"Area: {area:.2f}"
45
 
46
  return img, info
@@ -52,6 +61,7 @@ def draw_annotations(index):
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# Brain Tumor Image Dataset Viewer")
54
  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")
 
55
 
56
  with gr.Row():
57
  with gr.Column(scale=1):
 
2
  from datasets import load_dataset
3
  from PIL import Image, ImageDraw
4
  import numpy as np
5
+ from rdp import rdp
6
 
7
  # Load the dataset
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)
15
+ return simplified.tolist()
16
 
17
  def draw_annotations(index):
18
  try:
 
19
  record = dataset[index]
20
 
 
21
  if isinstance(record['image'], np.ndarray):
22
  img = Image.fromarray(record['image'])
23
  else:
24
  img = record['image']
25
 
26
+ img = img.convert("RGB")
 
27
  draw = ImageDraw.Draw(img)
28
 
29
  # Draw bounding box
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"]
45
  file_name = record["file_name"]
 
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
  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):