def image_ready():
    """Same as image_raw but no face detection"""
    try:
        image_name = request.args.get('name')
        if not os.path.exists(image_name):
            return "Path doesn't exists"
        img = cv2.imread(image_name)
        frame = transform_frame(img)
        embedding = get_feature(model, frame)

        most_similar_embedding = check_against_embedding_db(yale_faces, embedding)
        add_to_scores(most_similar_embedding)
    except Exception as e:
        print(e)
        return "Exception: Not found"
    return str(most_similar_embedding)
def add_to_db():
    global detector_mtcnn
    global yale_faces
    try:
        image_name = request.args.get('name')
        if not os.path.exists(image_name):
            return "Path doesn't exists"
        img = cv2.imread(image_name)
        img = dlib_hog_face_detection.get_frontal_dlib(img, detector, (112,112))
        frame = transform_frame(img)
        embedding = get_feature(model, frame)
        yale_faces[image_name] = embedding
        yale_faces_data[image_name] = img
    except Exception as e:
        print(e)
        return "Failure"
    return "Success"
def image_raw():
    global yale_faces
    try:
        image_name = request.args.get('name')
        if not os.path.exists(image_name):
            return "Path doesn't exists"
        img = cv2.imread(image_name)
        img = dlib_hog_face_detection.get_frontal_dlib(img, detector, (112,112))
        frame = transform_frame(img)
        embedding = get_feature(model, frame)

        most_similar_embedding = check_against_embedding_db(yale_faces, embedding)
        add_to_scores(most_similar_embedding)
    except Exception as e:
        print(e)
        return "Exception: Not found"
    return str(most_similar_embedding)
def add_to_db_queue():
    global q_send, q_return
    try:
        image_name = request.args.get('name')
        if not os.path.exists(image_name):
            return "Path doesn't exists"
        q_send.put(image_name)
        img = q_return.get()
        if img is None:
            return "Face detection failed"
        frame = transform_frame(img)

        embedding = get_feature(model, frame)
        yale_faces[image_name] = embedding
        yale_faces_data[image_name] = img
    except Exception as e:
        print(e)
        return "Failure"
    return "Success"
def image_raw_mtcnn():
    global q_send, q_return
    global yale_faces
    try:
        image_name = request.args.get('name')
        if not os.path.exists(image_name):
            return "Path doesn't exists"
        q_send.put(image_name)
        frame = q_return.get()
        if frame is None:
            return "Face detection failed"
        frame = transform_frame(frame)
        embedding = get_feature(model, frame)

        most_similar_embedding = check_against_embedding_db(yale_faces, embedding)
        add_to_scores(most_similar_embedding)
    except Exception as e:
        print(e)
        return "Exception: Not found"
    return str(most_similar_embedding)
def index():
    video_name = request.args.get('name')
    cap = cv2.VideoCapture(video_name)
    most_likely = dict()
    if not cap.isOpened():
        print("Error opening video stream or file")
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            frame = transform_frame(frame)
            embedding = get_feature(model, frame)

            most_similar_embedding = check_against_embedding_db(yale_faces, embedding)
            if most_similar_embedding[0] in most_likely:
                most_likely[most_similar_embedding[0]] += 1
            else:
                most_likely[most_similar_embedding[0]] = 1
        else:
            break
    cap.release()
    __import__('pprint').pprint(most_likely)
    return "Sucess"