File size: 2,723 Bytes
3b307ab
 
 
2967659
 
3b307ab
2967659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b307ab
 
 
 
2967659
 
0f3eab2
 
 
9d0b212
2967659
 
3b307ab
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import openslide

from PIL import Image, ImageDraw
from xml.etree import ElementTree as ET

# accept slide thumbnail, x, y and annotation 
def get_mask_from_xml(xml_path, image_size, image_shrinking_factor):
    tree = ET.parse(xml_path)
    root = tree.getroot()
    image = Image.new("L", image_size, "white")
    draw = ImageDraw.Draw(image)
    draw.fill = True
    label2grayscale_color = {"bg": 0, "tissue": 1, "tisuue": 1}
    for i in root[0]:
        annotation_type = i.attrib["Type"]
        annotation_label = i.attrib["PartOfGroup"]
        # there is roi rectangle
        if annotation_type not in ["Spline", "Polygon", "Rectangle"]:
            print(f"Annotation type must be either Spline, Rectangle or Polygon but found: {annotation_type}")
            continue
            
        if annotation_label not in label2grayscale_color:
            print(f"Annotation label must be either tissue or bg but found: {annotation_label}")
            continue
        
        coordinates = [(i.attrib["X"], i.attrib["Y"]) for i in i[0]]
        coordinates = [(str2float(x), str2float(y)) for x, y in coordinates]
        coordinates = [(x*image_shrinking_factor, y*image_shrinking_factor) for x, y in coordinates]
        
        if annotation_type in ["Spline", "Polygon"]:
            draw.polygon(coordinates, fill=label2grayscale_color[annotation_label])
        elif annotation_type == "Rectangle":
            # ^
            # |         point 1 is bigger than point 3
            # | 0 1
            # | 3 2 
            # |------->
            draw.rectangle([coordinates[3], coordinates[1]], fill=label2grayscale_color[annotation_label])
#         if annotation_type == "Spline":
#             draw.line(coordinates, fill=label2grayscale_color[annotation_label], width=1)
#         elif annotation_type == "Polygon":
#             draw.polygon(coordinates, fill=label2grayscale_color[annotation_label])
    return image


# output as png or npy 
def process(x, y, annotation_size, annotation):
    image_shrinking_factor = annotation_size / min(x, y)

    # get thumbnail
    image = get_mask_from_xml(annotation, (annotation_size, annotation_size), image_shrinking_factor)
    image.save("mask.png")
    return "mask.png"


demo = gr.Interface(
    fn=process,
    inputs=[
        # gr.File(label="Slide thumbnail", type="file", accept=".png"),
        gr.Number(label="X", value=10_000),
        gr.Number(label="Y", value=10_000),
        gr.Number(label="Thumbnail size", value=500),
        gr.File(label="ASAP Annotation", file_types=[".xml"]),
    ],
    outputs="image",
    title="Reverse Text",
    description="Reverses the text entered by the user",

)

demo.launch()