|
import gradio as gr |
|
from datasets import load_dataset |
|
from PIL import Image, ImageDraw |
|
import numpy as np |
|
|
|
|
|
dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test") |
|
|
|
|
|
def filter_dataset_by_category(category_id): |
|
filtered_indices = [i for i, record in enumerate(dataset) if record["category_id"] == category_id] |
|
return filtered_indices |
|
|
|
|
|
def draw_annotations(index, category_id): |
|
filtered_indices = filter_dataset_by_category(category_id) |
|
|
|
if index >= len(filtered_indices): |
|
index = 0 |
|
|
|
try: |
|
|
|
record = dataset[filtered_indices[index]] |
|
|
|
|
|
if isinstance(record['image'], np.ndarray): |
|
img = Image.fromarray(record['image']) |
|
else: |
|
img = record['image'] |
|
|
|
img = img.convert("RGB") |
|
|
|
draw = ImageDraw.Draw(img) |
|
|
|
|
|
bbox = record["bbox"] |
|
draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2) |
|
|
|
|
|
segmentation = record["segmentation"] |
|
for seg in segmentation: |
|
draw.polygon(seg, outline="blue", width=2) |
|
|
|
|
|
category_id = record["category_id"] |
|
area = record["area"] |
|
file_name = record["file_name"] |
|
|
|
info = f"File Name: {file_name}\n" |
|
info += f"Image ID: {record['id']}\n" |
|
info += f"Category ID: {category_id}\n" |
|
info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n" |
|
info += f"Segmentation: {segmentation}\n" |
|
info += f"Area: {area:.2f}" |
|
|
|
return img, info, len(filtered_indices) - 1 |
|
except Exception as e: |
|
print(f"Error processing image at index {index}: {e}") |
|
return Image.new('RGB', (300, 300), color='gray'), f"Error loading image information: {str(e)}", len(filtered_indices) - 1 |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Brain Tumor Image Dataset Viewer") |
|
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") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
image_output = gr.Image(label="Annotated Image") |
|
with gr.Column(scale=1): |
|
category_id_dropdown = gr.Dropdown(choices=[1, 2], value=1, label="Category ID") |
|
image_index = gr.Slider(minimum=0, maximum=0, step=1, value=0, label="Image ID Slider") |
|
info_output = gr.Textbox(label="Image Information", lines=10) |
|
|
|
def update_slider(category_id): |
|
img, info, max_index = draw_annotations(0, category_id) |
|
return gr.Slider.update(maximum=max_index), img, info |
|
|
|
|
|
category_id_dropdown.change(update_slider, inputs=category_id_dropdown, outputs=[image_index, image_output, info_output]) |
|
image_index.change(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index]) |
|
|
|
|
|
demo.load(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index]) |
|
|
|
demo.launch(debug=True) |
|
|