-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathimagerec.py
More file actions
149 lines (81 loc) · 2.87 KB
/
imagerec.py
File metadata and controls
149 lines (81 loc) · 2.87 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
# if you are on 32 bit OS:
# import Image
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import time
from collections import Counter
def createExamples():
numberArrayExamples = open('numArEx.txt','a')
numbersWeHave = range(0,10)
versionsWeHave = range(1,10)
for eachNum in numbersWeHave:
for eachVer in versionsWeHave:
#print str(eachNum)+'.'+str(eachVer)
imgFilePath = 'images/numbers/'+str(eachNum)+'.'+str(eachVer)+'.png'
ei = Image.open(imgFilePath)
eiar = np.array(ei)
eiar1 = str(eiar.tolist())
lineToWrite = str(eachNum)+'::'+eiar1+'\n'
numberArrayExamples.write(lineToWrite)
def threshold(imageArray):
balanceAr = []
newAr = imageArray
for eachRow in imageArray:
for eachPix in eachRow:
avgNum = reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3])
balanceAr.append(avgNum)
balance = reduce(lambda x, y: x + y, balanceAr)/len(balanceAr)
for eachRow in newAr:
for eachPix in eachRow:
if reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3]) > balance:
eachPix[0] = 255
eachPix[1] = 255
eachPix[2] = 255
eachPix[3] = 255
else:
eachPix[0] = 0
eachPix[1] = 0
eachPix[2] = 0
eachPix[3] = 255
return newAr
def whatNumIsThis(filePath):
matchedAr = []
loadExamps = open('numArEx.txt','r').read()
loadExamps = loadExamps.split('\n')
i = Image.open(filePath)
iar = np.array(i)
iarl = iar.tolist()
inQuestion = str(iarl)
for eachExample in loadExamps:
if len(eachExample) > 3:
splitEx = eachExample.split('::')
currentNum = splitEx[0]
currentAr = splitEx[1]
eachPixEx = currentAr.split('],')
eachPixInQ = inQuestion.split('],')
x=0
while x < len(eachPixEx):
if eachPixEx[x] == eachPixInQ[x]:
matchedAr.append(int(currentNum))
x += 1
print matchedAr
x = Counter(matchedAr)
print x
graphX = []
graphY = []
for eachThing in x:
print eachThing
graphX.append(eachThing)
print x[eachThing]
graphY.append(x[eachThing])
fig = plt.figure()
ax1 = plt.subplot2grid((4,4),(0,0), rowspan=1, colspan=4)
ax2 = plt.subplot2grid((4,4),(1,0), rowspan=3, colspan=4)
ax1.imshow(iar)
ax2.bar(graphX,graphY, align='center')
plt.ylim(400)
xloc = plt.MaxNLocator(12)
ax2.xaxis.set_major_locator(xloc)
plt.show()
whatNumIsThis('images/test.png')