This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
405 lines (327 loc) · 12.6 KB
/
Main.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
from __future__ import print_function
print("Starting up program...\n")
from glob import glob
import matplotlib.pyplot as plt
from skimage import *
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
'''''''''''''''''''''''''''''''''''''''''''''
' GLOBAL VARIABLES '
'''''''''''''''''''''''''''''''''''''''''''''
# What type of authorization do we want? We want the scope of the spreadsheet we're using
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# The ID of the spreadsheet
SPREADSHEET_ID = '1FLMm3aBwR1MjXJPwC50OiIlZYRXPjGwciewMAKkfxY8'
'''''''''''''''''''''''''''''''''''''''''''''
' IMAGE PROCESSING '
'''''''''''''''''''''''''''''''''''''''''''''
# Code Snippet from Google themselves, thankyou Google
def GetSheetsAPI():
creds = None
try:
# 'token.pickle' is our credentials, load it if it exists
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
# Get the service?
service = build('sheets', 'v4', credentials=creds)
# Get the sheets?
sheet = service.spreadsheets()
return sheet
except:
return None
def ReadRange(Sheet, Range):
global SPREADSHEET_ID
Result = Sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=Range).execute()
return Result.get('values')
def WriteRange(Sheet, Range, Data):
global SPREADSHEET_ID
Body = {'values':Data}
Result = Sheet.values().update(spreadsheetId=SPREADSHEET_ID, range=Range,
valueInputOption='RAW', body=Body).execute()
def GrabImageDimensions(Image):
W = len(Image[0])
H = len(Image)
print("Image Width: %i" % W )
print("Image Height: %i" % H )
print()
return (W, H)
def ContoursToRects(Contours):
Rectangles = []
for Contour in Contours:
MostLeft = 9999
MostRight = 0
MostUp = 9999
MostDown = 0
for Point in Contour:
MostLeft = min(MostLeft, Point[1]) # \
MostRight = max(MostRight, Point[1]) # \ Idk why, but the X & Y values are flipped...
MostUp = min(MostUp, Point[0]) # / So we counter flip it once here and never again
MostDown = max(MostDown, Point[0]) # /
Rect = [MostLeft, MostUp, MostRight-MostLeft, MostDown-MostUp]
Rectangles.append(Rect)
# Log it
print("Grabbed all rectangles...")
for Rect in Rectangles :
print(Rect)
print()
return Rectangles
def GrabTopRowRects(Rectangles, YLine):
TopRowRects = []
NumOfRectsToPop = 0
# All top row rects should be first in the array
# So go through the rects until one rect isnt in top row ( < YLine)
for i, Rect in enumerate(Rectangles):
if Rect[1] < YLine: TopRowRects.append(Rect)
else: break
# Now we sort through the top row rects from min->max x coord
# We can pop the corner rect later, it'll be easier that way
Temp = [0, 0, 0, 0]
for i in range(len(TopRowRects)):
SmallestRectIndex = i
for j in range(i, len(TopRowRects)):
if TopRowRects[j] < TopRowRects[SmallestRectIndex]:
SmallestRectIndex = j
Temp = TopRowRects[i]
TopRowRects[i] = TopRowRects[SmallestRectIndex]
TopRowRects[SmallestRectIndex] = Temp
# Pop the useless corner rect
TopRowRects.pop(0)
# Log it
print("Grabbed all Top Row Rectangles...")
for Rect in TopRowRects:
print(Rect)
print()
return TopRowRects
def GrabLeftColRects(Rectangles, XLine):
# Sadly we don't know which rects are our allignement rects
# So we go through every rect and add it if it's < XLine
LeftColRects = []
for i, Rect in enumerate(Rectangles):
if Rect[0] < XLine:
LeftColRects.append(Rect)
# Although it's not sorted x-wise, being sorted y-wise means it's already sorted
# Pop the useless corner rect
LeftColRects.pop(0)
# Log it
print("Grabbed all Left Collumn Rectangles...")
for Rect in LeftColRects:
print(Rect)
print()
return LeftColRects
def GrabCornerRect(Rectangles, YLine, ImageWidth):
# The corner rect is square unlike the allignment rects
# It's width should be > then half the height
Length_2 = Rectangles[0][3] / 2
for Rect in Rectangles:
if Rect[2] > Length_2 \
and Rect[0] < ImageWidth * 0.3:
print("Corner Rect pulled...")
print("Rect: "+str(Rect)+"\n")
return Rect
return -1
def GrabAllignmentCoordsX(TopRowRects):
XColCoords = []
for Rect in TopRowRects:
XColCoords.append(Rect[0] + Rect[2] / 2)
print("X Collumn Coords: %s\n" % XColCoords)
return XColCoords
def GrabAllignmentCoordsY(LeftColRects):
YRowCoords = []
for Rect in LeftColRects:
YRowCoords.append(Rect[1] + Rect[3] / 2)
print("Y Row Coords: %s\n" % YRowCoords)
return YRowCoords
def GrabBubbleRectsFromRects(Rectangles, XLine, YLine):
BubbleRects = []
for Rect in Rectangles:
if Rect[0] > XLine:
if Rect[1] > YLine:
BubbleRects.append(Rect)
print("Grabbed all the bubble rectangles...")
for B in BubbleRects:
print(B)
print()
return BubbleRects
def IsPointInRects(Point, Rects):
for Rect in Rects:
XDelta = Point[0] - Rect[0]
YDelta = Point[1] - Rect[1]
if XDelta < 0 \
or YDelta < 0 \
or XDelta > Rect[2] \
or YDelta > Rect[3] :
continue
else: return True
return False
def GetBubbles(XColCoords, YRowCoords, BubbleRects):
Bubbles = []
for Row in YRowCoords:
BubbleRow = []
for Col in XColCoords:
Point = [Col, Row]
IsFilled = IsPointInRects(Point, BubbleRects)
BubbleRow.append(IsFilled)
Bubbles.append(BubbleRow)
# Log it
print("Grabbed all bubble values...")
for BubbleRow in Bubbles:
print(BubbleRow)
print()
return Bubbles
'''''''''''''''''''''''''''''''''''''''''''''
' IMAGE DISPLAYING '
'''''''''''''''''''''''''''''''''''''''''''''
def DisplayImage(plt, Image, Title):
plt.imshow(Image)
plt.set_title(Title)
plt.axis('off')
return
def DrawContours(plt, Contours):
for Contour in Contours:
plt.plot(Contour[:,1], Contour[:,0])
def DrawRectangles(plt, Rectangles):
for Rect in Rectangles:
X = [0, 0, 0, 0, 0]
Y = [0, 0, 0, 0, 0]
X[0] = Rect[0]
X[1] = Rect[0]
X[2] = Rect[0]+Rect[2]
X[3] = Rect[0]+Rect[2]
X[4] = Rect[0]
Y[0] = Rect[1]
Y[1] = Rect[1]+Rect[3]
Y[2] = Rect[1]+Rect[3]
Y[3] = Rect[1]
Y[4] = Rect[1]
plt.plot(X, Y)
def DrawXRectangles(plt, Rectangles):
for Rect in Rectangles:
X = [Rect[0], Rect[0]+Rect[2]]
Y1 = [Rect[1], Rect[1]+Rect[3]]
Y2 = [Y1[1], Y1[0]]
plt.plot(X, Y1)
plt.plot(X, Y2)
def DrawVerticalLines(plt, Coords, y2):
for x in Coords:
plt.plot([x, x], [0, y2])
def DrawHorizontalLines(plt, Coords, x2):
for y in Coords:
plt.plot([0, x2], [y, y])
''''''''''''''''''''''''''''''''''''''''
' MAIN THREAD '
'''''''''''''''''''''''''''''''''''''''
def main():
print("Running program...\n")
# Grap all JPGs in the Images folder
Images = glob("Images/*.jpg")
# Grab all PNGs in the Images folder, just in case
Images += glob("Images/*.png")
print("Found all jpg/png images in Images directory")
for Image in Images:
print(Image)
print()
# Declare all our window cols/rows
fig, axes = plt.subplots(nrows=1, ncols=len(Images))
ax = axes.ravel()
BubbleSets = []
for i, ImageName in enumerate(Images):
# Read the image file as grayscale
RawImage = io.imread(ImageName, True)
# Make a rect for the entire image, for general awareness
ImageWidth, ImageHeight = GrabImageDimensions(RawImage)
# Gaussian blur the image, this greys out the text, circles, and lines
BlurImage = filters.gaussian(RawImage, sigma=1.5)
# Apply threshold to turn image from greyscale to black|white, this will full rid the text & circles
ThreshImage = BlurImage < 0.3
# Finds contours... Will turn our image into a list of shapes basically
Contours = measure.find_contours(ThreshImage, 0.8)
# Turns all the random shapes(contours) into simple rects
Rectangles = ContoursToRects(Contours)
# First rect might be the entire page, if so, pop it
if Rectangles[0][2] > ImageWidth / 2 :
print("Popped page rect...\n="+str(Rectangles[0])+"\n")
Rectangles.pop(0)
# To be safe, we won't do any more popping, as we will be saving indexes and stuff
# Since rects are in top-down order, lets make a line to seperate our allignment rects
YLine = Rectangles[0][1] + Rectangles[0][3]
print("YLine: %i\n" % YLine)
# All the rects above our YLine are our allignment rects
TopRowRects = GrabTopRowRects(Rectangles, YLine)
# We'll need the top-left corner rect for the X-Line
CornerRect = GrabCornerRect(Rectangles, YLine, ImageWidth)
# Same as YLine but x-wise, we use the CornerRect we grabbed to calculate this
XLine = CornerRect[0] + CornerRect[2] / 2
print("XLine: %i\n" % XLine)
# All the rects to the left of the XLine are also our allignment rects
LeftColRects = GrabLeftColRects(Rectangles, XLine)
# Grab all the X&y-axis coordinates to allign with bubbles
XColCoords = GrabAllignmentCoordsX(TopRowRects)
YRowCoords = GrabAllignmentCoordsY(LeftColRects)
# All the rects that are past the X&Y lines are our bubbles
BubbleRects = GrabBubbleRectsFromRects(Rectangles, XLine, YLine)
# Where magic happens. Check each intersection to see if there's a filled bubble there
Bubbles = GetBubbles(XColCoords, YRowCoords, BubbleRects)
# Display our results!
DisplayImage (ax[i], ThreshImage, ImageName)
DrawHorizontalLines(ax[i], YRowCoords, ImageWidth)
DrawVerticalLines (ax[i], XColCoords, ImageHeight)
DrawRectangles (ax[i], TopRowRects)
DrawRectangles (ax[i], LeftColRects)
DrawXRectangles (ax[i], BubbleRects)
# Append it
BubbleSets.append(Bubbles)
# -= End for Image in Images =-
# !!! Bubbles are accessed [y][x], not [x][y] !!!
###################################
# ...Convert Bubbles into Data... #
###################################
DataSets = [
["Beenis"]
]
print("Data collected...")
for Row in DataSets:
print(Row)
print()
# Get authorization and store an object to access the spreadsheet
Sheet = GetSheetsAPI()
# If we're offline (or something else fails)
if Sheet is None:
print("!!! Unable to reach the Google Spreadsheet. Outputing the data into a text file instead !!!\n")
file = open("DataOutput.txt", 'a')
for Row in DataSets:
file.write(str(Row[0]))
for i, Val in enumerate(Row, 1) :
file.write("\t"+str(Val))
file.write("\n")
file.close()
print("Successfully appended extracted data to DataOutput.txt\n")
else:
Vals = ReadRange(Sheet, "Sheet1!A1:C4")
print("Values pulled from Spreadsheet")
for Row in Vals:
print(Row)
print()
WriteRange(Sheet, "Sheet1!A6:D8", Data)
print("Updated the Spreadsheet with Data\n")
''' IMAGE DISPLAYING '''
# Show it!
plt.tight_layout(0.0)
plt.show()
input("Press Enter to Exit...")
return
if __name__ == "__main__" : main()