def test_image(known_names, known_face_encodings):
    cam = cv2.VideoCapture(0)
    ret, image_to_check = cam.read()

    cam.release()
    cv2.destroyAllWindows()

    unknown_image = image_to_check

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= 0.5625)  #0.5625 is tolerance

        if True in result:
            continue
        else:
            # No match
            sys.exit()

    if not unknown_encodings:
        # No faces were found in image
        sys.exit()
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        #if True in result:
        #[print_result(image_to_check, name, distance, show_distance) for is_match, name, distance in zip(result, known_names, distances) if is_match]
        #else:
        #print_result(image_to_check, "unknown_person", None, show_distance)

    if not unknown_encodings:
        # print out fact that no faces were found in image
        print_result(image_to_check, "no_persons_found", None, show_distance)
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    if max(unknown_image.shape) > 1600:

        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)
    faces = []
    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)
        res = []
        if True in result:
            [
                res.append(
                    print_result(image_to_check, name, distance,
                                 show_distance)) for is_match, name, distance
                in zip(result, known_names, distances) if is_match
            ]
        else:
            res.append(
                print_result(image_to_check, "unknown", None, show_distance))
        mat = defaultdict(int)
        mat = Counter(res)
        res = max(mat.items(), key=lambda x: x[1])
        faces.append(res[0])

    if not unknown_encodings:

        print_result(image_to_check, "no_persons_found", None, show_distance)
    return faces