osbm commited on
Commit
2967659
1 Parent(s): 3b307ab

add everything

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1,15 +1,69 @@
1
  import gradio as gr
2
  import openslide
3
 
 
 
4
 
5
- def process(text):
6
- return text[::-1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
  demo = gr.Interface(
10
  fn=process,
11
- inputs="text",
12
- outputs="text",
 
 
 
 
 
 
13
  title="Reverse Text",
14
  description="Reverses the text entered by the user",
15
 
 
1
  import gradio as gr
2
  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):
49
+ image_shrinking_factor = annotation_size / min(x, y)
50
+
51
+ # get thumbnail
52
+ image = get_mask_from_xml(annotation, (annotation_size, annotation_size), image_shrinking_factor)
53
+ image.save("mask.png")
54
+ return "mask.png"
55
 
56
 
57
  demo = gr.Interface(
58
  fn=process,
59
+ inputs=[
60
+ # gr.File(label="Slide thumbnail", type="file", accept=".png"),
61
+ gr.Number(label="X", default=10_000),
62
+ gr.Number(label="Y", default=10_000),
63
+ gr.Number(label="Thumbnail size", default=500),
64
+ gr.File(label="ASAP Annotation", type="file", accept=".xml"),
65
+ ],
66
+ outputs="image",
67
  title="Reverse Text",
68
  description="Reverses the text entered by the user",
69