forked from elmohafez/ayah-detection
/
sqlite_encoder.py
executable file
·251 lines (221 loc) · 7.59 KB
/
sqlite_encoder.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
249
250
251
#!/usr/bin/env python2
import sys
import sqlite3
import base64
import argparse
from tempfile import mkstemp
from glob import glob
import os
import filecmp
import csv
from tqdm import tqdm
from utils import safe_makedir
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--input_path', type=str, required=True,
help='''Path to input folder containing segmented data SQL files to import''')
parser.add_argument('--output_path', type=str, required=True,
help='''Path to output folder to generate platform specific files into''')
parser.add_argument('--reference_width', type=float, required=True,
help='''Reference width of which segmented data SQL files were generated''')
parser.add_argument('--recitation_id', type=int, required=True,
help='''Recitation ID of the segmented data''')
parser.add_argument('--update_previous_recitation', action='store_true',
help='''If set, will expect a previously generated tsv file in the input_path''')
return parser.parse_args()
def create_db():
print("Creating database...")
_, db_file_name = mkstemp()
print("Created database at " + db_file_name)
conn = sqlite3.connect(db_file_name)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE "glyphs" (
'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'pageId' INTEGER,
'lineId' INTEGER,
'suraId' INTEGER,
'verseId' INTEGER,
'indexId' INTEGER,
'left' INTEGER,
'right' INTEGER,
'top' INTEGER,
'bottom' INTEGER);
''')
cursor.execute('''
CREATE TABLE "encoded_glyphs" (
'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'pageId' INTEGER,
'lineId' INTEGER,
'suraId' INTEGER,
'verseId' INTEGER,
'data' TEXT);
''')
cursor.execute('''
CREATE TABLE "results" (
'pageId' INTEGER,
'suraId' INTEGER,
'verseId' INTEGER,
'lineId' INTEGER,
'data' TEXT);
''')
return conn
def import_data(conn, sql_dir):
print("Importing SQL files in " + sql_dir + "...")
cursor = conn.cursor()
for file_name in tqdm(glob(sql_dir + "/*.sql")):
with open(file_name, "rb") as file:
cursor.executescript(file.read())
conn.commit()
def group_data(conn):
print("Grouping data...")
cursor = conn.cursor()
cursor.execute('''
INSERT INTO encoded_glyphs (
pageId,
lineId,
suraId,
verseId,
data
)
SELECT
pageId,
min(lineId) lineId,
suraId,
verseId,
'[' || group_concat('"' || left || ',' || top || ',' || right || ',' || bottom || '"', ',') || ']' data
FROM glyphs
GROUP BY
suraid,
verseid,
pageid
ORDER BY
suraid,
verseid,
pageid;
''')
conn.commit()
def encode_data(conn, width, ref_width):
encoded_data = []
ratio = width / ref_width
cursor = conn.cursor()
for row in cursor.execute('SELECT * FROM encoded_glyphs ORDER BY pageid, suraid, verseid'):
data = row[5]
data = data.replace('[', '').replace(']', '')
rects = data.split('","')
data = ""
for rect in rects:
coords = rect.replace('"', '')
coords = coords.split(',')
if len(data) > 0:
data += ","
left = int(round(int(coords[0]) * ratio))
top = int(round(int(coords[1]) * ratio))
right = int(round(int(coords[2]) * ratio))
bottom = int(round(int(coords[3]) * ratio))
data += str(left << 48 | top << 32 | right << 16 | bottom)
data = base64.b64encode(data)
encoded_data.append((row[1], row[2], row[3], row[4], data))
return encoded_data
def prepare_results(conn, previous_tsv=None, page_ids=[]):
cursor = conn.cursor()
cursor.execute('DELETE FROM results')
if previous_tsv:
if os.path.isfile(previous_tsv):
# open file in universal new line mode: U
with open(previous_tsv, 'rU') as csvfile:
reader = csv.reader(csvfile, delimiter='\t', quotechar='"')
# file format: rewayaId pageId suraId verseId lineId data
next(reader) # skip header
data = [(int(row[1]), int(row[4]), int(row[2]), int(row[3]), row[5]) for row in reader]
insert_encoded_data(conn, data)
# delete previous pages matching new pages
if page_ids:
cursor.execute('DELETE FROM results WHERE pageId IN (%s)' %
(','.join(['?'] * len(page_ids))), page_ids)
else:
raise RuntimeError("Could not find previously generated tsv file at %s" % previous_tsv)
def insert_encoded_data(conn, data):
cursor = conn.cursor()
cursor.executemany('''
INSERT INTO results (
pageId,
lineId,
suraId,
verseId,
data
)
VALUES (?, ?, ?, ?, ?)
''', data)
conn.commit()
def export_data_sql(conn, recitation_id, file_name, page_ids=[]):
cursor = conn.cursor()
with open(file_name, 'wb') as file:
total = 0
if page_ids:
file.write('DELETE FROM recitations_data WHERE recitation_id = %s AND page_id IN (%s)\n' %
(recitation_id, ', '.join(map(str, page_ids))))
for row in cursor.execute('SELECT pageId, lineId, suraId, verseId, data FROM results ORDER BY pageId, lineId, suraId, verseId'):
file.write(
"INSERT INTO recitations_data (recitation_id, page_id, line_id, sura_id, verse_id, data) VALUES (%s, %s, %s, %s, %s, '%s')\n" \
% (recitation_id, row[0], row[1], row[2], row[3], row[4]))
total += 1
print("Exported %d records to %s" % (total, file_name))
def export_data_tsv(conn, recitation_id, file_name):
cursor = conn.cursor()
with open(file_name, 'wb') as file:
writer = csv.writer(file, delimiter='\t', lineterminator='\r',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow([
'rewayaId',
'pageId',
'suraId',
'verseId',
'lineId',
'data'
])
total = 0
for row in cursor.execute('SELECT pageId, lineId, suraId, verseId, data FROM results ORDER BY pageId, lineId, suraId, verseId'):
writer.writerow([
recitation_id,
row[0],
row[2],
row[3],
row[1],
row[4]
])
total += 1
print("Exported %d records to %s" % (total, file_name))
def get_unique_pages(conn):
cursor = conn.cursor()
return [int(row[0]) for row in cursor.execute('SELECT distinct pageId from glyphs ORDER BY pageId')]
def get_tsv_path(base_dir, recitation_id):
return "%s/RecitationData%d.tsv" % (base_dir, recitation_id)
if __name__ == "__main__":
args = parse_arguments()
conn = create_db()
import_data(conn, args.input_path)
group_data(conn)
page_ids = get_unique_pages(conn)
out_dir = safe_makedir(args.output_path)
# tsv
previous_tsv = None
if args.update_previous_recitation:
previous_tsv = get_tsv_path(args.input_path, args.recitation_id)
prepare_results(conn, previous_tsv, page_ids)
insert_encoded_data(conn, encode_data(conn, 800, args.reference_width))
tsv_filename = get_tsv_path(out_dir, args.recitation_id)
export_data_tsv(conn, args.recitation_id, tsv_filename)
if previous_tsv and filecmp.cmp(previous_tsv, tsv_filename):
print("No change was detected since the previous recitation, exiting")
os.remove(tsv_filename)
else:
# sql
if not args.update_previous_recitation:
page_ids = []
for width in [320, 480, 800, 1200, 1500]:
prepare_results(conn, None, page_ids)
insert_encoded_data(conn, encode_data(conn, width, args.reference_width))
export_data_sql(conn, args.recitation_id, "%s/%d.sql" %
(out_dir, width), page_ids)
conn.close()