forked from NiceRingNode/Awesome-Generative-Models-for-OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
170 lines (165 loc) · 7.99 KB
/
Copy pathprocess.py
File metadata and controls
170 lines (165 loc) · 7.99 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
import os,re,json,glob
from natsort import natsorted
from pathlib import Path
test_cases = []
cnt = 0
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp', '*.gif']
records = {}
subfields = os.listdir('./OCRGenBench')
for field in subfields:
if field == 'layout':
files = [str(f) for f in Path(f'./OCRGenBench/{field}/').rglob('*') if f.is_file()]
files = list(filter(lambda x:not x.endswith('.psd'),files))
json_paths = list(filter(lambda x:x.endswith('.json'),files))
images = list(set(files) - set(json_paths))
gt_images = list(filter(lambda x:'-gt' in x,images))
input_images = list(set(images) - set(gt_images))
for json_path in natsorted(json_paths):
name = json_path.split('/')[-1].replace('.json','')
with open(json_path,'r',encoding='utf-8') as f:
data = json.load(f)
if len(gt_images) > 0:
gt_matches = [path for path in gt_images if Path(path).stem == f"{name}-gt"]
gt_image = gt_matches[0]
else:
gt_image = None
matched_paths = [path for path in input_images if Path(path).stem == name]
input_image = matched_paths[0]
test_cases.append({
'id': cnt,
"field": field,
"task": 'Layout-Gen',
"prompt": data['prompt'],
"input_image_path_1": input_image,
"input_image_path_2": None,
"gt_image_path": gt_image,
'task_type': 'editing',
'output_path': f'./output/holder/{field}/{name}.png'
})
cnt += 1
records['layout'] = {'task':len(json_paths)}
for task in os.listdir(f'./OCRGenBench/{field}'):
inner_cnt = 0
if field == 'historical' and task.lower() == 'style-transfer':
files = [str(f) for f in Path(f'./OCRGenBench/{field}/{task}').rglob('*') if f.is_file()]
files = list(filter(lambda x:not x.endswith('.psd'),files))
json_paths = list(filter(lambda x:x.endswith('.json'),files))
for json_path in natsorted(json_paths):
name = json_path.split('/')[-1].replace('.json','')
with open(json_path,'r',encoding='utf-8') as f:
data = json.load(f)
images = glob.glob(f'./OCRGenBench/{field}/{task}/images/{name}/*')
content_image = list(filter(lambda x:'content_' in x,images))[0]
style_image = list(filter(lambda x:'style_' in x,images))[0]
test_cases.append({
'id': cnt,
"field": field,
"task": 'Historical-Style-Transfer',
"prompt": data['prompt'],
"input_image_path_1": content_image,
"input_image_path_2": style_image,
'task_type': 'editing',
'output_path': f'./output/holder/{field}/{task}/{name}.png'
})
cnt += 1
inner_cnt += 1
elif field == 'artistic' and task.lower() == 'style-transfer':
files = [str(f) for f in Path(f'./OCRGenBench/{field}/{task}').rglob('*') if f.is_file()]
files = list(filter(lambda x:not x.endswith('.psd'),files))
json_paths = list(filter(lambda x:x.endswith('.json'),files))
images = list(set(files) - set(json_paths))
gt_images = list(filter(lambda x:'-gt' in x,images))
input_images = list(set(images) - set(gt_images))
for json_path in natsorted(json_paths):
name = json_path.split('/')[-1].replace('.json','')
with open(json_path,'r',encoding='utf-8') as f:
data = json.load(f)
if len(gt_images) > 0:
gt_matches = [path for path in gt_images if Path(path).stem == f"{name}-gt"]
gt_image = gt_matches[0]
else:
gt_image = None
matched_paths = [path for path in input_images if Path(path).stem == name]
input_image = matched_paths[0]
annotations = data['annotations'] if 'annotations' in data else None
test_cases.append({
'id': cnt,
"field": field,
"task": 'Artistic-Style-Transfer',
"prompt": data['prompt'],
"input_image_path_1": input_image,
"input_image_path_2": None,
"gt_image_path": gt_image,
'annotations': annotations,
'task_type': 'editing',
'output_path': f'./output/holder/{field}/{task}/{name}.png'
})
cnt += 1
inner_cnt += 1
elif 'T2I' in task:
json_paths = [str(f) for f in Path(f'./OCRGenBench/{field}/{task}').rglob('*') if f.is_file()]
for json_path in natsorted(json_paths):
name = json_path.split('/')[-1].replace('.json','')
with open(json_path,'r',encoding='utf-8') as f:
data = json.load(f)
test_cases.append({
'id': cnt,
"field": field,
"task": task,
"prompt": data['prompt'],
"input_image_path_1": None,
"input_image_path_2": None,
'task_type': "generation",
'output_path': f'./output/holder/{field}/{task}/{name}.png'
})
cnt += 1
inner_cnt += 1
else:
files = [str(f) for f in Path(f'./OCRGenBench/{field}/{task}').rglob('*') if f.is_file()]
files = list(filter(lambda x:not x.endswith('.psd'),files))
json_paths = list(filter(lambda x:x.endswith('.json'),files))
images = list(set(files) - set(json_paths))
gt_images = list(filter(lambda x:'-gt' in x,images))
input_images = list(set(images) - set(gt_images))
for json_path in natsorted(json_paths):
name = json_path.split('/')[-1].replace('.json','')
with open(json_path,'r',encoding='utf-8') as f:
data = json.load(f)
if len(gt_images) > 0:
gt_matches = [path for path in gt_images if Path(path).stem == f"{name}-gt"]
gt_image = gt_matches[0]
else:
gt_image = None
matched_paths = [path for path in input_images if Path(path).stem == name]
input_image = matched_paths[0]
annotations = data['annotations'] if 'annotations' in data else None
if task == 'Removal' and field == 'handwriting':
cur_task_name = 'Handwriting-Removal'
elif task == 'Removal' and field == 'scene':
cur_task_name = 'Scene-Text-Removal'
else:
cur_task_name = task
test_cases.append({
'id': cnt,
"field": field,
"task": cur_task_name,
"prompt": data['prompt'],
"input_image_path_1": input_image,
"input_image_path_2": None,
"gt_image_path": gt_image,
'annotations': annotations,
'task_type': 'editing',
'output_path': f'./output/holder/{field}/{task}/{name}.png'
})
cnt += 1
inner_cnt += 1
if records.get(field) is None:
records[field] = {}
records[field][task] = inner_cnt
os.makedirs('./data',exist_ok=True)
with open('data/test_cases.json','w',encoding='utf-8') as f:
json.dump(test_cases,f,indent=4,ensure_ascii=False)
for k in records:
print(k,sum(records[k].values()))
print('sum:',sum([v for k in records for v in records[k].values()]))