# -*- coding: utf-8 -*-
import os
import time
import cv2
from sklearn.ensemble import RandomForestRegressor
import csv_io
import numpy as np
import pickle

test = []
count = 0
image_files = sorted(os.listdir('./images_test_rev1'))
#image_files = sorted(os.listdir('../../data/a2'))

print "Test Images"
for img in image_files:
    tmp = cv2.imread('./images_test_rev1/' + img)
    #tmp = cv2.imread('../../data/a2/' + img)
    tmp = cv2.resize(tmp, (25, 25), interpolation = cv2.INTER_CUBIC)
    nlist = [float(j) for j in list(tmp.ravel())]
    test.append(nlist)
    count += 1     


predicted_probs = [[0 for j in range(count)] for i in range(37)]
for i in range(37):
    rf = pickle.load( open( "./pickle/rf"+str(i)+".p", "rb" ) )
    print "Predict", i
    pred1 = rf.predict(test)
    for j in range(len(pred1)):
        predicted_probs[i][j]=pred1[j]
    
#print predicted_probs
predicted = [[] for i in range(len(predicted_probs[0]))]
for i in range(len(predicted_probs[0])):
    predicted[i] = [x[i] for x in predicted_probs]
#print predicted
for i in range(len(predicted)):
    for j in range(len(predicted[0])):
        predicted[i][j] = "%f" %predicted[i][j]
#predicted_probs = [x[1] for x in predicted_probs]
csv_io.write_delimited_file("rf_benchmark.csv", predicted)