-
Notifications
You must be signed in to change notification settings - Fork 12
/
addTranscriptionsToJsons.py
248 lines (230 loc) · 10.3 KB
/
addTranscriptionsToJsons.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import json,csv,sys,os
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
import matplotlib.patches as patches
from matplotlib import gridspec
import numpy as np
DEBUG=False
if DEBUG:
print('DEBUG')
def fixBox(ann,group,imagename):
#points=ann['poly_points']
#image = mpimg.imread(os.path.join('groups',group,imagename+'.jpg'))
#fig = plt.figure()
#gs = gridspec.GridSpec(1, 1)
#ax_im = plt.subplot(gs[0])
#ax_im.imshow(image,cmap='gray')
#box = patches.Polygon(np.array(points),linewidth=2,edgecolor='r',facecolor='none')
#ax_im.add_patch(box)
#plt.show()
trans = input('trans? (¦): ')
return ann, trans
with open(sys.argv[1]) as f:
head=None
json_path=None
cur_json_path=None
data=[]
if sys.argv[1].endswith('.csv'):
reader = csv.reader(f,delimiter=',',quotechar='"')
for ri,row in enumerate(reader):
if head is None:
head = {v:i for i,v in enumerate(row)}
continue
csvId = row[head['id']] if 'id' in head else row[head['ID']]
trans = row[head['Transcription']]
empty = 'FALSE'!=row[head['empty']] if 'empty' in head else False
bad_crop = 'FALSE'!=row[head['bad crop']] if 'bad crop' in head else False
illegible = 'FALSE'!=row[head['illegible']] if 'illegible' in head else False
data.append((csvId,trans,empty,bad_crop,illegible,row[head['image']],True,False))
elif sys.argv[1].endswith('.json'):
instances = json.load(f)
for i in instances:
if 'id' in i and 'gt' in i:
data.append((i['id'],i['gt'],False,False,False,i['context_image'],True,False))
elif 'gt' in i and 'matches' in i:
for csv_id in i['matches']:
data.append((csv_id,i['gt'],False,False,False,i['image'],True,len(i['matches'])>1))
elif 'pred' in i and 'matches' in i:
for csv_id in i['matches']:
data.append((csv_id,i['pred'],False,False,False,i['image'],False,False))
for csvId,trans,empty,bad_crop,illegible,image_name,corrected,no_strikethrough in data:
print(csvId)
group,image,bbId = csvId.split('-')
json_path = os.path.join('groups',group,image+'.json')
if json_path!=cur_json_path:
if cur_json_path is not None:
#json_data['fieldBBs']=list(fieldsById.values())
if 'raw tesseract transcriptions' in json_data:
del json_data['raw tesseract transcriptions']
if DEBUG:
print('write {}'.format(cur_image))
with open('test/{}.json'.format(cur_image),'w') as out:
json.dump(json_data,out)
else:
with open(cur_json_path,'w') as out:
json.dump(json_data,out)
with open(json_path) as j:
json_data = json.load(j)
if 'transcriptions' not in json_data:
json_data['transcriptions']={}
fieldsById={}
for fieldBB in json_data['fieldBBs']:
fieldsById[fieldBB['id']]=fieldBB
textById={}
for textBB in json_data['textBBs']:
textById[textBB['id']]=textBB
cur_json_path=json_path
cur_image=image
assert(not any([x in trans for x in "«»¿§"]))
if bbId not in fieldsById and bbId not in textById:
#import pdb;pdb.set_trace()
continue
if len(trans)==0:
if illegible:
trans = "§"
elif empty:
pass
elif bad_crop:
print('!!!')
print('bad_crop: {} {}'.format(csvId,image_name))
fieldsById[bbId],newtrans=fixBox(fieldsById[bbId],group,image)
trans=newtrans
elif illegible:
print('{} marked as illegible, but trans is : {}'.format(csvId,trans))
elif empty:
print('{} marked as empty, but has trans: {}'.format(csvId,trans))
trans = input('Correct: ')
elif bad_crop:
print('bad_crop: {} {}'.format(csvId,image_name))
print(' but had trans: {}'.format(trans))
fieldsById[bbId],newtrans=fixBox(fieldsById[bbId],group,image)
trans=newtrans
while "\\" in trans and "'" in trans:
if "'\\s[" in trans and "]'" in trans:
newtrans = trans.replace("'\\s[","«")
newtrans=newtrans.replace("]'","»")
#print('Fix strikethrough: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "'\\?'" in trans:
newtrans=trans.replace("'\\?'","¿")
#print('Fix unknown: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "'\\illegible'" in trans:
newtrans=trans.replace("'\\illegible'","§")
#print('Fix illegible: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "'\\ilegible'" in trans:
newtrans=trans.replace("'\\ilegible'","§")
#print('Fix illegible: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "'\\empty'" in trans:
newtrans=trans.replace("'\\empty'","")
assert(len(newtrans)==0)
#print('Fix empty: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\s[" in trans and "]'" in trans:
newtrans = trans.replace("\\s[","«")
newtrans=newtrans.replace("]'","»")
#print('Fix strikethrough: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\?'" in trans:
newtrans=trans.replace("\\?'","¿")
#print('Fix unknown: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\illegible'" in trans:
newtrans=trans.replace("\\illegible'","§")
#print('Fix illegible: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\ilegible'" in trans:
newtrans=trans.replace("\\ilegible'","§")
#print('Fix illegible: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\empty'" in trans:
newtrans=trans.replace("\\empty'","")
assert(len(newtrans)==0)
#print('Fix empty: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\bad" in trans:
print('BAD: {}'.format(csvId))
fieldsById[bbId],newtrans=fixBox(fieldsById[bbId],group,image)
trans=newtrans
#elif '\\\\' in trans:
# trans=trans.replace('\\\\','\\')
#elif not corrected:
# trans=trans.replace('\\','')
elif ' \\' in trans:
print('Unknown case: {}'.format(trans))
trans = input('Correct: ')
else:
break
while "\\" in trans:
if "\\s[" in trans and "]" in trans:
newtrans = trans.replace("\\s[","«")
newtrans=newtrans.replace("]","»")
#print('Fix strikethrough: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "S\\[" in trans and "]" in trans:
newtrans = trans.replace("S\\[","«")
newtrans=newtrans.replace("]","»")
#print('Fix strikethrough: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "s\\[" in trans and "]" in trans:
newtrans = trans.replace("s\\[","«")
newtrans=newtrans.replace("]","»")
#print('Fix strikethrough: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\?" in trans:
newtrans=trans.replace("\\?","¿")
#print('Fix unknown: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\illegible" in trans:
newtrans=trans.replace("\\illegible","§")
#print('Fix illegible: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\empty" in trans:
newtrans=trans.replace("\\empty","")
assert(len(newtrans)==0)
#print('Fix empty: {} -> {}'.format(trans,newtrans))
trans=newtrans
elif "\\bad" in trans:
if bbId in fieldsById:
print('BAD: {}'.format(csvId))
fieldsById[bbId],newtrans=fixBox(fieldsById[bbId],group,image)
trans=newtrans
else:
trans=''
elif '\\\\' in trans:
trans=trans.replace('\\\\','\\')
elif not corrected:
trans=trans.replace('\\','')
elif ' \\' in trans:
print('Unknown case: {}'.format(trans))
trans = input('Correct: ')
else:
break
#otherwise assuming it's a newline marker
if bbId[0]=='f' and bbId in json_data['transcriptions'] and json_data['transcriptions'][bbId]!=trans:
if not corrected:
trans = json_data['transcriptions'][bbId]
corrected=True
else:
print('Different transcription. Prev: {} != New: {}'.format(json_data['transcriptions'][bbId],trans))
if json_data['transcriptions'][bbId]!='' and trans!='':
new_trans = input('Enter correct (leave blank to accept new): ')
if new_trans!='':
trans=new_trans
else:
trans = input('Enter correct: ')
elif not corrected and bbId in json_data['transcriptions']:
continue
if no_strikethrough:
trans = trans.replace('«','').replace('»','')
json_data['transcriptions'][bbId]=trans
#print('{} : {}'.format(bbId,trans))
#json_data['fieldBBs']=list(fieldsById.values())
if DEBUG:
with open('test/{}.json'.format(image),'w') as out:
json.dump(json_data,out)
else:
with open(cur_json_path,'w') as out:
json.dump(json_data,out)