import tensorflow as tf
import logging
import numpy as np
import cv2
import sys
import os
import random
import pandas as pd
from pprint import pprint
sys.path.append('CNN')

log = logging.getLogger()
log.setLevel(logging.DEBUG)

mean_image = np.load('CNN/mean_image.npy')

def resize(im):
    desired_size = 256

    old_size = im.shape[:2] # old_size is in (height, width) format

    ratio = float(desired_size)/max(old_size)
    new_size = tuple([int(x*ratio) for x in old_size])

    # new_size should be in (width, height) format

    im = cv2.resize(im, (new_size[1], new_size[0]))

    delta_w = desired_size - new_size[1]
    delta_h = desired_size - new_size[0]
    top, bottom = delta_h//2, delta_h-(delta_h//2)
    left, right = delta_w//2, delta_w-(delta_w//2)

    color = [0, 0, 0]
    new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
        value=color)

    return new_im

def get_pictures(location):

    cap = cv2.VideoCapture(location)
    video = np.empty([int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 227, 227, 3])
    fliped = random.random() < .5
    for i in range(video.shape[0]):
        ret, frame = cap.read()
        resized_image = resize(frame)
        x = random.randint(0, 256 - 227)
        resized_image = resized_image[x:x+227, x:x+227]

        resized_image = resized_image - mean_image

        if fliped:
            resized_image = cv2.flip(resized_image, 0)

        video[i] = resized_image

    cap.release()
    cv2.destroyAllWindows()
    assert not np.any(np.isnan(video))

    return video

#X = tf.placeholder(tf.float32, shape=(None, 227, 227, 3), name='inputs')
#y = tf.placeholder(tf.int64, [None], name='labels')


with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:

    with tf.device("/device:GPU:0"):  # "/cpu:0" or "/gpu:0"

        with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE) as scope:

            lr = 1e-4
            relative_root = "CNN/trained_networks/static_v2_lr_test_val-" + str(lr)

            saver = tf.train.import_meta_graph("C:/Riley/DeepLetters/CNN/trained_networks/one opt/static_v2_lr-1e-06/epoch-7/static_v2_lr-1e-06.ckpt.meta")

            #saver = tf.train.Saver()

            saver.restore(sess=sess, save_path="C:/Riley/DeepLetters/CNN/trained_networks/one opt/static_v2_lr-1e-06/epoch-7/static_v2_lr-1e-06.ckpt")

            graph = tf.get_default_graph()
            loss3_SLclassifier_0 = graph.get_tensor_by_name('pool5/7x7_s1:0')
            loss3_SLclassifier_0 = tf.contrib.layers.flatten(loss3_SLclassifier_0)

            X = graph.get_tensor_by_name('inputs:0')
            y = graph.get_tensor_by_name('labels:0')

            #print([n.name for n in tf.get_default_graph().as_graph_def().node])

            csv = pd.DataFrame(columns=['word', 'filepath', 'signer'])

            for signer in ['signer1', 'signer2', 'signer3', 'signer4', 'signer5']:
                for root, dirnames, filenames in os.walk("video_data/" + str(signer)):
                    for filename in filenames:

                        full_path = os.path.join(root, filename)

                        feed_dict = {X: get_pictures(full_path)}

                        word = ''.join(filter(str.isalpha, str(filename[:-4]))).lower()

                        csv = csv.append({'word': word, 'filepath': "video_data/numpy/" + word + '.npy',
                                    'signer': signer}, ignore_index=True)

                        pool_layers = sess.run([loss3_SLclassifier_0], feed_dict=feed_dict)

                        pprint(pool_layers)
                        print(len(pool_layers[0]))
                        #print(csv)

                        #np.save("video_data/numpy/" + word + '.npy', pool_layers)

            csv.to_csv('pool_layers.csv')
