Repository navigation
Expand file tree
/
Copy pathTrainAndTest.py
More file actions
156 lines (103 loc) · 5.45 KB
/
Copy pathTrainAndTest.py
File metadata and controls
156 lines (103 loc) · 5.45 KB
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
# TrainAndTest.py
import cv2
import numpy as np
import operator
import os
MIN_CONTOUR_AREA = 100
RESIZED_IMAGE_WIDTH = 20
RESIZED_IMAGE_HEIGHT = 30
class ContourWithData():
npaContour = None
boundingRect = None
intRectX = 0
intRectY = 0
intRectWidth = 0
intRectHeight = 0
fltArea = 0.0
def calculateRectTopLeftPointAndWidthAndHeight(self):
[intX, intY, intWidth, intHeight] = self.boundingRect
self.intRectX = intX
self.intRectY = intY
self.intRectWidth = intWidth
self.intRectHeight = intHeight
def checkIfContourIsValid(self):
if self.fltArea < MIN_CONTOUR_AREA: return False
return True
def main():
allContoursWithData = []
validContoursWithData = []
try:
npaClassifications = np.loadtxt("classifications1.txt", np.float32)
except:
print ("error, unable to open classifications.txt, exiting program\n")
os.system("pause")
return
# end try
try:
npaFlattenedImages = np.loadtxt("flattened_images1.txt", np.float32)
except:
print ("error, unable to open flattened_images.txt, exiting program\n")
os.system("pause")
return
# end try
npaClassifications = npaClassifications.reshape((npaClassifications.size, 1))
kNearest = cv2.ml.KNearest_create()
kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)
nCropped_img_loc = 'Cropped Images-Text/8.png'
imgTestingNumbers = cv2.imread(nCropped_img_loc)
if imgTestingNumbers is None:
print ("error: image not read from file \n\n")
os.system("pause")
return
# end if
imgGray = cv2.cvtColor(imgTestingNumbers, cv2.COLOR_BGR2GRAY)
imgBlurred = cv2.GaussianBlur(imgGray, (5,5), 0)
imgThresh = cv2.adaptiveThreshold(imgBlurred,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV,
11,
2)
imgThreshCopy = imgThresh.copy()
npaContours, npaHierarchy = cv2.findContours(imgThreshCopy,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
for npaContour in npaContours:
contourWithData = ContourWithData()
contourWithData.npaContour = npaContour
contourWithData.boundingRect = cv2.boundingRect(contourWithData.npaContour)
contourWithData.calculateRectTopLeftPointAndWidthAndHeight()
contourWithData.fltArea = cv2.contourArea(contourWithData.npaContour)
allContoursWithData.append(contourWithData)
# end for
for contourWithData in allContoursWithData:
if contourWithData.checkIfContourIsValid():
validContoursWithData.append(contourWithData)
# end if
# end for
validContoursWithData.sort(key = operator.attrgetter("intRectX"))
strFinalString = ""
for contourWithData in validContoursWithData:
cv2.rectangle(imgTestingNumbers,
(contourWithData.intRectX, contourWithData.intRectY),
(contourWithData.intRectX + contourWithData.intRectWidth, contourWithData.intRectY + contourWithData.intRectHeight),
(0, 255, 0),
2)
imgROI = imgThresh[contourWithData.intRectY : contourWithData.intRectY + contourWithData.intRectHeight,
contourWithData.intRectX : contourWithData.intRectX + contourWithData.intRectWidth]
imgROIResized = cv2.resize(imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))
npaROIResized = imgROIResized.reshape((1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))
npaROIResized = np.float32(npaROIResized)
retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 5)
strCurrentChar = str(chr(int(npaResults[0][0])))
strFinalString = strFinalString + strCurrentChar
# end for
print("\nCharacter recognition using KNN Algorithm")
print ("\n" + strFinalString[2:] + "\n")
cv2.imshow("Char recognition", imgTestingNumbers)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
if __name__ == "__main__":
main()
# end if