-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestMultipleNumbers.py
62 lines (55 loc) · 1.78 KB
/
testMultipleNumbers.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
import convert
import Image
import numpy
import time
import textwrap
import os
import pickle
import ExtractCharacters
#Reads the image located ./data/number/test.png
#Extracts the individual digits from the image
#Uses the neural net to attempt to categorize the handwritten digits
def Indicator(n):
if n > .5: return 1
else : return 0
def norm(n):
return (n-237.1)/100.0
def TestNumber(testVec):
testVec = map(norm,convert.getFeatureVecFromImg(testVec))
net.ClearNodes()
net.SetInputs(testVec)
rawOutputs = net.ComputeOutput()
outputs = map (Indicator,net.ComputeOutput())
print "Confidence Values:"
print ("\n" + fspace).join(textwrap.wrap(fspace+str(rawOutputs).ljust(10)))
print "Greatest Confidence:"
print fspace + str(rawOutputs.index(max(rawOutputs))) + " with " + str(100*max(rawOutputs)) + "% confidence"
print
return rawOutputs.index(max(rawOutputs))
def TestImage(path):
testImage = Image.open(path).convert("L")
imgArr = numpy.asarray(testImage)
j = numpy.array(ExtractCharacters.LabelPixels(imgArr),dtype=numpy.uint8)
imagesWithCenters = ExtractCharacters.PasteCharacters(imgArr,j)
numbers = []
for imageWCenter in imagesWithCenters:
numbers.append((imageWCenter[1],TestNumber(imageWCenter[0])))
numbers.sort()
print "The neural net read this!"
print " ",
s = ""
for k in numbers:
s += str(k[1])
print s
print
netName = "BestNet"
netFile = open(netName+".pkl","rb")
print "The image we test against is located at ./data/number/test.png"
net = pickle.load(netFile)
fspace = " "
while 1:
testFile = raw_input("Press enter to test: ")
#time.sleep(5)
curDir = os.getcwd()
testFile = os.path.join(curDir,"data//number/test.png")
TestImage(testFile)