test_performance.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python
  2. from os import listdir
  3. from sys import argv, exit
  4. from time import time
  5. from GrayscaleImage import GrayscaleImage
  6. from NormalizedCharacterImage import NormalizedCharacterImage
  7. from Character import Character
  8. from data import IMAGES_FOLDER
  9. from create_classifier import load_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. print 'Loading %d characters...' % count
  17. chars = []
  18. i = 0
  19. br = False
  20. for value in sorted(listdir(IMAGES_FOLDER)):
  21. for image in sorted(listdir(IMAGES_FOLDER + value)):
  22. f = IMAGES_FOLDER + value + '/' + image
  23. image = GrayscaleImage(f)
  24. char = Character(value, [], image)
  25. chars.append(char)
  26. i += 1
  27. if i == count:
  28. br = True
  29. break
  30. if br:
  31. break
  32. # Load classifier (run create_classifier.py first)
  33. classifier = load_classifier(neighbours, blur_scale, verbose=1)
  34. # Measure the time it takes to recognize <count> characters
  35. start = time()
  36. for char in chars:
  37. # Normalize the character image
  38. char.image = NormalizedCharacterImage(image, blur=blur_scale, height=42)
  39. # Create the image's feature vector
  40. char.get_single_cell_feature_vector(neighbours)
  41. # Feed the feature vector to the classifier
  42. classifier.classify(char)
  43. elapsed = time() - start
  44. individual = elapsed / count
  45. print 'Took %fs to classify %d caracters (%fms per character)' \
  46. % (elapsed, count, individual * 1000)