import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.models import Sequential, load_model
from keras.models import Model

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

img_width, img_height = 64, 64
model_path = './models/model.h5'
model_weights_path = './models/weights.2000.h5'
model = load_model(model_path)
model.load_weights(model_weights_path)

middle_model = Model(inputs=model.input, outputs=model.get_layer('dense_2').output)

def predict(file):
    x = load_img(file, target_size=(img_width,img_height))
    x = img_to_array(x)
    x = np.expand_dims(x, axis=0)
    array = middle_model.predict(x)
    result = array[0]
    return result

Tmax=np.loadtxt('./result/imagenum.txt')
Jr = np.zeros([int(Tmax),3])

DirTmp=u'./result'
for i in range(Tmax):
    result = predict(DirTmp + '/image' + str(i+1) + '.jpg')
    Jr[i,0]=np.asarray(result[0])
    Jr[i,1]=np.asarray(result[1])
    Jr[i,2]=np.asarray(result[2])
np.savetxt(u'./result/Features.txt', Jr, delimiter=',')


