def test_compare_faces(self):
        img_a1 = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        img_a2 = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama2.jpg'))
        img_a3 = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama3.jpg'))

        img_b1 = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'biden.jpg'))

        face_encoding_a1 = api.face_encodings(img_a1)[0]
        face_encoding_a2 = api.face_encodings(img_a2)[0]
        face_encoding_a3 = api.face_encodings(img_a3)[0]
        face_encoding_b1 = api.face_encodings(img_b1)[0]

        faces_to_compare = [
            face_encoding_a2, face_encoding_a3, face_encoding_b1
        ]

        match_results = api.compare_faces(faces_to_compare, face_encoding_a1)

        self.assertEqual(type(match_results), list)
        self.assertTrue(match_results[0])
        self.assertTrue(match_results[1])
        self.assertFalse(match_results[2])
    def test_partial_face_loc(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img',
                         'obama_partial_face.jpg'))
        detected_faces = api.face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0], (142, 191, 365, 0))

        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img',
                         'obama_partial_face2.jpg'))
        detected_faces = api.face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0], (142, 551, 409, 349))
    def test_face_loc(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        detected_faces = api.face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0], (142, 617, 409, 349))
    def test_cnn_raw_face_loc_32bit_img(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', '32bit.png'))
        detected_faces = api._raw_face_locations(img, model="cnn")

        self.assertEqual(len(detected_faces), 1)
        self.assertAlmostEqual(detected_faces[0].rect.top(), 259, delta=25)
        self.assertAlmostEqual(detected_faces[0].rect.bottom(), 552, delta=25)
    def test_raw_face_loc_32bit_img(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', '32bit.png'))
        detected_faces = api._raw_face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0].top(), 290)
        self.assertEqual(detected_faces[0].bottom(), 558)
    def test_raw_face_loc(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        detected_faces = api._raw_face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0].top(), 142)
        self.assertEqual(detected_faces[0].bottom(), 409)
    def test_raw_face_landmarks(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        self.assertEqual(len(face_landmarks), 1)
        self.assertEqual(face_landmarks[0].num_parts, 68)
        self.assertEqual((example_landmark.x, example_landmark.y), (552, 399))
    def test_cnn_face_loc(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        detected_faces = api.face_locations(img, model="cnn")

        self.assertEqual(len(detected_faces), 1)
        self.assertAlmostEqual(detected_faces[0][0], 144, delta=25)
        self.assertAlmostEqual(detected_faces[0][1], 608, delta=25)
        self.assertAlmostEqual(detected_faces[0][2], 389, delta=25)
        self.assertAlmostEqual(detected_faces[0][3], 363, delta=25)
    def test_batched_face_loc(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        images = [img, img, img]

        batched_detected_faces = api.batch_face_locations(
            img, number_of_times_to_upsample=0)

        for detected_faces in batched_detected_faces:
            self.assertEqual(len(detected_faces), 1)
            self.assertEqual(detected_faces[0], (154, 611, 390, 375))
    def test_raw_face_loc_batched(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
        images = [img, img, img]
        batched_detected_faces = api._raw_face_locations_batched(
            img, number_of_times_to_upsample=0)

        for detected_faces in batched_detected_faces:
            self.assertEqual(len(detected_faces), 1)
            self.assertEqual(detected_faces[0].rect.top(), 154)
            self.assertEqual(detected_faces[0].rect.bottom(), 390)
    def test_compare_faces_empty_lists(self):
        img = api.load_img_file(
            os.path.join(os.path.dirname(__file__), 'test_img', 'biden.jpg'))
        face_encoding = api.face_encodings(img)[0]

        # empty python list
        faces_to_compare = []

        match_results = api.compare_faces(faces_to_compare, face_encoding)
        self.assertEqual(type(match_results), list)
        self.assertListEqual(match_results, [])

        # empty numpy list
        faces_to_compare = np.array([])

        match_results = api.compare_faces(faces_to_compare, face_encoding)
        self.assertEqual(type(match_results), list)
        self.assertListEqual(match_results, [])
 def test_load_img_file_32bit(self):
     img = api.load_img_file(
         os.path.join(os.path.dirname(__file__), 'test_img', '32bit.png'))
     self.assertEqual(img.shape, (1200, 626, 3))
 def test_load_img_file(self):
     img = api.load_img_file(
         os.path.join(os.path.dirname(__file__), 'test_img', 'obama.jpg'))
     self.assertEqual(img.shape, (1137, 910, 3))