Update app.py
#1
by
dwb2023
- opened
app.py
CHANGED
@@ -8,7 +8,7 @@ from rdp import rdp
|
|
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:
|
12 |
return []
|
13 |
epsilon = 1.0
|
14 |
simplified = rdp(np.array(segmentation), epsilon=epsilon)
|
@@ -19,38 +19,38 @@ def simplify_segmentation(segmentation, max_points=20):
|
|
19 |
|
20 |
def draw_annotations(index):
|
21 |
try:
|
|
|
22 |
record = dataset[index]
|
23 |
|
|
|
24 |
if isinstance(record['image'], np.ndarray):
|
25 |
img = Image.fromarray(record['image'])
|
26 |
else:
|
27 |
img = record['image']
|
28 |
|
29 |
-
img = img.convert("RGB")
|
|
|
30 |
draw = ImageDraw.Draw(img)
|
31 |
|
32 |
# Draw bounding box
|
33 |
bbox = record["bbox"]
|
34 |
draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], outline="red", width=2)
|
35 |
|
36 |
-
# Draw segmentation mask
|
37 |
segmentation = record["segmentation"]
|
38 |
original_points = 0
|
39 |
simplified_points = 0
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
for seg in simplified_segmentation:
|
50 |
-
if len(seg) > 0:
|
51 |
-
draw.polygon(seg, outline="green", width=2)
|
52 |
-
simplified_points += len(seg)
|
53 |
|
|
|
54 |
category_id = record["category_id"]
|
55 |
area = record["area"]
|
56 |
file_name = record["file_name"]
|
|
|
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 []
|
13 |
epsilon = 1.0
|
14 |
simplified = rdp(np.array(segmentation), epsilon=epsilon)
|
|
|
19 |
|
20 |
def draw_annotations(index):
|
21 |
try:
|
22 |
+
# Fetch the image and annotations from the dataset
|
23 |
record = dataset[index]
|
24 |
|
25 |
+
# Convert image to PIL Image if it's a numpy array
|
26 |
if isinstance(record['image'], np.ndarray):
|
27 |
img = Image.fromarray(record['image'])
|
28 |
else:
|
29 |
img = record['image']
|
30 |
|
31 |
+
img = img.convert("RGB") # Ensure the image is in RGB mode
|
32 |
+
|
33 |
draw = ImageDraw.Draw(img)
|
34 |
|
35 |
# Draw bounding box
|
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 original segmentation mask
|
40 |
segmentation = record["segmentation"]
|
41 |
original_points = 0
|
42 |
simplified_points = 0
|
43 |
+
for seg in segmentation:
|
44 |
+
if seg: # Check if the segmentation is not empty
|
45 |
+
draw.polygon(seg, outline="blue", width=2)
|
46 |
+
original_points += len(seg)
|
47 |
+
|
48 |
+
# Simplify and draw simplified segmentation
|
49 |
+
simplified_seg = simplify_segmentation(seg)
|
50 |
+
draw.polygon(simplified_seg, outline="green", width=2)
|
51 |
+
simplified_points += len(simplified_seg)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Prepare additional information
|
54 |
category_id = record["category_id"]
|
55 |
area = record["area"]
|
56 |
file_name = record["file_name"]
|