-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.py
80 lines (58 loc) · 2.6 KB
/
application.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
import sys
from keras.models import load_model
import cv2
from preprocessors import x_cord_contour, makeSquare, resize_to_pixel
import pyfiglet
class findHandwrittenDigits:
def __init__(self, imageFileName):
self.classifier = load_model('mnistHandModel.h5')
self.image = cv2.imread(imageFileName)
self.dispImage = self.image.copy()
self.full_number = []
def findDigits(self):
image = self.image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
# Find Contours
contours, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filtering out smaller contours
contours = [i for i in contours if cv2.contourArea(i)>10]
#Sort out contours left to right by using their x cordinates
contours = sorted(contours, key = x_cord_contour, reverse = False)
# Create empty array to store entire number
# loop over the contours
for c in contours:
# compute the bounding box for the rectangle
(x, y, w, h) = cv2.boundingRect(c)
if w >= 5 and h >= 25:
roi = blurred[y:y + h, x:x + w]
ret, roi = cv2.threshold(roi, 127, 255,cv2.THRESH_BINARY_INV)
roi = makeSquare(roi)
roi = resize_to_pixel(28, roi)
roi = roi / 255.0
roi = roi.reshape(1,28,28,1)
## Get Prediction
res = str(self.classifier.predict_classes(roi, 1, verbose = 0)[0])
self.full_number.append(res)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(image, res, (x , y + 155), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)
self.dispImage = image.copy()
def displayResult(self, displayImg = False):
if displayImg:
cv2.imshow("image", self.dispImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
figText = pyfiglet.figlet_format(str(''.join(self.full_number)))
print ("The number is: " )
print(figText)
if __name__ == "__main__":
try:
filename = sys.argv[1]
except:
print('''Mention the file name while running the app.
Usage: python application.py 'filenamehere' ''')
exit()
finderObject = findHandwrittenDigits(filename)
finderObject.findDigits()
finderObject.displayResult(True)