-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhwr.py
More file actions
64 lines (58 loc) · 1.67 KB
/
hwr.py
File metadata and controls
64 lines (58 loc) · 1.67 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
"""
Hand writing recognition
"""
from __future__ import print_function
from future.builtins import input
import sys
import knn
from dataset import *
from imgprocess import *
import util
def recog(vec,ts):
return knn.classify(vec, ts.mat, ts.labels, 3)
# test case
def testCase():
testset = testSet()
trainingset = trainingSet()
errors = 0
print(trainingset.n)
for i in range(len(testset.mat)):
n = testset.labels[i]
r = recog(testset.mat[i],trainingset)
print("the recognition came back with: %d, the real answer is: %d" % (r,n))
if r != n:
errors +=1
trainingset.selfLearn(testset.mat[i],n)
print("Errors: %d in total %d" % (errors,testset.n))
# recognise from img
def recogImg(fn):
img = IMGProcess(fn,(32,32))
vec = img.getVec()
ts = trainingSet()
util.printVec(vec,32)
return recog(vec, ts), vec
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Invalid command")
print("Usage: hwr.py test|<filename>")
print("Command test, test with test case")
print("Command <filename>, recognise from image")
exit(0)
if sys.argv[1] == "test":
print("=========")
print("Self test")
print("=========")
testCase()
else:
fn = sys.argv[1]
r, vec = recogImg(fn)
print("\nrecognition result is %d\n" % r)
c = input("Is the result correct? (Y or N): ")
if c.lower() == "n":
try:
cr = int(input("What is the correct answer? "))
except:
pass
else:
ts = trainingSet()
ts.selfLearn(vec,cr)