Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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"]
|
38 |
+
|
39 |
+
info = f"File Name: {file_name}\n"
|
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
|
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:
|
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):
|
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)
|