-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_clean_json.py
53 lines (39 loc) · 1.62 KB
/
data_clean_json.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
import json
from pathlib import Path
import os
import copy
def open_json(ufo_path: str) -> dict:
with Path(ufo_path).open(encoding='utf8') as f:
ufo_data = json.load(f)
return ufo_data
def save_json(ufo_data: dict, output_path: str) -> None:
with open(output_path, 'w') as f:
json.dump(ufo_data, f, indent=2, ensure_ascii=False)
print(f"saved json at {output_path}")
def create_json_without_(ufo_path: str, output_path: str, key: str="") -> None:
ufo_data = open_json(ufo_path)
total = 0
img_files = ufo_data['images'].keys()
for img in img_files:
items = copy.deepcopy(ufo_data['images'][img]['words'])
for num, word in ufo_data['images'][img]['words'].items():
if word['transcription'] == key:
# print(f"erased : {img}, {num}, {word}")
total += 1
del items[num]
ufo_data['images'][img]['words'] = items
print(f"from {ufo_path}, total {total} bboxs with transcription='{key}' were erased")
save_json(ufo_data, output_path)
if __name__=="__main__":
base_data_path = './data'
base_ufo_path = 'ufo/train.json'
paths = ['vietnamese_receipt',
'chinese_receipt',
'japanese_receipt',
'thai_receipt']
output_file_name = 'erased_empty_transcription.json'
key = ""
for path in paths:
ufo_path = os.path.join(base_data_path, path, base_ufo_path)
output_path = ufo_path.replace('train.json', output_file_name)
create_json_without_(ufo_path=ufo_path, output_path=output_path, key=key)