-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
377 lines (288 loc) · 11.9 KB
/
utils.py
File metadata and controls
377 lines (288 loc) · 11.9 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import utils
import pickle
import os
import random
from tqdm import tqdm
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import glob
import img2pdf
import numpy as np
class Combinator:
def __init__(self,score_to_id, target, id_used, papers_used ,mcq = True):
self.score_to_id = score_to_id
self.target = target
self.papers_used = papers_used
self.id_used = id_used
self.mcq = mcq
self.combinations = []
self.score_dict_ = {}
def backtrack(self,candidates ,cur, pos, target):
if target==0:
self.combinations.append(cur.copy())
if target<=0:
return
prev = -1
for i in range(pos, len(candidates)):
if candidates[i]==prev:
continue
cur.append(candidates[i])
self.backtrack(candidates, cur, i+1, target- candidates[i])
cur.pop()
prev = candidates[i]
def generate_combinations(self):
candidates = []
for score in self.score_to_id:
self.score_dict_[int(score)] = len(self.score_to_id[score])
for i in range(len(self.score_to_id[score])):
candidates.append(int(score))
candidates = sorted(candidates)
self.backtrack(candidates,[], 0, self.target)
if self.mcq:
for i in range(self.score_dict_[1]//self.target):
self.combinations.append([1]*self.target)
if len(self.combinations)<self.papers_used*2:
self.combinations*=self.papers_used
np.random.shuffle(self.combinations)
# print(self.combinations)
# if len(self.combinations)>1000:
# self.combinations = self.combinations[:1000]
# print(len(self.combinations))
combo_list = []
for pos in range(len(self.combinations)):
score_dict = self.score_dict_.copy()
combo = []
for i in range(pos, len(self.combinations)):
use_combo = True
for x in range(len(self.combinations[i])):
score = self.combinations[i][x]
if score_dict[score]>0:
score_dict[score]-=1
else:
use_combo = False
for c in range(x):
score_dict[self.combinations[i][c]]+=1
break
if use_combo:
combo.append(self.combinations[i])
if len(combo)==self.papers_used:
break
print(score_dict)
if len(combo) == self.papers_used:
combo_list.append(combo)
if len(combo_list)>self.papers_used:
break
return combo_list
def get_idlist_of_combinations(self,combos):
combination_ids = []
for combination in combos:
ids = []
for score in combination:
for id_ in self.score_to_id[str(score)]:
if not self.id_used[id_][0]:
self.id_used[id_][0] = True
ids.append(id_)
break
combination_ids.append(ids)
return combination_ids
# datafiles_struct =
# [qs_score_to_id, qs_id_to_img, qs_id_used,mcq_score_to_id, mcq_id_to_img, mcq_id_used, num_papers, ms_id_to_img, qs_target_mark, mcq_target_mark]
def randomize_score_to_ID(score_to_id):
if len(score_to_id):
for score in score_to_id.keys():
# print(score_to_id[score])
for i in range(random.randint(2,4)):
random.shuffle(score_to_id[score])
# print(score_to_id[score])
return score_to_id
def get_combination(qs_data):
qs_target = qs_data[-2]
mcq_target = qs_data[-1]
qs_score_to_id = randomize_score_to_ID(qs_data[0])
mcq_score_to_id = randomize_score_to_ID(qs_data[3])
# total = 0
# for score in qs_score_to_id.keys():
# total+=int(score)*len(qs_score_to_id[score])
# print(total/110)
# qs_id_to_img = qs_data[1]
# mcq_id_to_img = qs_data[4]
qs_id_used = qs_data[2]
mcq_id_used = qs_data[5]
num_papers_used = qs_data[-4]
return_values = []
# variables to change when sub is changed, papers_used
qs_combinator= Combinator(qs_score_to_id, qs_target, qs_id_used,papers_used = num_papers_used,mcq = False)
qs_combo_list =qs_combinator.generate_combinations()
qs_combos = qs_combo_list[random.randint(0, len(qs_combo_list)-1)]
qs_combo_ids = qs_combinator.get_idlist_of_combinations(qs_combos)
return_values.append(qs_combo_ids)
if mcq_target:
mcq_combinator = Combinator(mcq_score_to_id, mcq_target, mcq_id_used, papers_used = num_papers_used,mcq = True)
mcq_combo_list = mcq_combinator.generate_combinations()
mcq_combos = mcq_combo_list[random.randint(0, len(mcq_combo_list)-1)]
mcq_combos_2 = mcq_combo_list[random.randint(0, len(mcq_combo_list)-1)]
mcq_combo_ids = mcq_combinator.get_idlist_of_combinations(mcq_combos)
for key in mcq_id_used.keys():
mcq_id_used[key][0] = False
mcq_combo_ids_2 = mcq_combinator.get_idlist_of_combinations(mcq_combos_2)
return_values.append(mcq_combo_ids)
return_values.append(mcq_combo_ids_2)
else:
return_values.append(None)
return_values.append(None)
return return_values
def save_combination_file(combo_save_file, subject_combinations, current_combo_index):
pass
def retrieve_or_generate_combinations(combo_save_file, files, subject_index, qs_data):
if not os.path.exists(combo_save_file):
subject_combinations = {f"{files[subject_index].replace('.pickle','')}": get_combination(qs_data)}
current_combo_index = {f"{files[subject_index].replace('.pickle','')}":[0,0,0]}
print("saving combinations for later use...")
pickle.dump([subject_combinations, current_combo_index], open(combo_save_file,'wb'))
else:
print("\nLoading...")
# NOTE: saved_combinations_file = [subject_combination_dictionary, current_combo_index_dictionary]
# subject_combination_dictionary = {subject_name: [[qs_combo_lists],[mcq_combo_lists]], ...}
# current_combo_index_dictionary = {subject_name: [qs_combo_index, mcq_combo_index]}
saved_combinations_file = pickle.load(open(combo_save_file,'rb'))
subject_combinations = saved_combinations_file[0]
current_combo_index = saved_combinations_file[1]
if files[subject_index].replace('.pickle','') in subject_combinations.keys():
print("The subject combination is in the file")
else:
print("Not in file, thus generating combinations")
subject_combinations[f"{files[subject_index].replace('.pickle','')}"] = get_combination(qs_data)
current_combo_index[f"{files[subject_index].replace('.pickle','')}"] = [0,0,0]
print("Saving combinations for later use...")
pickle.dump([subject_combinations, current_combo_index], open(combo_save_file,'wb'))
return [subject_combinations, current_combo_index]
def reverse_sort_img_files(img_files):
files = []
path_origin = ''
for i in img_files:
split_list = i.split('/')
files.append(int( split_list[-1].replace(".jpeg", '') ))
if path_origin == '':
for x in split_list[:-1]:
path_origin+=x+'/'
files = sorted(files, reverse = True)
# del split_list
img_files = [path_origin+str(i)+".jpeg" for i in files]
return img_files
def create_pages(mcq_combination_ids, mcq_id_to_img, mcq_id_used ,qs_combination_ids, qs_id_to_img, qs_id_used, ms_id_to_img,front_page,full_test = True ,v_lim = 1490, h_fixed = 1054, output_qs_file = f'test.pdf',output_ms_file = 'test_ms.pdf'):
pages = []
pages_ms = []
qs_num = 1
image = None
if mcq_combination_ids is not None:
for id_ in mcq_combination_ids:
mark = mcq_id_used[id_][1]
for i in range(len(mcq_id_to_img[id_])):
img = mcq_id_to_img[id_][i]
img = cv2.cvtColor(cv2.resize(img, (h_fixed, img.shape[0])), cv2.COLOR_GRAY2RGB)
if i == 0:
# Adding number and white bg to the number only used if first page of id
cv2.rectangle(img, (0,0), (45,80), color = (255,255,255),thickness = -1)
cv2.putText(img,f'{qs_num}.',(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,0,2,2)
if i == len(mcq_id_to_img[id_])-1:
# adding marks of the qs and white bg for mark, only used of last page of id
cv2.rectangle(img, (0, img.shape[0]-25), (img.shape[1], img.shape[0]), color = (255,255,255),thickness = -1)
if int(mark)>1:
mt = 'marks'
else:
mt = 'mark'
sentence = f"(Total for question {qs_num} is {mark} {mt})"
cv2.putText(img, sentence, (img.shape[1]-600,img.shape[0]-10), cv2.FONT_HERSHEY_SIMPLEX, 1,0,2,2)
if image is None:
image = img
# print(image.shape)
elif (image.shape[0]+img.shape[0])<=v_lim:
print(f"joint: {image.shape[0]+img.shape[0]}")
image = np.concatenate((image, img), axis = 0)
else:
# print('Adding pages together...')
image = cv2.copyMakeBorder(image, 0, abs(v_lim-image.shape[0]), 0, 0, cv2.BORDER_CONSTANT, None, [255,255,255])
pages.append(image)
image = img
for i in range(len(ms_id_to_img[id_])):
img = ms_id_to_img[id_][i]
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
if i == 0:
# Adding number and white bg to the number only used if first page of id
cv2.rectangle(img, (0,0), (45,80), color = (255,255,255),thickness = -1)
cv2.putText(img,f'{qs_num}.',(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,0,2,2)
pages_ms.append(img)
qs_num+=1
image = cv2.copyMakeBorder(image, 0, abs(v_lim-image.shape[0]), 0, 0, cv2.BORDER_CONSTANT, None, [255,255,255])
pages.append(image)
image = None
if full_test:
print("full test")
for id_ in qs_combination_ids:
mark = qs_id_used[id_][1]
for i in range(len(qs_id_to_img[id_])):
img = qs_id_to_img[id_][i]
img = cv2.cvtColor(cv2.resize(img, (h_fixed, img.shape[0])), cv2.COLOR_GRAY2RGB)
if i == 0:
# Adding number and white bg to the number only used if first page of id
cv2.rectangle(img, (0,0), (45,80), color = (255,255,255),thickness = -1)
cv2.putText(img,f'{qs_num}.',(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,0,2,2)
if i == len(qs_id_to_img[id_])-1:
# adding marks of the qs and white bg for mark, only used of last page of id
cv2.rectangle(img, (0, img.shape[0]-25), (img.shape[1], img.shape[0]), color = (255,255,255),thickness = -1)
if int(mark)>1:
mt = 'marks'
else:
mt = 'mark'
sentence = f"(Total for question {qs_num} is {mark} {mt})"
cv2.putText(img, sentence, (img.shape[1]-600,img.shape[0]-10), cv2.FONT_HERSHEY_SIMPLEX, 1,0,2,2)
if image is None:
image = img
elif image.shape[0]+img.shape[0]<=v_lim:
image = np.concatenate((image, img), axis = 0)
else:
image = cv2.copyMakeBorder(image, 0, abs(v_lim-image.shape[0]), 0, 0, cv2.BORDER_CONSTANT, None, [255,255,255])
pages.append(image)
image = img
for i in range(len(ms_id_to_img[id_])):
img = ms_id_to_img[id_][i]
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
if i == 0:
# Adding number and white bg to the number only used if first page of id
cv2.rectangle(img, (0,0), (45,80), color = (255,255,255),thickness = -1)
cv2.putText(img,f'{qs_num}.',(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,0,2,2)
pages_ms.append(img)
qs_num+=1
image = cv2.copyMakeBorder(image, 0, abs(v_lim-image.shape[0]), 0, 0, cv2.BORDER_CONSTANT, None, [255,255,255])
pages.append(image)
image = None
last_page = pages[-1]
cv2.rectangle(last_page, (10,v_lim-30), (396,v_lim), color = (0,200,0),thickness = -1)
cv2.putText(last_page,f'IAO TestSuite by Ishraque sarwar',(10, v_lim-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,0,2,2)
pages[-1] = last_page
for i in range(len(pages)):
cv2.imwrite(f'Lab/{i}.jpeg', pages[i])
files = reverse_sort_img_files(glob.glob("Lab/*.jpeg"))[::-1]
cv2.imwrite('Lab/front_page.jpeg', front_page)
files.insert(0, 'Lab/front_page.jpeg')
with open(output_qs_file,'wb')as f:
f.write(img2pdf.convert(files))
for file in tqdm(files):
os.remove(file)
# for i in range(len(pages)):
# cv2.imwrite(f'Lab/{i}.jpeg', pages_ms[i])
# files = reverse_sort_img_files(glob.glob("Lab/*.jpeg"))[::-1]
# cv2.imwrite('Lab/front_page.jpeg', front_page)
# files.insert(0, 'Lab/front_page.jpeg')
# with open(output_ms_file,'wb')as f:
# f.write(img2pdf.convert(files))
# for file in tqdm(files):
# os.remove(file)
for i in range(len(pages_ms)):
cv2.imwrite(f'Lab/{i}.jpeg', pages_ms[i])
files = reverse_sort_img_files(glob.glob("Lab/*.jpeg"))[::-1]
with open(output_ms_file,'wb')as f:
f.write(img2pdf.convert(files))
for file in tqdm(files):
os.remove(file)