forked from Udayraj123/OMRChecker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.py
316 lines (266 loc) · 9.5 KB
/
template.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
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
"""
Designed and Developed by-
Udayraj Deshmukh
https://github.com/Udayraj123
"""
import cv2
import os
import json
import numpy as np
from globals import *
### Coordinates Part ###
class Pt():
"""
Container for a Point Box on the OMR
"""
"""
qNo is the point's property- question to which this point belongs to
It can be used as a roll number column as well. (eg roll1)
It can also correspond to a single digit of integer type Q (eg q5d1)
"""
def __init__(self, pt, qNo, qType, val):
self.x=round(pt[0])
self.y=round(pt[1])
self.qNo=qNo
self.qType=qType
self.val=val
class QBlock():
def __init__(self, dims, key, orig, traverse_pts):
# dims = (width, height)
self.dims = tuple(round(x) for x in dims)
self.key = key
self.orig = orig
self.traverse_pts = traverse_pts
# will be set when using
self.shift = 0
qtype_data = {
'QTYPE_MED':{
'vals' : ['E','H'],
'orient':'V'
},
'QTYPE_ROLL':{
'vals':range(10),
'orient':'V'
},
'QTYPE_INT':{
'vals':range(10),
'orient':'V'
},
'QTYPE_MCQ4':{
'vals' : ['A','B','C','D'],
'orient':'H'
},
'QTYPE_MCQ5':{
'vals' : ['A','B','C','D','E'],
'orient':'H'
},
# Add custom question types here-
# ,
# 'QTYPE_MCQ_COL_5A':{'vals' : ['A']*5, 'orient':'V'},
# 'QTYPE_MCQ_COL_5B':{'vals' : ['B']*5, 'orient':'V'},
# 'QTYPE_MCQ_COL_5C':{'vals' : ['C']*5, 'orient':'V'},
# 'QTYPE_MCQ_COL_5D':{'vals' : ['D']*5, 'orient':'V'},
# 'QTYPE_MCQ_COL_4A':{'vals' : ['A']*4, 'orient':'V'},
# 'QTYPE_MCQ_COL_4B':{'vals' : ['B']*4, 'orient':'V'},
# 'QTYPE_MCQ_COL_4C':{'vals' : ['C']*4, 'orient':'V'},
# 'QTYPE_MCQ_COL_4D':{'vals' : ['D']*4, 'orient':'V'},
}
class Template():
def __init__(self, jsonObj):
self.QBlocks = []
# throw exception on key not exist
self.dims = jsonObj["Dimensions"]
self.bubbleDims = jsonObj["BubbleDimensions"]
self.concats = jsonObj["Concatenations"]
self.singles = jsonObj["Singles"]
# Add custom options
self.options = jsonObj.get("Options", {})
# Add new qTypes from template
if "qTypes" in jsonObj:
qtype_data.update(jsonObj["qTypes"])
# Allow template to override globals
if "Globals" in jsonObj:
globals().update(jsonObj['Globals'])
for k, QBlocks in jsonObj['QBlocks'].items():
# Add QBlock to array of grids
self.addQBlocks(k, QBlocks)
# Expects bubbleDims to be set already
def addQBlocks(self, key, rect):
assert(self.bubbleDims != [-1, -1])
# For qType defined in QBlocks
if 'qType' in rect:
rect.update(**qtype_data[rect['qType']])
else:
rect['qType'] = {'vals':rect['vals'],
'orient': rect['orient']}
# keyword arg unpacking followed by named args
self.QBlocks += genGrid(self.bubbleDims, key, **rect)
# self.QBlocks.append(QBlock(rect.orig, calcQBlockDims(rect), maketemplate(rect)))
def genQBlock(bubbleDims, QBlockDims, key, orig, qNos, gaps, vals, qType, orient, col_orient):
"""
Input:
orig - start point
qNos - a tuple of qNos
gaps - (gapX,gapY) are the gaps between rows and cols in a block
vals - a 1D array of values of each alternative for a question
Output:
// Returns set of coordinates of a rectangular grid of points
Returns a QBlock containing array of Qs and some metadata?!
Ref:
1 2 3 4
1 2 3 4
1 2 3 4
(q1, q2, q3)
00
11
22
33
44
(q1.1,q1.2)
"""
H, V = (0,1) if(orient=='H') else (1,0)
# orig[0] += np.random.randint(-6,6)*2 # test random shift
Qs=[]
traverse_pts = []
o = [float(i) for i in orig]
if(col_orient == orient):
for q in range(len(qNos)):
pt = o.copy()
pts = []
for v in range(len(vals)):
pts.append(Pt(pt.copy(),qNos[q],qType,vals[v]))
pt[H] += gaps[H]
# For diagonalal endpoint of QBlock
pt[H] += bubbleDims[H] - gaps[H]
pt[V] += bubbleDims[V]
#TODO- make a mini object for this
traverse_pts.append(([o.copy(), pt.copy()], pts))
o[V] += gaps[V]
else:
for v in range(len(vals)):
pt = o.copy()
pts = []
for q in range(len(qNos)):
pts.append(Pt(pt.copy(),qNos[q],qType,vals[v]))
pt[V] += gaps[V]
# For diagonalal endpoint of QBlock
pt[V] += bubbleDims[V] - gaps[V]
pt[H] += bubbleDims[H]
#TODO- make a mini object for this
traverse_pts.append(([o.copy(), pt.copy()], pts))
o[H] += gaps[H]
# Pass first three args as is. only append 'traverse_pts'
return QBlock(QBlockDims, key, orig, traverse_pts)
def genGrid(bubbleDims, key, qType, orig, bigGaps, gaps, qNos, vals, orient='V', col_orient='V'):
"""
Input(Directly passable from JSON parameters):
bubbleDims - dimesions of single QBox
orig- start point
qNos - an array of qNos tuples(see below) that align with dimension of the big grid (gridDims extracted from here)
bigGaps - (bigGapX,bigGapY) are the gaps between blocks
gaps - (gapX,gapY) are the gaps between rows and cols in a block
vals - a 1D array of values of each alternative for a question
orient - The way of arranging the vals (vertical or horizontal)
Output:
// Returns an array of Q objects (having their points) arranged in a rectangular grid
Returns grid of QBlock objects
00 00 00 00
Q1 1 2 3 4 1 2 3 4 11 11 11 11
Q2 1 2 3 4 1 2 3 4 22 22 22 22 1234567
Q3 1 2 3 4 1 2 3 4 33 33 33 33 1234567
44 44 44 44
, 55 55 55 55 , 1234567 and many more possibilities!
Q7 1 2 3 4 1 2 3 4 66 66 66 66 1234567
Q8 1 2 3 4 1 2 3 4 77 77 77 77
Q9 1 2 3 4 1 2 3 4 88 88 88 88
99 99 99 99
TODO: Update this part, add more examples like-
Q1 1 2 3 4
Q2 1 2 3 4
Q3 1 2 3 4
Q4 1 2 3 4
Q5 1 2 3 4
MCQ type (orient='H')-
[
[(q1,q2,q3),(q4,q5,q6)]
[(q7,q8,q9),(q10,q11,q12)]
]
INT type (orient='V')-
[
[(q1d1,q1d2),(q2d1,q2d2),(q3d1,q3d2),(q4d1,q4d2)]
]
ROLL type-
[
[(roll1,roll2,roll3,...,roll10)]
]
"""
gridData = np.array(qNos)
# print(gridData.shape, gridData)
if(0 and len(gridData.shape)!=3 or gridData.size==0): # product of shape is zero
print("Error(genGrid): Invalid qNos array given:", gridData.shape, gridData)
exit(4)
return []
# ^4ENDUSER should also validate no overlap of rect points somehow?!
"""
orient = 'H'
numVals = 4
[
[["q1", "q2", "q3", "q4"], ["q5", "q6", "q7", "q8"]],
[["q9", "q10", "q11", "q12"], ["q13", "q14", "q15", "q16"]]
]
q1 q9
q2 q10
q3 q11
q4 q12
q5 q13
q6 q14
q7 q15
q8 q16
"""
orig = np.array(orig)
numQsMax = max([max([len(qb) for qb in row]) for row in gridData])
numDims = [numQsMax, len(vals)]
QBlocks=[]
# **Simple is powerful**
# H and V are named with respect to orient == 'H', reverse their meaning when orient = 'V'
H, V = (0,1) if(orient=='H') else (1,0)
# print(orig, numDims, gridData.shape, gridData)
# orient is also the direction of making QBlocks
# print(key, numDims, orig, gaps, bigGaps, origGap )
qStart = orig.copy()
origGap = [0, 0]
# Usually single row
for row in gridData:
qStart[V] = orig[V]
# Usually multiple qTuples
for qTuple in row:
# Update numDims and origGaps
numDims[0] = len(qTuple)
# bigGaps is indep of orientation
origGap[0] = bigGaps[0] + (numDims[V]-1)*gaps[H]
origGap[1] = bigGaps[1] + (numDims[H]-1)*gaps[V]
# each qTuple will have qNos
QBlockDims = [
# width x height in pixels
gaps[0] * (numDims[V]-1) + bubbleDims[H],
gaps[1] * (numDims[H]-1) + bubbleDims[V]
]
# WATCH FOR BLUNDER(use .copy()) - qStart was getting passed by reference! (others args read-only)
QBlocks.append(genQBlock(bubbleDims, QBlockDims, key, qStart.copy(),qTuple,gaps,vals,qType,orient,col_orient))
# Goes vertically down first
qStart[V] += origGap[V]
qStart[H] += origGap[H]
return QBlocks
# The utility for GUI
def calcGaps(PointsX,PointsY,numsX,numsY):
gapsX = ( abs(PointsX[0]-PointsX[1])/(numsX[0]-1),abs(PointsX[2]-PointsX[3]) )
gapsY = ( abs(PointsY[0]-PointsY[1])/(numsY[0]-1),abs(PointsY[2]-PointsY[3]) )
return (gapsX,gapsY)
def read_template(filename):
with open(filename, "r") as f:
try:
return json.load(f)
except Exception as e:
print("Error: Invalid JSON file '"+filename+"'")
print('\t',e)
exit(5)