Advertisement
Guest User

Untitled

a guest
May 6th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import sys
  2.  
  3. caffe_root = '/home/user/libs/caffe'
  4.  
  5. # We need to define how many digits our CAPTCHAs can have at maximum.
  6. # For simplicity we only have CAPTCHAs of fixed length 6 in this version!
  7. maxNumberOfDigits = 6
  8.  
  9. model = 'data_iter_25000.caffemodel'
  10.  
  11. import numpy as np
  12. # show whole arrays in outputs
  13. np.set_printoptions(threshold=np.nan)
  14. import matplotlib
  15. # Force matplotlib to not use any Xwindows backend.
  16. matplotlib.use('Agg')
  17. import matplotlib.pyplot as plt
  18. sys.path.insert(0, caffe_root + 'python')
  19. import caffe
  20. import time
  21. import os
  22.  
  23. from math import log
  24. from sklearn import svm, datasets
  25. from sklearn.cross_validation import train_test_split
  26. from sklearn.metrics import confusion_matrix
  27. from random import shuffle
  28.  
  29. import warnings
  30. warnings.filterwarnings("ignore", category=DeprecationWarning)
  31.  
  32. # for copy
  33. import shutil
  34.  
  35.  
  36.  
  37. # This function maps the ascii value of a character to a number.
  38. # 0 -> 0, 1->1, ... 9->9, A->10, B->11, ... Z->35,
  39. # a->37, b->38, ... z->62
  40. # there is a small mistkate! The class 36 is never asigned. But it doesn't matter ;)
  41. def convertCharacterToClass(ascii_value):
  42. if ascii_value > 90:
  43. # a small letter
  44. correctClass = ascii_value-60
  45. elif ascii_value > 57:
  46. # a big letter
  47. correctClass = ascii_value-55
  48. else:
  49. # a digit
  50. correctClass=ascii_value-48
  51. return correctClass
  52.  
  53. # This function is the inverse function of convertCharacterToClass
  54. def convertClassToCharacter(predictedClass):
  55. if predictedClass < 10:
  56. predictedCharacter = chr(predictedClass+48)
  57. #print 'Predicted digit:', predictedCharacter
  58. elif predictedClass <= 36:
  59. predictedCharacter = chr(predictedClass+55)
  60. #print "Predicted big letter", predictedCharacter
  61. else:
  62. predictedCharacter = chr(predictedClass+60)
  63. #print "Predicted small letter", predictedCharacter
  64. return predictedCharacter;
  65.  
  66. network = "network_captchas_with_3_convolutional_layers.prototxt"
  67.  
  68. # Make classifier.
  69. classifier = caffe.Classifier(network,model,mean=None)
  70.  
  71.  
  72. start = time.time()
  73.  
  74. IMAGE_FILE = 'aaavbz.png'
  75. #print(IMAGE_FILE)
  76.  
  77. correctString = os.path.splitext(file)[0]
  78. #convert the string into a list of chars
  79. correctChars = list(correctString)
  80.  
  81. input_image = caffe.io.load_image(IMAGE_FILE, color=False)
  82.  
  83. # convert image to grayscale with 1 channel if it is saved with 3 channels
  84. # We assume that all three channels are identical and thus just take the second channel and ignore the others
  85. if input_image.shape[2]>1:
  86. input_image = input_image[:,:,1]
  87. input_image = np.reshape(input_image, (50,180,1))
  88.  
  89. # print input_image
  90. inputs = [input_image]
  91. print input_image.shape
  92. print inputs
  93.  
  94.  
  95. # Classify.
  96. prediction = classifier.predict(inputs, oversample=False)
  97.  
  98. predictedString = ""
  99. numberOfDigits = 6
  100. classesPerDigit = 63
  101. numberOfCorrectChars = 0
  102.  
  103. for x in xrange(0, numberOfDigits):
  104. predictedChar = prediction[0][63*x:63*(x+1)]
  105. # normalize to a sum of 1
  106. predictedChar = predictedChar * sum(predictedChar) ** -1
  107.  
  108. # first guess
  109. predictedClass = predictedChar.argmax()
  110. probabilityFirst = predictedChar.max()
  111. predictedCharacter = convertClassToCharacter(predictedClass)
  112. predictedString+=predictedCharacter
  113.  
  114. # secondguess
  115. predictedChar[predictedClass]=0
  116. predictedClassSecond = predictedChar.argmax()
  117. probabilitySecond = predictedChar.max()
  118. predictedCharacterSecond = convertClassToCharacter(predictedClassSecond)
  119.  
  120. # unceartainty: 0: absolutley certatin, 1: absoluteley uncertain
  121. uncertainty = uncertainty + probabilitySecond / probabilityFirst
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement