test_performance.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python
  2. from os import listdir
  3. from cPickle import load
  4. from sys import argv, exit
  5. from time import time
  6. from GrayscaleImage import GrayscaleImage
  7. from NormalizedCharacterImage import NormalizedCharacterImage
  8. from Character import Character
  9. from Classifier import Classifier
  10. if len(argv) < 4:
  11. print 'Usage: python %s NEIGHBOURS BLUR_SCALE COUNT' % argv[0]
  12. exit(1)
  13. neighbours = int(argv[1])
  14. blur_scale = float(argv[2])
  15. count = int(argv[3])
  16. suffix = '_%s_%s' % (blur_scale, neighbours)
  17. #chars_file = 'characters%s.dat' % suffix
  18. classifier_file = 'classifier%s.dat' % suffix
  19. #print 'Loading characters...'
  20. #chars = load(open(chars_file, 'r'))[:count]
  21. #count = len(chars)
  22. #
  23. #for char in chars:
  24. # del char.feature
  25. #
  26. #print 'Read %d characters' % count
  27. print 'Loading %d characters...' % count
  28. chars = []
  29. i = 0
  30. br = False
  31. for value in sorted(listdir('../images/LearningSet')):
  32. for image in sorted(listdir('../images/LearningSet/' + value)):
  33. f = '../images/LearningSet/' + value + '/' + image
  34. image = GrayscaleImage(f)
  35. char = Character(value, [], image)
  36. chars.append(char)
  37. i += 1
  38. if i == count:
  39. br = True
  40. break
  41. if br:
  42. break
  43. print 'Loading classifier...'
  44. classifier = Classifier(filename=classifier_file)
  45. classifier.neighbours = neighbours
  46. start = time()
  47. for char in chars:
  48. char.image = NormalizedCharacterImage(image, blur=blur_scale, height=42)
  49. char.get_single_cell_feature_vector(neighbours)
  50. classifier.classify(char)
  51. elapsed = time() - start
  52. individual = elapsed / count
  53. print 'Took %fs to classify %d caracters (%fms per character)' \
  54. % (elapsed, count, individual * 1000)