def test_api_configuration(self):
     engine = FaceEngine()
     with app.test_request_context():
         response = self.client.put(api.url_for(Configuration),
                                    query_string={'enable_cnn': True})
         self.assertEqual(response.status_code, 200)
         self.assertEqual(engine.enable_cnn, True)
    def test_api_delete_encodings(self):
        engine = FaceEngine()
        engine.add_face('1', 'face encoding')
        with app.test_request_context():
            response = self.client.delete(api.url_for(FaceEncodingList))
            self.assertEqual(response.status_code, 200)

        self.assertTrue(len(engine.faces) == 0)
    def test_api_delete_encodings_id(self):
        engine = FaceEngine()
        engine.delete_all_encodings()
        engine.add_face('1', 'encoding')

        with app.test_request_context():
            response = self.client.delete(api.url_for(FaceEncoding, encoding_id='1'))
            self.assertTrue(response.status_code, 200)
            self.assertEqual(len([x.id == 1 for x in engine.faces]), 0)
def add_encoding(client, id, encoding_file):
    # np_file = '{0}/test_files/face.dat'.format(BASE_DIR)
    np_file = encoding_file
    with open(np_file, 'rb') as f:
        bytes = f.read()
    data = {
        'id': id,
        'encoding_file': (io.BytesIO(bytes), '1.dat')
    }
    with app.test_request_context():
        response = client.post(api.url_for(FaceEncodingList),
                               buffered=True, content_type='multipart/form-data', data=data)
    def test_api_post_conversion_encoding(self):
        img_file = '{0}/test_files/biden.jpg'.format(BASE_DIR)
        with open(img_file, 'rb') as img:
            bytes = img.read()

        data = {
            # 'face_image': (io.BytesIO(b'content'), '1.jpg')
            'face_image': (io.BytesIO(bytes), '1.jpg')
        }

        with app.test_request_context():
            response = self.client.post(api.url_for(Conversion), buffered=True,
                                        content_type='multipart/form-data', data=data)
            self.assertEqual(response.status_code, 200)
 def test_compare_biden2_with_known_faces(self):
     np_file = self.biden2_encoding_file
     with open(np_file, 'rb') as f:
         bytes = f.read()
     data = {
         'tolerance': 0.6,
         'unknown_dat': (io.BytesIO(bytes), '1.dat')
     }
     with app.test_request_context():
         response = self.client.post(api.url_for(CompareDistances),
                                     buffered=True, content_type='multipart/form-data', data=data)
         self.assertEqual(response.status_code, 200)
         dist_json = json.loads(response.get_data(as_text=True))
         biden_id_exists = str(self.biden_id) in dist_json
         self.assertTrue(biden_id_exists)
    def test_api_post_encodings(self):
        engine = FaceEngine()

        np_file = '{0}/test_files/face.dat'.format(BASE_DIR)
        with open(np_file, 'rb') as f:
            bytes = f.read()
        data = {
            'id': '1',
            'encoding_file': (io.BytesIO(bytes), '1.dat')
        }
        with app.test_request_context():
            response = self.client.post(api.url_for(FaceEncodingList),
                                        buffered=True, content_type='multipart/form-data', data=data)
            self.assertTrue(response.status_code, 200)
            face_exists = any([x.id == '1' for x in engine.faces])
            self.assertTrue(face_exists)
def convert(client, img_file, id):
    # img_file = '{0}/test_files/biden.jpg'.format(BASE_DIR)
    with open(img_file, 'rb') as img:
        bytes = img.read()

    data = {
        # 'face_image': (io.BytesIO(b'content'), '1.jpg')
        'face_image': (io.BytesIO(bytes), '1.jpg')
    }

    with app.test_request_context():
        response = client.post(api.url_for(Conversion), buffered=True,
                               content_type='multipart/form-data', data=data)
        encoding_file = '{0}/{1}.dat'.format(BASE_DIR, str(id))
        with open(encoding_file, 'wb') as f:
            f.write(response.data)

        return encoding_file
    def test_post_comparison_distances(self):
        engine = FaceEngine()
        img_file = '{0}/test_files/biden.jpg'.format(BASE_DIR)

        image = engine.load_image_file(img_file)
        known_face_encoding = engine.face_encodings(image)[0]
        engine.add_face('1', known_face_encoding)

        np_file = '{0}/test_files/face.dat'.format(BASE_DIR)
        with open(np_file, 'rb') as f:
            bytes = f.read()
        data = {
            'tolerance': 0.6,
            'unknown_dat': (io.BytesIO(bytes), '1.dat')
        }
        with app.test_request_context():
            response = self.client.post(api.url_for(CompareDistances),
                                        buffered=True, content_type='multipart/form-data', data=data)
            self.assertTrue(response.status_code, 200)