text: #-*- coding:utf-8 -*-
  import os
  import numpy as np
  from imp import reload
  from PIL import Image, ImageOps
  
  from keras.layers import Input
  from keras.models import Model
  # import keras.backend as K
  
  from . import keys
  from . import densenet
  
  reload(densenet)
  
  characters = keys.alphabet[:]
  characters = characters[1:] + u'卍'
  nclass = len(characters)
  
  input = Input(shape=(32, None, 1), name='the_input')
  y_pred= densenet.dense_cnn(input, nclass)
  basemodel = Model(inputs=input, outputs=y_pred)
  
  modelPath = os.path.join(os.getcwd(), 'densenet/models/weights_densenet.h5')
  if os.path.exists(modelPath):
      basemodel.load_weights(modelPath)
  
  def decode(pred):
      char_list = [] # 保存解码后的字符
      # 针对第3维选择最大的索引，即每个位置的字符进行5990的softmax，取最大概率对应的下标
      pred_text = pred.argmax(axis=2)[0]
      for i in range(len(pred_text)):
          # 如果预测的不是最后一个字符，最后一个字符是空白符
          # 并且
          # 这个字符与前面一个字符不相等 或者 这个字符与前面第二个字符不相等
          if pred_text[i] != nclass - 1 and ((not (i > 0 and pred_text[i] == pred_text[i - 1])) or (i > 1 and pred_text[i] == pred_text[i - 2])):
              char_list.append(characters[pred_text[i]])
      return u''.join(char_list)
  
  def predict(img):
      width, height = img.size[0], img.size[1]
      scale = height * 1.0 / 32
      width = int(width / scale)
      # 图片大小重新改成32高度，宽度随便的小图片
      img = img.resize([width, 32], Image.ANTIALIAS)
     
      '''
      img_array = np.array(img.convert('1'))
      boundary_array = np.concatenate((img_array[0, :], img_array[:, width - 1], img_array[31, :], img_array[:, 0]), axis=0)
      if np.median(boundary_array) == 0:  # 将黑底白字转换为白底黑字
          img = ImageOps.invert(img)
      '''
      # 按照颜色转换，大于125的
      img = np.array(img).astype(np.float32) / 255.0 - 0.5
      
      X = img.reshape([1, 32, width, 1])
  
      # 这里的输入对颜色属性进行过转义
      y_pred = basemodel.predict(X)
      y_pred = y_pred[:, :, :]
  
      # out = K.get_value(K.ctc_decode(y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1])[0][0])[:, :]
      # out = u''.join([characters[x] for x in out[0]])
  
      # 针对预测结果进行解码
      out = decode(y_pred)
  
      return out
  