Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,46 +3,7 @@ import openslide
|
|
3 |
|
4 |
from PIL import Image, ImageDraw
|
5 |
from xml.etree import ElementTree as ET
|
6 |
-
|
7 |
-
# accept slide thumbnail, x, y and annotation
|
8 |
-
def get_mask_from_xml(xml_path, image_size, image_shrinking_factor):
|
9 |
-
tree = ET.parse(xml_path)
|
10 |
-
root = tree.getroot()
|
11 |
-
image = Image.new("L", image_size, "white")
|
12 |
-
draw = ImageDraw.Draw(image)
|
13 |
-
draw.fill = True
|
14 |
-
label2grayscale_color = {"bg": 0, "tissue": 1, "tisuue": 1}
|
15 |
-
for i in root[0]:
|
16 |
-
annotation_type = i.attrib["Type"]
|
17 |
-
annotation_label = i.attrib["PartOfGroup"]
|
18 |
-
# there is roi rectangle
|
19 |
-
if annotation_type not in ["Spline", "Polygon", "Rectangle"]:
|
20 |
-
print(f"Annotation type must be either Spline, Rectangle or Polygon but found: {annotation_type}")
|
21 |
-
continue
|
22 |
-
|
23 |
-
if annotation_label not in label2grayscale_color:
|
24 |
-
print(f"Annotation label must be either tissue or bg but found: {annotation_label}")
|
25 |
-
continue
|
26 |
-
|
27 |
-
coordinates = [(i.attrib["X"], i.attrib["Y"]) for i in i[0]]
|
28 |
-
coordinates = [(str2float(x), str2float(y)) for x, y in coordinates]
|
29 |
-
coordinates = [(x*image_shrinking_factor, y*image_shrinking_factor) for x, y in coordinates]
|
30 |
-
|
31 |
-
if annotation_type in ["Spline", "Polygon"]:
|
32 |
-
draw.polygon(coordinates, fill=label2grayscale_color[annotation_label])
|
33 |
-
elif annotation_type == "Rectangle":
|
34 |
-
# ^
|
35 |
-
# | point 1 is bigger than point 3
|
36 |
-
# | 0 1
|
37 |
-
# | 3 2
|
38 |
-
# |------->
|
39 |
-
draw.rectangle([coordinates[3], coordinates[1]], fill=label2grayscale_color[annotation_label])
|
40 |
-
# if annotation_type == "Spline":
|
41 |
-
# draw.line(coordinates, fill=label2grayscale_color[annotation_label], width=1)
|
42 |
-
# elif annotation_type == "Polygon":
|
43 |
-
# draw.polygon(coordinates, fill=label2grayscale_color[annotation_label])
|
44 |
-
return image
|
45 |
-
|
46 |
|
47 |
# output as png or npy
|
48 |
def process(x, y, annotation_size, annotation):
|
|
|
3 |
|
4 |
from PIL import Image, ImageDraw
|
5 |
from xml.etree import ElementTree as ET
|
6 |
+
from project_utils.preprocessing import get_mask_from_xml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# output as png or npy
|
9 |
def process(x, y, annotation_size, annotation):
|