From 593a3c2539d344e57812e6d8f704d1d86fb3aab4 Mon Sep 17 00:00:00 2001 From: Reza Karbasi Date: Wed, 20 Mar 2024 06:04:32 +0330 Subject: [PATCH 1/3] add: a test added for 'represent' API endpoint to check the url input This commit adds a new test case for the 'represent' API endpoint. The test verifies that the endpoint correctly processes an image URL and returns the expected data structure, including the correct number of face embeddings, face confidence, and facial area for each detected face in the image. --- tests/test_api.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index ab74333..07418d0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -106,6 +106,29 @@ class TestVerifyEndpoint(unittest.TestCase): logger.info("✅ representation api test is done") + def test_represent_url(self): + data = { + "model_name": "Facenet", + "detector_backend": "mtcnn", + "img": "https://github.com/serengil/deepface/blob/master/tests/dataset/couple.jpg?raw=true" + } + + response = self.app.post("/represent", json=data) + assert response.status_code == 200 + result = response.json + logger.debug(result) + assert result.get("results") is not None + assert isinstance(result["results"], list) is True + assert len(result["results"]) == 2 # 2 faces are in the image link + for i in result["results"]: + assert i.get("embedding") is not None + assert isinstance(i.get("embedding"), list) is True + assert len(i.get("embedding")) == 128 + assert i.get("face_confidence") is not None + assert i.get("facial_area") is not None + + logger.info("✅ representation api test is done") + def test_analyze(self): data = { "img": "dataset/img1.jpg", From acea51c81d51740bef8df27e22962ef8d102782b Mon Sep 17 00:00:00 2001 From: Reza Karbasi Date: Wed, 20 Mar 2024 06:19:28 +0330 Subject: [PATCH 2/3] refactor: some logging descriptions revised in tests --- tests/test_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 07418d0..e64b456 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -76,7 +76,7 @@ class TestVerifyEndpoint(unittest.TestCase): assert i.get("face_confidence") is not None assert i.get("facial_area") is not None - logger.info("✅ representation api test is done") + logger.info("✅ representation api test is done (for image path)") def test_represent_encoded(self): image_path = "dataset/img1.jpg" @@ -104,7 +104,7 @@ class TestVerifyEndpoint(unittest.TestCase): assert i.get("face_confidence") is not None assert i.get("facial_area") is not None - logger.info("✅ representation api test is done") + logger.info("✅ representation api test is done (for encoded image)") def test_represent_url(self): data = { @@ -127,7 +127,7 @@ class TestVerifyEndpoint(unittest.TestCase): assert i.get("face_confidence") is not None assert i.get("facial_area") is not None - logger.info("✅ representation api test is done") + logger.info("✅ representation api test is done (for image url)") def test_analyze(self): data = { From 6fac5a95434619db462f9536677cd06f0be4b754 Mon Sep 17 00:00:00 2001 From: Reza Karbasi Date: Wed, 20 Mar 2024 07:33:56 +0330 Subject: [PATCH 3/3] add: test regarding checking the unity of the output with respect to different data resources (for the same picture) added --- tests/test_api.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index e64b456..72e6202 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -150,6 +150,58 @@ class TestVerifyEndpoint(unittest.TestCase): logger.info("✅ analyze api test is done") + def test_analyze_inputformats(self): + image_path = "dataset/couple.jpg" + with open(image_path, "rb") as image_file: + encoded_image = "data:image/jpeg;base64," + \ + base64.b64encode(image_file.read()).decode("utf8") + + image_sources = [ + # image path + image_path, + # image url + f"https://github.com/serengil/deepface/blob/master/tests/{image_path}?raw=true", + # encoded image + encoded_image + ] + + results = [] + for img in image_sources: + data = { + "img": img, + } + response = self.app.post("/analyze", json=data) + + assert response.status_code == 200 + result = response.json + results.append(result) + + assert result.get("results") is not None + assert isinstance(result["results"], list) is True + assert len(result["results"]) > 0 + for i in result["results"]: + assert i.get("age") is not None + assert isinstance(i.get("age"), (int, float)) + assert i.get("dominant_gender") is not None + assert i.get("dominant_gender") in ["Man", "Woman"] + assert i.get("dominant_emotion") is not None + assert i.get("dominant_race") is not None + + assert len(results[0]["results"]) == len(results[1]["results"])\ + and len(results[0]["results"]) == len(results[2]["results"]) + + for i in range(len(results[0]['results'])): + assert results[0]["results"][i]["dominant_emotion"] == results[1]["results"][i]["dominant_emotion"]\ + and results[0]["results"][i]["dominant_emotion"] == results[2]["results"][i]["dominant_emotion"] + + assert results[0]["results"][i]["dominant_gender"] == results[1]["results"][i]["dominant_gender"]\ + and results[0]["results"][i]["dominant_gender"] == results[2]["results"][i]["dominant_gender"] + + assert results[0]["results"][i]["dominant_race"] == results[1]["results"][i]["dominant_race"]\ + and results[0]["results"][i]["dominant_race"] == results[2]["results"][i]["dominant_race"] + + logger.info("✅ different inputs test is done") + def test_invalid_verify(self): data = { "img1_path": "dataset/invalid_1.jpg",