forked from MjdMahasneh/MSLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Write_ARs_Xml_All.py
93 lines (57 loc) · 2.47 KB
/
Write_ARs_Xml_All.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import xml.etree.ElementTree as et
from pathlib import Path
def write_xml_into_existing_file(xml_file, img_name, top_x, top_y, bottom_x, bottom_y):
tree = et.parse(xml_file)
root = tree.getroot()
Active_region = et.SubElement(root, "Active_region", attrib={"observation_time": img_name})
Active_region_ID = et.SubElement(Active_region, "Active_region_ID")
top_X = et.SubElement(Active_region, "top_X")
top_Y = et.SubElement(Active_region, "top_Y")
bottom_X = et.SubElement(Active_region, "bottom_X")
bottom_Y = et.SubElement(Active_region, "bottom_Y")
Active_region_ID.text = "1"
top_X.text = str(top_x)
top_Y.text = str(top_y)
bottom_X.text = str(bottom_x)
bottom_Y.text = str(bottom_y)
tree.write(xml_file)
return()
def create_and_write_into_xml(xml_file):
filename = xml_file
root = et.Element("Data")
Active_region = et.Element("Active_region", attrib={"observation_time": "observation_time" })
root.append(Active_region)
Active_region_ID = et.SubElement(Active_region, "Active_region_ID")
top_X = et.SubElement(Active_region, "top_X")
top_Y = et.SubElement(Active_region, "top_Y")
bottom_X = et.SubElement(Active_region, "bottom_X")
bottom_Y = et.SubElement(Active_region, "bottom_Y")
Active_region_ID.text = "ID"
top_X.text = "top_X"
top_Y.text = "top_Y"
bottom_X.text = "bottom_X"
bottom_Y.text = "bottom_Y"
tree = et.ElementTree(root)
tree.write(filename)
return()
def Empty_img_old_info(xml_file, img_name):
tree = et.parse(xml_file)
root = tree.getroot()
for Active_region in root.findall('Active_region'):
observation_time = Active_region.get('observation_time')
if observation_time == img_name:
root.remove(Active_region)
tree.write(xml_file)
return()
def write_ARs(xml_file, img_name, rects):
my_file = Path(xml_file)
if my_file.is_file():
Empty_img_old_info(xml_file, img_name)
for AR in rects:
top_x, top_y, bottom_x, bottom_y = AR[0], AR[1], AR[0]+ AR[2], AR[1]+ AR[3]
write_xml_into_existing_file(xml_file, img_name, top_x, top_y, bottom_x, bottom_y)
else:
create_and_write_into_xml(xml_file)
for AR in rects:
top_x, top_y, bottom_x, bottom_y = AR[0], AR[1], AR[0]+ AR[2], AR[1]+ AR[3]
write_xml_into_existing_file(xml_file, img_name, top_x, top_y, bottom_x, bottom_y)