/
scannable_paper.py
467 lines (329 loc) · 13.8 KB
/
scannable_paper.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import cv2
import numpy as np
import pandas as pd
from keras.preprocessing.image import img_to_array
import calendar;
import time;
from keras.models import load_model
from keras.models import model_from_json
import os
import cv2
import numpy as np
import pandas as pd
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from keras.models import model_from_json
# load the pre-trained Keras model (here we are using a model
# pre-trained on ImageNet and provided by Keras, but you can
# substitute in your own networks just as easily)
def rectify(h):
h = h.reshape((4,2))
hnew = np.zeros((4,2),dtype = np.float32)
add = h.sum(1)
hnew[0] = h[np.argmin(add)]
hnew[2] = h[np.argmax(add)]
diff = np.diff(h,axis = 1)
hnew[1] = h[np.argmin(diff)]
hnew[3] = h[np.argmax(diff)]
return hnew
def outerRectangle(image):
height, width, channels = image.shape
if width > height:
image = cv2.transpose(image)
image = cv2.flip(image,1)
# resize image so it can be processed
image = cv2.resize(image, (1600, 1200))
# creating copy of original image
orig = image.copy()
# convert to grayscale and blur to smooth
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
#blurred = cv2.medianBlur(gray, 5)
edged = cv2.Canny(blurred, 0,50)
orig_edged = edged.copy()
# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
(_,contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# get approximate contour
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4:
target = approx
break
# mapping target points to 800x800 quadrilateral
approx = rectify(target)
(tl, tr, br, bl) = approx
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])
M = cv2.getPerspectiveTransform(approx,pts2)
dst = cv2.warpPerspective(orig,M,(800, 800))
mask = np.ones(orig.shape, np.uint8)
mask = cv2.bitwise_not(mask)
x_offset=y_offset=50
mask[y_offset:y_offset+dst.shape[0], x_offset:x_offset+dst.shape[1]] = dst
return mask
def correctprespective(image):
#result2 = cv2.add(orig,result)
# cv2.imshow('image', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()#
# mapping target points to 800x800 quadrilateral
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
#blurred = cv2.medianBlur(gray, 5)
# apply Canny Edge Detection
edged = cv2.Canny(blurred, 0,50)
# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
(_,contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# get approximate contour
pt = []
largestctr=""
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4:
target = approx
#largestctr = ctr
break
orig = image.copy()
approx = rectify(target)
#cv2.drawContours(orig,[target],-1,(0,255,0),1)
x, y, w, h = cv2.boundingRect(approx)
dst = orig[y:y+h,x:x+w]
return dst
def innerRectangles(dst):
names = []
answers= []
questions = []
gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
# apply Canny Edge Detection
edged = cv2.Canny(blurred, 0, 50)
dst2 = dst.copy()
(_,contours,_) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
targetvec = list()
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4 and cv2.contourArea(approx) >4000: #parameter which needs to be tuned for separate area size
#print("Area:",cv2.contourArea(approx))
targetvec.append(approx)
point_list = []
for c in targetvec:
x1, y1, width1, height1 = cv2.boundingRect (c)
point_list.append([x1,y1,width1,height1])
#filter necessary so that the big outer contour is not detected
point_array = [point for point in point_list if point[0] > 15]
duplicate_array = []
same_pt = []
point_array = sorted(point_array,key=lambda x: (x[1]))
# for i in point_array:
# print ("Point Array :",i)
for i in range(len(point_array)):
for j in range(i+1,len(point_array)):
#nearby contour points to remove
if point_array[i][1]+ 10 > point_array[j][1]:
point_array[j][1] = point_array[i][1]
point_array = sorted(point_array,key=lambda x: (x[1],x[0]))
for i in range(len(point_array)):
for j in range(i+1,len(point_array)):
if point_array[i][0]+ 10 > point_array[j][0] and point_array[i][1]+ 10 > point_array[j][1] and point_array[i][2]+ 10 > point_array[j][2] and point_array[i][3]+ 10 > point_array[j][3] :
duplicate_array.append(j)
# print("final size is : ", len(point_array))
# print("duplicate_array size is : ", len(duplicate_array))
#deleting from reverse based on index to avoid out of index issue
duplicate_array = sorted(list(set(duplicate_array)),reverse=True)
# print("Points detected:",len(point_array),"Duplicate Points to be removed:",len(list(set(duplicate_array))))
# #print(duplicate_array)
# for i in duplicate_array:
# print ("Deleted",i)
for i in duplicate_array:
del point_array[i]
for i in point_array:
x, y, width, height = i[0],i[1],i[2],[3]
for i in range(0,len(point_array)):
x, y, width, height = point_array[i][0],point_array[i][1],point_array[i][2],point_array[i][3]
#if y < 720:
#cropping some padding which contains box lines
roi = dst[y-3:y+height+3, x-5:x+width+5]
# cv2.rectangle(dst,(x,y),(x+width,y+height),(0,255,0),1)
# print(roi.shape)
# print("height - width {}".format(abs(height-width)))
area = height * width
if height+30 >=width:
continue
#print("final area :: ", area)
os.path.join('.')
if i==0 or i==1:
names.append(roi)
elif i>1:
if (area>9000 and area<20000) or area>200000:
continue
elif (area >4500 and area<9000):
answers.append(roi)
elif (area >50000 and area <200000):
questions.append(roi)
print(len(answers))
print(len(questions))
for i in range(len(names)):
if not os.path.isdir('name'):
os.makedirs('name')
cv2.imwrite(os.path.join("name","name" + str(i+1)+".png"), names[i])
for i in range(len(answers)):
if not os.path.isdir('answers'):
os.makedirs('answers')
cv2.imwrite(os.path.join("answers","answers" + str(i+1)+".png"), answers[i])
question_array_names = []
for i in range(len(questions)):
if not os.path.isdir('questions'):
os.makedirs('questions')
file_name = str(int(calendar.timegm(time.gmtime()))) + "_question" + str(i+1)+".png"
cv2.imwrite(os.path.join("questions", file_name), questions[i])
question_array_names.append(file_name);
return len(point_array), answers, question_array_names
def getResponseFromImage(input_image):
success = False
image = cv2.imread("static/" + input_image)
image = outerRectangle(image)
dst = correctprespective(image)
#dst = correctprespective(image)
#qpts_data = pd.read_csv("question_data.csv")
regions_detected, answers, question_array_names = innerRectangles(dst)
print("Detected regions :",regions_detected)
responses = []
q_types = ["ocr","ocr", "ocr", "omr","omr"]
idx_char_omr = { 1 : "A", 2 : "B", 3 : "C", 4: "D"}
if not len(answers) == len(q_types):
print("Not able to detect properly")
return False, answers, question_array_names
return True, answers, question_array_names
def getBlob(im):
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 0
params.maxThreshold = 100
# Filter by Area.
params.filterByArea = True
params.minArea = 100
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.3
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.5
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.1
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
def getKey(item):
return item[1]
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
li = []
for i in range(len(keypoints)):
print(i,"x:",keypoints[i].pt)
li.append(keypoints[i].pt)
keypoint_sorted = sorted(li, key=getKey)
return keypoint_sorted
def getCircles(image):
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT, 1, 20,
param1=45,
param2=22,
minRadius=0,
maxRadius=55)
point_list = []
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
#print(x,y)
point_list.append([x,y])
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 1)
#cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
#sort by x coord because we will get row_count in metadata
point_list = sorted(point_list,key=lambda x: x[0])
return point_list
def evaluateOmrQuestion(image,row_count=2,x_response = ["A","B","C","D"],y_response= ["Vertebrate","Invertebrate"]):
#if __name__ == "__main__":
#load image
#image = cv2.imread("roi5.png")
#get all circles
# print("shape of OMR: ", image.shape)
# point_list = getCircles(image)
# #setting the x-y range based on circles
# x_range = []
# y_range = sorted([point_list[i][1] for i in range(row_count)])
# print(y_range)
# for i in range(0,len(point_list),row_count):
# row_group = point_list[i:i+row_count]
# x_range.append(min([row[0] for row in row_group ]))
# print(x_range)
x_range = [60,110,160,210]
#Detecting blob points
blob_points = getBlob(image)
print ("blob_points ", blob_points)
# #final response list
responses = []
for point in blob_points:
print(point)
found = False
for i in range(len(x_range)):
if int(point[0]) < x_range[i]:
responses.append(i + 1)
#print(responses)
found = True
if found:break
# print("Final responses")
# print(responses)
return responses
# success , answers, question_array_names = getResponseFromImage("test.jpg")
# q_types = ["ocr","ocr", "ocr", "omr","omr"]
# idx_char_omr = { 1 : "A", 2 : "B", 3 : "C", 4: "D"}
# responses=[]
# for i in range(len(answers)):
# q_img = "answers"+str(i+1)+".png"
# if q_types[i] == "omr":
# img = cv2.imread(os.path.join('./answers',q_img))
# detected_omr_ans = evaluateOmrQuestion(img);
# print("detected",detected_omr_ans)
# #responses.append(idx_char_omr[detected_omr_ans[0] ] )
# if q_types[i] =="ocr":
# img = cv2.imread(os.path.join('./answers',q_img))
# responses.append(ocr_prediction(img))