-
Notifications
You must be signed in to change notification settings - Fork 1
/
grade-v2.py
296 lines (215 loc) · 7.79 KB
/
grade-v2.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
#!/usr/bin/env python
# coding: utf-8
# In[287]:
from PIL import Image
from PIL import ImageFilter
import sys
import random
im = Image.open("test-images/a-3.jpg")
# Check its width, height, and number of color channels
print("Image is %s pixels wide." % im.width)
print("Image is %s pixels high." % im.height)
print("Image mode is %s." % im.mode)
im = im.filter(ImageFilter.GaussianBlur)
im.thumbnail((475,550), Image.ANTIALIAS)
# In[288]:
import math as math
import random
import numpy as np
def pixelValues(coordinateArray):
pvs = [im.getpixel(coordinate) for coordinate in coordinateArray]
return pvs
k = 2
clusterCenters = [random.randint(0,255) for _ in range(k)]
clusterAssignments = {i:[] for i in range(k)}
print("Initial Cluster Centers: ", clusterCenters)
for x in range(50):
clusterAssignments = {i:[] for i in range(k)}
for x in range(im.width):
for y in range(im.height):
p = im.getpixel((x,y))
closestCenterIndex,closestCenterValue = -1,999
for cluster_index, cluster_value in enumerate(clusterCenters):
distance = abs(p-cluster_value)
if distance < closestCenterValue:
closestCenterValue = distance
closestCenterIndex = cluster_index
clusterAssignments[closestCenterIndex].append((x,y))
clustersChanged = False
for cluster in clusterAssignments:
pvs = pixelValues(clusterAssignments[cluster])
newCenter = int(np.mean(pvs))
if newCenter != clusterCenters[cluster]:
clustersChanged=True
clusterCenters[cluster] = newCenter
print("New Cluster Centers: ", clusterCenters)
if clustersChanged is False:
break
# In[289]:
blacks = clusterAssignments[np.argmin(clusterCenters)]
whites = clusterAssignments[np.argmax(clusterCenters)]
for pixel in blacks:
im.putpixel((pixel),0)
for pixel in whites:
im.putpixel((pixel),255)
im.save("segmented.jpg")
# In[290]:
#Find all black regions
answersDetected = 0
percentageThreshold = 85
im_highlighted = im.copy()
im_highlighted = im_highlighted.convert("RGB")
square_size = 8
y = im.height//5
x = im.width//9
step_size_y = 1
step_size_x = 1
while y < im.height-10:
while x < im.width-im.width//9:
step_size_x = 1
numBlacks = 0
totalRegion = square_size * square_size
for x_offset in range(square_size):
for y_offset in range(square_size):
numBlacks += int(im.getpixel((x+x_offset,y+y_offset)) ==0)
percentageBlack = numBlacks/totalRegion * 100
if percentageBlack >= percentageThreshold:
step_size_x = 10
step_size_y = 1
answersDetected +=1
for x_offset in range(square_size):
for y_offset in range(square_size):
im_highlighted.putpixel((x+x_offset,y+y_offset), (255,0,0))
x = x+step_size_x
x = im.width//9
y = y+step_size_y
print("At threshold percent", percentageThreshold, ":", answersDetected, "answers were detected")
im_colored_template = im_highlighted.copy()
im_highlighted.save("colored.jpg")
# In[291]:
# find y coordinate of first answer box
answersDetected = 0
percentageThreshold = 30
im_highlighted = im.copy()
im_highlighted = im_highlighted.convert("RGB")
rectangle_width= 300
rectangle_height = 10
y = im.height//5
x = im.width//9
step_size_y = 1
step_size_x = 1
terminate = False
while x < im.width-im.width//9:
while y < im.height-10:
step_size_x = 1
numBlacks = 0
totalRegion = rectangle_width * rectangle_height
for x_offset in range(rectangle_width):
for y_offset in range(rectangle_height):
numBlacks += int(im.getpixel((x+x_offset,y+y_offset)) ==0)
percentageBlack = numBlacks/totalRegion * 100
if percentageBlack >= percentageThreshold:
step_size_x = 10
step_size_y = 1
answersDetected +=1
terminate = True
break
y = y+step_size_y
if terminate is True:
break
y = im.height//5
x = x+step_size_x
print("At threshold percent", percentageThreshold, ":", answersDetected, "answers were detected")
# find x coordinate of first answer box
percentageThreshold = 30
im_highlighted = im.copy()
im_highlighted = im_highlighted.convert("RGB")
rectangle_width= 10
rectangle_height = 3
x = im.width//9
newY = y
y= newY
step_size_y = 1
step_size_x = 1
terminate = False
while y < im.height-10:
while x < im.width-im.width//9:
step_size_x = 1
numBlacks = 0
totalRegion = rectangle_width * rectangle_height
for x_offset in range(rectangle_width):
for y_offset in range(rectangle_height):
numBlacks += int(im.getpixel((x+x_offset,y+y_offset)) ==0)
percentageBlack = numBlacks/totalRegion * 100
if percentageBlack >= percentageThreshold:
step_size_x = 10
step_size_y = 1
answersDetected +=1
for x_offset in range(rectangle_width):
for y_offset in range(rectangle_height):
im_highlighted.putpixel((x+x_offset,y+y_offset), (255,0,0))
terminate = True
break
x = x+step_size_x
if terminate is True:
break
y = y +step_size_y
im_highlighted.putpixel((x+5,y),(0,0,255))
im_highlighted.putpixel((x+5,y-1),(0,0,255))
im_highlighted.putpixel((x+5,y-2),(0,0,255))
im_highlighted.putpixel((x+5,y+1),(0,0,255))
im_highlighted.putpixel((x+5,y+2),(0,0,255))
im_highlighted.putpixel((x+4,y),(0,0,255))
im_highlighted.putpixel((x+4,y-1),(0,0,255))
im_highlighted.putpixel((x+4,y-2),(0,0,255))
im_highlighted.putpixel((x+4,y+1),(0,0,255))
im_highlighted.putpixel((x+4,y+2),(0,0,255))
print("Corner Coordinate:",(x,y))
START_COORDINATE = (x+5,y+5)
im_highlighted.save("corner.jpg")
# In[299]:
x = START_COORDINATE[0]
y = START_COORDINATE[1]
im_colored = im_colored_template.copy()
#starting at center of first box (start_coordinate), jump right RIGHT_JUMP pixels at a time, reading whether
#there is a red pixel present, then reset x to start coordinate and jump down DOWN_JUMP pixels
RIGHT_JUMP = 15
DOWN_JUMP = 12
question_number = 1
answerDictionary = {i:[] for i in range(1,86)}
allWhite = True
while True:
y_offset = DOWN_JUMP * (question_number - 1)
allWhite = True
for x_offset,symbol in zip([RIGHT_JUMP * i for i in range(5)], ["A","B","C","D","E"]):
p = im_colored.getpixel((x+x_offset, y+y_offset))
for i in range(-3,1):
im_colored.putpixel((x+x_offset, y+y_offset-i), (0,255,0))
if p == (255,0,0):
answerDictionary[question_number].append(symbol)
allWhite = False
if p == (0,0,0):
allWhite = False
margin_x = START_COORDINATE[0] - int(RIGHT_JUMP * 1.5) if question_number <10 else START_COORDINATE[0] - int(RIGHT_JUMP * 1.75)
numBlacks = 0
rectangle_height = 10
rectangle_width = 10
totalRegion = rectangle_width * rectangle_height
percentageThreshold = 10
for x_kernel in range(rectangle_width):
for y_kernel in range(rectangle_height):
numBlacks += int(im_colored.getpixel((margin_x+x_kernel,y+y_offset-5+y_kernel)) ==0)
im_colored.putpixel((margin_x+x_kernel,y+y_offset-5+y_kernel), (0,255,0))
percentageBlack = numBlacks/totalRegion * 100
if percentageBlack >= percentageThreshold:
answerDictionary[question_number].append('x')
if not allWhite:
question_number+=1
else:
break
im_colored.save("colored2.jpg")
print("Answers Detected Column 1:", answerDictionary)
#find next column,update X part of START_COORDINATE, then repeat above procedure
x = START_COORDINATE[0] + (RIGHT_JUMP *6)
#find next column, update X part of START_COORDINATE, then repeat above procedure
# In[292]: