|
@@ -1,7 +1,8 @@
|
|
|
#!/usr/bin/python
|
|
#!/usr/bin/python
|
|
|
from cPickle import load
|
|
from cPickle import load
|
|
|
from sys import argv, exit
|
|
from sys import argv, exit
|
|
|
-from pylab import imsave
|
|
|
|
|
|
|
+from pylab import imsave, plot, subplot, imshow, show, axis, title
|
|
|
|
|
+from math import sqrt, ceil
|
|
|
import os
|
|
import os
|
|
|
|
|
|
|
|
from Classifier import Classifier
|
|
from Classifier import Classifier
|
|
@@ -25,39 +26,56 @@ print 'Loading test set...'
|
|
|
test_set = load(file(test_set_file, 'r'))
|
|
test_set = load(file(test_set_file, 'r'))
|
|
|
l = len(test_set)
|
|
l = len(test_set)
|
|
|
matches = 0
|
|
matches = 0
|
|
|
-classified = {}
|
|
|
|
|
|
|
+#classified = {}
|
|
|
|
|
+classified = []
|
|
|
|
|
|
|
|
for i, char in enumerate(test_set):
|
|
for i, char in enumerate(test_set):
|
|
|
prediction = classifier.classify(char, char.value)
|
|
prediction = classifier.classify(char, char.value)
|
|
|
|
|
|
|
|
if char.value != prediction:
|
|
if char.value != prediction:
|
|
|
- key = '%s_as_%s' % (char.value, prediction)
|
|
|
|
|
|
|
+ classified.append((char, prediction))
|
|
|
|
|
|
|
|
- if key not in classified:
|
|
|
|
|
- classified[key] = [char]
|
|
|
|
|
- else:
|
|
|
|
|
- classified[key].append(char)
|
|
|
|
|
|
|
+ #key = '%s_as_%s' % (char.value, prediction)
|
|
|
|
|
+
|
|
|
|
|
+ #if key not in classified:
|
|
|
|
|
+ # classified[key] = [char]
|
|
|
|
|
+ #else:
|
|
|
|
|
+ # classified[key].append(char)
|
|
|
|
|
|
|
|
print '"%s" was classified as "%s"' \
|
|
print '"%s" was classified as "%s"' \
|
|
|
% (char.value, prediction)
|
|
% (char.value, prediction)
|
|
|
else:
|
|
else:
|
|
|
matches += 1
|
|
matches += 1
|
|
|
|
|
|
|
|
- print '%d of %d (%d%% done)' % (i + 1, l, int(100 * (i + 1) / l))
|
|
|
|
|
|
|
+ print '%d of %d (%d%% done)' % (i + 1, l, round(100 * (i + 1) / l))
|
|
|
|
|
|
|
|
print '\n%d matches (%d%%), %d fails' % (matches, \
|
|
print '\n%d matches (%d%%), %d fails' % (matches, \
|
|
|
- int(100 * matches / l), \
|
|
|
|
|
|
|
+ round(100 * matches / l), \
|
|
|
len(test_set) - matches)
|
|
len(test_set) - matches)
|
|
|
|
|
|
|
|
-print 'Saving faulty classified characters...'
|
|
|
|
|
-folder = '../images/faulty/'
|
|
|
|
|
|
|
+# Show a grid plot of all faulty classified characters
|
|
|
|
|
+print 'Plotting faulty classified characters...'
|
|
|
|
|
+rows = int(ceil(sqrt(l - matches)))
|
|
|
|
|
+columns = int(ceil((l - matches) / float(rows)))
|
|
|
|
|
|
|
|
-if not os.path.exists(folder):
|
|
|
|
|
- os.mkdir(folder)
|
|
|
|
|
|
|
+for i, pair in enumerate(classified):
|
|
|
|
|
+ char, prediction = pair
|
|
|
|
|
+ subplot(rows, columns, i + 1)
|
|
|
|
|
+ title('%s as %s' % (char.value, prediction))
|
|
|
|
|
+ imshow(char.image.data, cmap='gray')
|
|
|
|
|
+ axis('off')
|
|
|
|
|
|
|
|
-for filename, chars in classified.iteritems():
|
|
|
|
|
- if len(chars) == 1:
|
|
|
|
|
- imsave('%s%s' % (folder, filename), char.image.data, cmap='gray')
|
|
|
|
|
- else:
|
|
|
|
|
- for i, char in enumerate(chars):
|
|
|
|
|
- imsave('%s%s_%d' % (folder, filename, i), char.image.data, cmap='gray')
|
|
|
|
|
|
|
+show()
|
|
|
|
|
+
|
|
|
|
|
+#print 'Saving faulty classified characters...'
|
|
|
|
|
+#folder = '../images/faulty/'
|
|
|
|
|
+#
|
|
|
|
|
+#if not os.path.exists(folder):
|
|
|
|
|
+# os.mkdir(folder)
|
|
|
|
|
+#
|
|
|
|
|
+#for filename, chars in classified.iteritems():
|
|
|
|
|
+# if len(chars) == 1:
|
|
|
|
|
+# imsave('%s%s' % (folder, filename), char.image.data, cmap='gray')
|
|
|
|
|
+# else:
|
|
|
|
|
+# for i, char in enumerate(chars):
|
|
|
|
|
+# imsave('%s%s_%d' % (folder, filename, i), char.image.data, cmap='gray')
|