Update app.py
Browse files
app.py
CHANGED
@@ -2,20 +2,10 @@ import gradio as gr
|
|
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 |
-
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:
|
16 |
-
epsilon *= 1.5
|
17 |
-
simplified = rdp(np.array(segmentation), epsilon=epsilon)
|
18 |
-
return simplified.tolist()
|
19 |
|
20 |
def draw_annotations(index):
|
21 |
try:
|
@@ -36,16 +26,11 @@ def draw_annotations(index):
|
|
36 |
bbox = record["bbox"]
|
37 |
draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2)
|
38 |
|
39 |
-
# Draw
|
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"]
|
51 |
area = record["area"]
|
@@ -55,8 +40,7 @@ def draw_annotations(index):
|
|
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"
|
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
|
@@ -68,7 +52,6 @@ def draw_annotations(index):
|
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown("# Brain Tumor Image Dataset Viewer")
|
70 |
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")
|
71 |
-
gr.Markdown("### Red: Bounding Box, Blue: Original Segmentation, Green: Simplified Segmentation (max 20 points)")
|
72 |
|
73 |
with gr.Row():
|
74 |
with gr.Column(scale=1):
|
|
|
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:
|
|
|
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"]
|
|
|
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 |
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):
|