import tensorflow as tf
import tflearn
from tflearn.layers.conv import conv_2d,max_pool_2d
from tflearn.layers.core import input_data,dropout,fully_connected
from tflearn.layers.estimator import regression
import numpy as np
import cv2
from sklearn.utils import shuffle

#Load Images from Swing
loadedImages = []
for i in range(0, 500):
    path="/Users/aakash/Desktop/Hand/Dataset/SwingImages/swing_"+str(i)+".png"
    #print(path)
    image = cv2.imread(path)
    #print(image)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))
    #gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #loadedImages.append(gray_image.reshape(89, 100, 1))
#Load Images from Yo
for i in range(0, 500):
    p="/Users/aakash/Desktop/Hand/Dataset/YoloImages/YoIoImages"+str(i)+".png"
    image = cv2.imread(p)
    #print(image.shape)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))

'''Load Images from Namaste
for i in range(0, 500):
    image = cv2.imread('Dataset/NamasteImages/namaste_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))'''

#Load Images from atb
for i in range(0, 500):
    image = cv2.imread('Dataset/AtbImages/atb_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))

#Load Images from Peace
for i in range(0, 500):
    image = cv2.imread('Dataset/PeaceImages/peace_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))

#Load Images From Palm
for i in range(0, 500):
    image = cv2.imread('Dataset/PalmImages/palm_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))
    
#Load Images From Fist
for i in range(0, 500):
    image = cv2.imread('Dataset/FistImages/fist_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    loadedImages.append(gray_image.reshape(89, 100, 1))

# Create OutputVector

outputVectors = []
for i in range(0, 500):
    outputVectors.append([1, 0, 0,0,0,0])

for i in range(0, 500):
    outputVectors.append([0, 1, 0,0,0,0])

for i in range(0, 500):
    outputVectors.append([0, 0, 1,0,0,0])

for i in range(0, 500):
    outputVectors.append([0, 0, 0,1,0,0])
for i in range(0, 500):
    outputVectors.append([0, 0, 0,0,1,0])
for i in range(0, 500):
    outputVectors.append([0, 0, 0,0,0,1])

'''for i in range(0, 500):
    outputVectors.append([0, 0, 0,0,0,0,1])'''
testImages = []

#Load Images for swing
for i in range(0, 100):
    image = cv2.imread('Dataset/SwingTest/swing_' + str(i) + '.png')
    #print("test",image)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

#Load Images for Palm
for i in range(0, 100):
    image = cv2.imread('Dataset/PalmTest/palm_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

#Load Images from Yo
for i in range(0, 100):
    image = cv2.imread('/Users/aakash/Desktop/Hand/Dataset/YoloTest/yolo_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

'''Load Images from Namaste
for i in range(3, 100):
    #path='/Users/aakash/desktop/Hand/Dataset/ntest/namaste_1.png'
    #print(path)
    #print(image)
    path='/Dataset/ntest/namaste_'+str(i)+'.png'
    print(path)
    image=cv2.imread(path)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))'''

#Load Images from atb
for i in range(0, 100):
    image = cv2.imread('Dataset/AtbTest/atb_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

#Load Images from Peace
for i in range(0, 100):
    image = cv2.imread('Dataset/PeaceTest/peace_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

#Load Images for Fist
for i in range(0, 100):
    image = cv2.imread('Dataset/FistTest/fist_' + str(i) + '.png')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    testImages.append(gray_image.reshape(89, 100, 1))

testLabels = []

for i in range(0, 100):
    testLabels.append([1, 0, 0,0,0,0])
    
for i in range(0, 100):
    testLabels.append([0, 1, 0,0,0,0])

for i in range(0, 100):
    testLabels.append([0, 0, 1,0,0,0])

for i in range(0, 100):
    testLabels.append([0, 0, 0,1,0,0])

for i in range(0, 100):
    testLabels.append([0, 0, 0,0,1,0])

for i in range(0, 100):
    testLabels.append([0, 0, 0,0,0,1])
#for i in range(0, 100):
  #  testLabels.append([0, 0, 0,0,0,0])
# Define the CNN Model
tf.reset_default_graph()
convnet=input_data(shape=[None,89,100,1],name='input')
convnet=conv_2d(convnet,32,2,activation='relu')
convnet=max_pool_2d(convnet,2)
convnet=conv_2d(convnet,64,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=conv_2d(convnet,128,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=conv_2d(convnet,256,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=conv_2d(convnet,256,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=conv_2d(convnet,128,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=conv_2d(convnet,64,2,activation='relu')
convnet=max_pool_2d(convnet,2)

convnet=fully_connected(convnet,1000,activation='relu')
convnet=dropout(convnet,0.75)

convnet=fully_connected(convnet,6,activation='softmax')

convnet=regression(convnet,optimizer='adam',learning_rate=0.001,loss='categorical_crossentropy',name='regression')

model=tflearn.DNN(convnet,tensorboard_verbose=0)


# Shuffle Training Data
loadedImages, outputVectors = shuffle(loadedImages, outputVectors, random_state=0)

# Train model
model.fit(loadedImages, outputVectors, n_epoch=50,
           validation_set = (testImages, testLabels),
           snapshot_step=100, show_metric=True, run_id='convnet_coursera')

model.save("TrainedModel/GestureRecogModel.tfl")