import face_recognition
import pickle
import cv2

def recognizeDriverFace(img):
	print("[INFO] grabbing image")
	print("[INFO] file path = " + img)
	image = cv2.imread(img)


	detectionMethod = "cnn"
	encodingPath = "encodings.pickle"


	# load the known faces and embeddings
	print("[INFO] loading encodings...")
	data = pickle.loads(open(encodingPath, "rb").read())

	# load the input image and convert it from BGR to RGB

	rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

	# detect the (x, y)-coordinates of the bounding boxes corresponding
	# to each face in the input image, then compute the facial embeddings
	# for each face
	print("[INFO] recognizing faces...")
	boxes = face_recognition.face_locations(rgb,
	model=detectionMethod)
	encodings = face_recognition.face_encodings(rgb, boxes)

	# initialize the list of names for each face detected
	names = []

	# loop over the facial embeddings
	for encoding in encodings:
	# attempt to match each face in the input image to our known
	# encodings
		matches = face_recognition.compare_faces(data["encodings"],
			encoding)
		name = "Unknown"
		# check to see if we have found a match
		if True in matches:
			# find the indexes of all matched faces then initialize a
			# dictionary to count the total number of times each face
			# was matched
			matchedIdxs = [i for (i, b) in enumerate(matches) if b]
			counts = {}
			# loop over the matched indexes and maintain a count for
			# each recognized face face
			for i in matchedIdxs:
				name = data["names"][i]
				counts[name] = counts.get(name, 0) + 1
			# determine the recognized face with the largest number of
			# votes (note: in the event of an unlikely tie Python will
			# select first entry in the dictionary)
			name = max(counts, key=counts.get)

		# update the list of names
		names.append(name)


		# loop over the recognized faces
		for ((top, right, bottom, left), name) in zip(boxes, names):
		# draw the predicted face name on the image
			cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
			y = top - 15 if top - 15 > 15 else top + 15
			cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
				0.75, (0, 255, 0), 2)
			# show the output image
   
		
		print("Showing image....")
		cv2.imwrite("output/Detected.jpg", image)
		# cv2.imshow("Image", image)
		# cv2.waitKey(0)

# image = cv2.imread("examples/PodiumAug2019.jpg")
# recognizeFaces(image)  