def InferForImageArray(self, image_numpy_arr):
     if self._rescale_in_preprocessing:
         image_numpy_arr = ImagePreprocessor.RescaleImageInput(
             image_numpy_arr)
     else:
         image_numpy_arr = ImagePreprocessor.NormalizeImageInput(
             image_numpy_arr)
     inference_inputs = [numpy.expand_dims(image_numpy_arr, axis=0)]
     if self._use_mask_input:
         inference_inputs.append(numpy.ones((1, self._num_rnn_steps)))
     predicted_char_id, predicted_probs = self._inference_fn(
         *inference_inputs)
     chars = self.captcha_model.CHARS
     if predicted_char_id.ndim == 1:
         predicted_chars = chars[predicted_char_id[0]]
         probs_by_chars = {}
         for i in range(predicted_probs.shape[1]):
             probs_by_chars[chars[i]] = predicted_probs[0, i]
     else:
         assert predicted_char_id.ndim == 2, predicted_char_id.shape
         predicted_chars = [0] * predicted_char_id.shape[1]
         probs_by_chars = [{} for _ in range(predicted_char_id.shape[1])]
         for i in range(predicted_char_id.shape[1]):
             predicted_chars[i] = chars[predicted_char_id[0, i]]
             for j in range(predicted_probs.shape[2]):
                 probs_by_chars[i][chars[j]] = predicted_probs[0, i, j]
     return predicted_chars, probs_by_chars
 def Load(cls, file_path, rescale_in_preprocessing=False):
   training_data = numpy.load(file_path)
   image_input = training_data['image_data']
   if rescale_in_preprocessing:
     for row in range(image_input.shape[0]):
       image_input[row, 0, :, :] = ImagePreprocessor.RescaleImageInput(
           image_input[row, 0, :, :])
   else:
     image_input = ImagePreprocessor.NormalizeImageInput(image_input)
   ret = (image_input, training_data['chars'])
   del training_data.f
   training_data.close()
   return ret
Exemplo n.º 3
0
def read_and_parse(file_content,cracker):
    im = read_data(file_content)
    array = numpy.asarray(im.convert('L')).copy() 
    image_input = ImagePreprocessor.ProcessImage(array)
    #print "saving image"
    #im.save("geet.png")
    predicted_chars, char_probabilities = cracker.InferForImageArray(image_input)
    return "".join(x for x in predicted_chars)
    def GenerateTrainingData(cls,
                             captchas_dir,
                             training_data_dir,
                             max_size=20000,
                             max_captcha_length=8):
        image_shape = _GetShapeOfImagesUnderDir(captchas_dir)
        training_data_shape = tuple(
            [max_size] +
            list(ImagePreprocessor.GetProcessedImageShape(image_shape)))
        training_image_data = numpy.zeros(training_data_shape,
                                          dtype=numpy.float32)
        training_labels = numpy.zeros((max_size, max_captcha_length),
                                      dtype=numpy.int32)

        i = 0
        for captcha_filepath in utils.GetFilePathsUnderDir(captchas_dir):
            try:
                image_data = ImagePreprocessor.GetImageData(captcha_filepath)
            except Exception as e:
                print e, captcha_filepath
                continue

            i += 1
            index = i % max_size
            training_image_data[index] = ImagePreprocessor.ProcessImage(
                image_data)
            captcha_ids = _GetCaptchaIdsFromImageFilename(captcha_filepath)
            training_labels[index, :] = numpy.zeros(max_captcha_length,
                                                    dtype=numpy.int32)
            training_labels[index, :captcha_ids.shape[0]] = captcha_ids

            if i != 0 and (i % 10000) == 0:
                print 'Generated {0} examples.'.format(i)

            if i != 0 and i % max_size == 0:
                file_path = os.path.join(
                    training_data_dir,
                    "training_images_{0}.npy".format(i / max_size))
                try:
                    cls.Save(file_path, training_image_data, training_labels)
                except Exception as e:
                    print e
 def InferFromImagePath(self, image_path):
     image_input = ImagePreprocessor.ProcessImage(
         ImagePreprocessor.GetImageData(image_path))
     print("image_input%%%%", image_input.shape)
     return self.InferForImageArray(image_input.copy())
def _GetShapeOfImagesUnderDir(captchas_dir):
  for captcha_filepath in utils.GetFilePathsUnderDir(captchas_dir):
    image_data = ImagePreprocessor.GetImageData(captcha_filepath)
    return image_data.shape
  return None