ClassifierTest.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python
  2. from LicensePlate import LicensePlate
  3. from Classifier import Classifier
  4. from cPickle import dump, load
  5. chars = []
  6. for i in range(9):
  7. for j in range(100):
  8. try:
  9. filename = '%04d/00991_%04d%02d.info' % (i, i, j)
  10. print 'loading file "%s"' % filename
  11. plate = LicensePlate(i, j)
  12. if hasattr(plate, 'characters'):
  13. chars.extend(plate.characters)
  14. except:
  15. print 'epic fail'
  16. print 'loaded %d chars' % len(chars)
  17. #dump(chars, file('chars', 'w+'))
  18. #----------------------------------------------------------------
  19. chars = load(file('chars', 'r'))
  20. learned = []
  21. learning_set = []
  22. test_set = []
  23. for char in chars:
  24. if learned.count(char.value) > 80:
  25. test_set.append(char)
  26. else:
  27. learning_set.append(char)
  28. learned.append(char.value)
  29. dump(learning_set, file('learning_set', 'w+'))
  30. dump(test_set, file('test_set', 'w+'))
  31. #----------------------------------------------------------------
  32. learning_set = load(file('learning_set', 'r'))
  33. # Train the classifier with the learning set
  34. classifier = Classifier(c=30)
  35. classifier.train(learning_set)
  36. classifier.save('classifier')
  37. #----------------------------------------------------------------
  38. classifier = Classifier(filename='classifier')
  39. test_set = load(file('test_set', 'r'))
  40. l = len(test_set)
  41. matches = 0
  42. for i, char in enumerate(test_set):
  43. prediction = classifier.classify(char)
  44. if char.value == prediction:
  45. print ':) ------> Successfully recognized "%s"' % char.value,
  46. matches += 1
  47. else:
  48. print ':( Expected character "%s", got "%s"' \
  49. % (char.value, prediction),
  50. print ' -- %d of %d (%d%% done)' % (i + 1, l, int(100 * (i + 1) / l))
  51. print '\n%d matches (%d%%), %d fails' % (matches, \
  52. int(100 * matches / len(test_set)), \
  53. len(test_set) - matches)