# USAGE
# python train_model.py --embeddings output/embeddings.pickle \
#	--recognizer output/recognizer.pickle --le output/le.pickle

# import the necessary packages
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import pickle
from .extract_embeddings import extract_embeddings
import pathlib

def train_model() :
	# construct the argument parser and parse the arguments
	path = str(pathlib.Path(__file__).parent.absolute())
	args = {"embeddings" : path + r"\output\embeddings.pickle" ,"recognizer" : path + r"\output\recognizer.pickle" ,"le" : path + r"\output\le.pickle"}



	# load the face embeddings
	print("[INFO] loading face embeddings...")
	data = pickle.loads(open(args["embeddings"], "rb").read())

	# encode the labels
	print("[INFO] encoding labels...")
	le = LabelEncoder()
	labels = le.fit_transform(data["names"])

	# train the model used to accept the 128-d embeddings of the face and
	# then produce the actual face recognition
	print("[INFO] training model...")
	recognizer = SVC(C=1.0, kernel="linear", probability=True)

	recognizer.fit(data["embeddings"], labels)

	# write the actual face recognition model to disk
	f = open(args["recognizer"], "wb")
	f.write(pickle.dumps(recognizer))
	f.close()

	# write the label encoder to disk
	f = open(args["le"], "wb")
	f.write(pickle.dumps(le))
	f.close()

if __name__ == "__main__":
	extract_embeddings()
	train_model()