# This is a demo of running face recognition on live video from your webcam, which includes some
# basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.
#
# Face recognition DB under face_db/ dir.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo.

import numpy as np
import face_recognition
import cv2
import click
import os
import re

DISTANCE_THRESHOLD=0.45
NUM_JITTERS=3

def image_files_in_folder(folder):
    return [os.path.join(folder, f) for f in os.listdir(folder) if re.match(r'.*\.(jpg|jpeg|png|gif|bmp)', f, flags=re.I)]

def scan_known_people(known_people_folder):
    known_names = []
    known_face_encodings = []

    for file in image_files_in_folder(known_people_folder):
        basename = os.path.splitext(os.path.basename(file))[0]
        img = face_recognition.load_image_file(file)
        face_locations = face_recognition.face_locations(img, model='hog')
        #face_locations = face_recognition.face_locations(img, model='cnn')
        encodings = face_recognition.face_encodings(img, face_locations, num_jitters=NUM_JITTERS)

        if len(encodings) > 1:
            click.echo("WARNING: More than one face found in {}. Only considering the first face.".format(file))

        if len(encodings) == 0:
            click.echo("WARNING: No faces found in {}. Ignoring file.".format(file))
        else:
            known_names.append(basename)
            known_face_encodings.append(encodings[0])

    return known_names, known_face_encodings

def face_recog():
    # Get a reference to webcam #0 (the default one)
    video_capture = cv2.VideoCapture(0)
    known_face_names, known_face_encodings = scan_known_people ("../face_db")

    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_landmarks_list = []
    face_names = []
    process_this_frame = True

    while True:
        # Grab a single frame of video
        ret, frame = video_capture.read()
        # Resize frame of video to 1/4 size for faster face recognition processing
        #small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(frame, model='hog')
            #face_locations = face_recognition.face_locations(frame, model='cnn')
            face_landmarks_list = face_recognition.face_landmarks(frame)
            face_encodings = face_recognition.face_encodings(frame, face_locations, num_jitters=NUM_JITTERS)

            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                result = list(distances <= DISTANCE_THRESHOLD)
                index = np.argmin(distances)
                name = "Unknown"

                # If a match was found in known_face_encodings, just use the first one.
                if True in result:
                    name = known_face_names[index]
                face_names.append(name)

        process_this_frame = not process_this_frame

        # Display the results
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            #top *= 4
            #right *= 4
            #bottom *= 4
            #left *= 4

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        # Display face landmarks
        for face_landmarks in face_landmarks_list:
            # Print the location of each facial feature in this image
            facial_features = [
                'chin',
                'left_eyebrow',
                'right_eyebrow',
                'nose_bridge',
                'nose_tip',
                'left_eye',
                'right_eye',
                'top_lip',
                'bottom_lip'
            ]

            for facial_feature in facial_features:
                for (x,y) in face_landmarks[facial_feature]:
                    x *= 4
                    y *= 4
                    #cv2.circle(frame, (x,y), 4, (0, 0, 255), -1)

        # Display the resulting image
        cv2.imshow('Video', frame)
        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release handle to the webcam
    video_capture.release()
    cv2.destroyAllWindows()


def main():
    face_recog()


if __name__ == "__main__":
    main()
