#-*- 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 = []
    all_list = []
    rate = []
    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])):
            # rate.append()
            r = pred[0][i][pred_text[i]]
            if r < 0.96:
                ra = np.argsort(pred[0][i])[::-1][:int(1/r)+1]
                la = []
                for v in ra:
                    la.append({'c':characters[v], 'r': float(int(1e4*pred[0][i][v])/1.e4)})
                rate.append(la)
            elif (len(rate) > 0) and (type(rate[-1]) == list):
                    rate.append(len(char_list))
                # rate.append([{'c':characters[pred_text[i]], 'r': float(int(1e4*r)/1.e4)}])
            char_list.append(characters[pred_text[i]])
            all_list.append(characters[pred_text[i]])
        elif pred_text[i] != nclass - 1:
            all_list.append('.')
        else:
            all_list.append('_')
    rate.insert(0, u''.join(all_list))
    return u''.join(char_list), rate

def predict(img):
    width, height = img.size[0], img.size[1]
    scale = height * 1.0 / 32
    width = int(width / scale)
    
    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)
    '''

    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, rate = decode(y_pred)

    return out, rate, scale
