Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,11 @@ import numpy as np
|
|
| 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
|
|
@@ -22,9 +27,11 @@ def draw_annotations(index):
|
|
| 22 |
|
| 23 |
draw = ImageDraw.Draw(img)
|
| 24 |
|
| 25 |
-
# Draw bounding box
|
| 26 |
bbox = record["bbox"]
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Draw segmentation mask
|
| 30 |
segmentation = record["segmentation"]
|
|
@@ -32,13 +39,13 @@ def draw_annotations(index):
|
|
| 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}"
|
|
|
|
| 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 |
+
CATEGORY_COLORS = {
|
| 11 |
+
1: "red",
|
| 12 |
+
2: "green"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
def draw_annotations(index):
|
| 16 |
try:
|
| 17 |
# Fetch the image and annotations from the dataset
|
|
|
|
| 27 |
|
| 28 |
draw = ImageDraw.Draw(img)
|
| 29 |
|
| 30 |
+
# Draw bounding box with color based on category
|
| 31 |
bbox = record["bbox"]
|
| 32 |
+
category_id = record["category_id"]
|
| 33 |
+
box_color = CATEGORY_COLORS.get(category_id, "blue") # Default to blue if category not in mapping
|
| 34 |
+
draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline=box_color, width=2)
|
| 35 |
|
| 36 |
# Draw segmentation mask
|
| 37 |
segmentation = record["segmentation"]
|
|
|
|
| 39 |
draw.polygon(seg, outline="blue", width=2)
|
| 40 |
|
| 41 |
# Prepare additional information
|
|
|
|
| 42 |
area = record["area"]
|
| 43 |
file_name = record["file_name"]
|
| 44 |
|
| 45 |
info = f"File Name: {file_name}\n"
|
| 46 |
info += f"Image ID: {record['id']}\n"
|
| 47 |
info += f"Category ID: {category_id}\n"
|
| 48 |
+
info += f"Bounding Box Color: {box_color}\n"
|
| 49 |
info += f"Bounding Box: [{bbox[0]:.2f}, {bbox[1]:.2f}, {bbox[2]:.2f}, {bbox[3]:.2f}]\n"
|
| 50 |
info += f"Segmentation: {segmentation}\n"
|
| 51 |
info += f"Area: {area:.2f}"
|