-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_corr.py
79 lines (69 loc) · 3.13 KB
/
manual_corr.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
import json
import os
import click
from PIL import Image
from constants import XML_CLEAN, IMG
from src.bin.parser import ParserXML
from src.bin.terminal import prompt
@click.command()
@click.option("-i", "--image", "image", is_flag=True, show_default=True, default=False,
help="To appear jpg associated in folder \"img\"")
@click.option("-s", "--security", "security", is_flag=True, show_default=True, default=False,
help="Security confirmation to execute the change")
def correction(image, security):
"""
Cli function to do correction in mode manually. The function open json where data ambigus spe
:param image: Boolean, to activate image screen
:param security: Boolean, to activate confirmation mode of changement
:return: None
"""
img = None
# Open json correction missed
with open(os.path.join(XML_CLEAN, 'list_correction.json')) as json_file:
data_corr = json.load(json_file)
print("There are " + str(len([element for element in data_corr if element['manual'] is False])) + " items left to correct")
# File iteration
for file in os.listdir(XML_CLEAN):
if file.endswith(".xml"):
#instanciation Parser
xml = ParserXML(file=os.path.join(XML_CLEAN, file), mode="r+", automatic=False)
# Print jpg
if image:
try:
img = Image.open(os.path.join(IMG, file.replace(".xml", ".jpg")))
img.show()
except FileNotFoundError:
print("Img directory don't contain the image associated")
break
# order json by file and line number
data_corr.sort(key=lambda x: (x['file'], x['line']))
line_prev = None
dict_line = {}
for element in data_corr:
if element["file"] == file and element["manual"] is False:
if line_prev == element["line"]:
corr = prompt(element, security)
dict_line[corr[0]] = corr[1]
elif line_prev is None:
corr = prompt(element, security)
dict_line[corr[0]] = corr[1]
line_prev = element["line"]
elif line_prev != element["line"] and line_prev is not None:
xml.replacer(line_prev, dict_line)
dict_line = {}
corr = prompt(element, security)
dict_line[corr[0]] = corr[1]
line_prev = element["line"]
element["manual"] = True
with open(os.path.join(XML_CLEAN, 'list_correction.json'), "w") as json_write:
json.dump(data_corr, json_write, indent=3, ensure_ascii=False)
xml.replacer(line_prev, dict_line)
xml.xml_writer()
if image:
img.close()
# Verify if all corrections are done
if all([element["manual"] for element in data_corr]):
print("Correction is finished")
break
if __name__ == '__main__':
correction()