import face_recognition
import os
from os import listdir
from os.path import isfile, join
import sys
import time
import cv2
import numpy
import array
import glob
import errno
import cv2
import random
from matplotlib import pyplot as plt
from collections import Counter

def getReferences(dir):
    subdirs=listdir(dir) 
    names=[];
    emb=[]
    for subdir in (subdirs):
        name=subdir
        subdir=join(dir,subdir)
        files=[f for f in listdir(subdir) if isfile(join(subdir,f))]
        for file in (files):
            file=join(subdir,file);
            image = face_recognition.load_image_file(file)
            encodings=face_recognition.face_encodings(image)
            if (encodings):
                names.append(name)
                emb.append(encodings[0])
    return emb,names

def main(args,distance):
    subdirs=listdir(args) 
    features=[];
    names=[];
    M=numpy.array([[1.0, 0.5, 0 ],[ 0.5 , 1.0, 0]],dtype='f')
    for subdir in (subdirs):
        name=subdir
        subdir=join(args,subdir)
        files=[f for f in listdir(subdir) if isfile(join(subdir,f))]
        txtFile = open(subdir + '/' + 'ModifiedCalculs.txt', 'a')
        for file in (files):
            file=join(subdir,file);
            original=cv2.imread(file)
            rgbImage = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)
            for change  in range(0,7):
                if change == 0:
                    img=rgbImage
                elif change == 1:
                    img=cv2.resize(rgbImage,None, fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
                    #cv2.imwrite('modified', img)
                elif change == 2:
                     img = cv2.GaussianBlur(rgbImage,(25,25),0)
                else:
                    x=random.choice(range(15))/40 
                    y=random.choice(range(15))/40
                    M[0][1]=x
                    M[1][0]=y
                    dsize=rgbImage.shape
                    img = cv2.warpAffine(rgbImage,M,(dsize[0],dsize[1]))
                    '''cv2.imshow('modified', img)
                    cv2.waitKey() remove comentation comas to show image'''
                align=face_recognition.face_landmarks(img)
                encodings=face_recognition.face_encodings(img)
                if (len(encodings)):
                    output = encodings[0]
                    txtFile.write("%s\n" % output)
                    print("File", file)
                    print("Original value: ", change, output)        
if __name__=="__main__":
	if len(sys.argv) > 1:
		path = sys.argv[1]
	else:
		path = "./database2/"
	sys.exit(int(main(path,0.55)) or 0)