Update app.py
Browse files
app.py
CHANGED
@@ -5,22 +5,12 @@ import numpy as np
|
|
5 |
|
6 |
# Load the dataset
|
7 |
dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
|
|
|
8 |
|
9 |
-
|
10 |
-
def filter_dataset_by_category(category_id):
|
11 |
-
filtered_indices = [i for i, record in enumerate(dataset) if record["category_id"] == category_id]
|
12 |
-
return filtered_indices
|
13 |
-
|
14 |
-
# Function to draw annotations
|
15 |
-
def draw_annotations(index, category_id):
|
16 |
-
filtered_indices = filter_dataset_by_category(category_id)
|
17 |
-
|
18 |
-
if index >= len(filtered_indices):
|
19 |
-
index = 0
|
20 |
-
|
21 |
try:
|
22 |
# Fetch the image and annotations from the dataset
|
23 |
-
record = dataset[
|
24 |
|
25 |
# Convert image to PIL Image if it's a numpy array
|
26 |
if isinstance(record['image'], np.ndarray):
|
@@ -53,10 +43,10 @@ def draw_annotations(index, category_id):
|
|
53 |
info += f"Segmentation: {segmentation}\n"
|
54 |
info += f"Area: {area:.2f}"
|
55 |
|
56 |
-
return img, info
|
57 |
except Exception as e:
|
58 |
print(f"Error processing image at index {index}: {e}")
|
59 |
-
return Image.new('RGB', (300, 300), color='gray'), f"Error loading image information: {str(e)}"
|
60 |
|
61 |
# Create Gradio interface
|
62 |
with gr.Blocks() as demo:
|
@@ -67,19 +57,13 @@ with gr.Blocks() as demo:
|
|
67 |
with gr.Column(scale=1):
|
68 |
image_output = gr.Image(label="Annotated Image")
|
69 |
with gr.Column(scale=1):
|
70 |
-
|
71 |
-
image_index = gr.Slider(minimum=0, maximum=0, step=1, value=0, label="Image ID Slider")
|
72 |
info_output = gr.Textbox(label="Image Information", lines=10)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
return gr.Slider.update(maximum=max_index), img, info
|
77 |
-
|
78 |
-
# Update image and info when slider or category changes
|
79 |
-
category_id_dropdown.change(update_slider, inputs=category_id_dropdown, outputs=[image_index, image_output, info_output])
|
80 |
-
image_index.change(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index])
|
81 |
|
82 |
# Display initial image and info
|
83 |
-
demo.load(draw_annotations, inputs=
|
84 |
|
85 |
-
demo.launch(debug=True)
|
|
|
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):
|
|
|
43 |
info += f"Segmentation: {segmentation}\n"
|
44 |
info += f"Area: {area:.2f}"
|
45 |
|
46 |
+
return img, info
|
47 |
except Exception as e:
|
48 |
print(f"Error processing image at index {index}: {e}")
|
49 |
+
return Image.new('RGB', (300, 300), color='gray'), f"Error loading image information: {str(e)}"
|
50 |
|
51 |
# Create Gradio interface
|
52 |
with gr.Blocks() as demo:
|
|
|
57 |
with gr.Column(scale=1):
|
58 |
image_output = gr.Image(label="Annotated Image")
|
59 |
with gr.Column(scale=1):
|
60 |
+
image_index = gr.Slider(minimum=0, maximum=len(dataset)-1, step=1, value=0, label="Image ID Slider")
|
|
|
61 |
info_output = gr.Textbox(label="Image Information", lines=10)
|
62 |
|
63 |
+
# Update image and info when slider changes
|
64 |
+
image_index.change(draw_annotations, inputs=image_index, outputs=[image_output, info_output])
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# Display initial image and info
|
67 |
+
demo.load(draw_annotations, inputs=image_index, outputs=[image_output, info_output])
|
68 |
|
69 |
+
demo.launch(debug=True)
|