Skip to content

Commit

Permalink
form data tests added, argument names updated for existing tests (img…
Browse files Browse the repository at this point in the history
… not img_path anymore)
  • Loading branch information
serengil committed Nov 10, 2024
1 parent 4429066 commit b19fce5
Showing 1 changed file with 115 additions and 29 deletions.
144 changes: 115 additions & 29 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def setUp(self):

def test_tp_verify(self):
data = {
"img1_path": "dataset/img1.jpg",
"img2_path": "dataset/img2.jpg",
"img1": "dataset/img1.jpg",
"img2": "dataset/img2.jpg",
}
response = self.app.post("/verify", json=data)
assert response.status_code == 200
Expand All @@ -40,8 +40,8 @@ def test_tp_verify(self):

def test_tn_verify(self):
data = {
"img1_path": "dataset/img1.jpg",
"img2_path": "dataset/img2.jpg",
"img1": "dataset/img1.jpg",
"img2": "dataset/img2.jpg",
}
response = self.app.post("/verify", json=data)
assert response.status_code == 200
Expand Down Expand Up @@ -83,14 +83,11 @@ def test_represent(self):
def test_represent_encoded(self):
image_path = "dataset/img1.jpg"
with open(image_path, "rb") as image_file:
encoded_string = "data:image/jpeg;base64," + \
base64.b64encode(image_file.read()).decode("utf8")
encoded_string = "data:image/jpeg;base64," + base64.b64encode(image_file.read()).decode(
"utf8"
)

data = {
"model_name": "Facenet",
"detector_backend": "mtcnn",
"img": encoded_string
}
data = {"model_name": "Facenet", "detector_backend": "mtcnn", "img": encoded_string}

response = self.app.post("/represent", json=data)
assert response.status_code == 200
Expand All @@ -112,7 +109,7 @@ 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"
"img": "https://github.com/serengil/deepface/blob/master/tests/dataset/couple.jpg?raw=true",
}

response = self.app.post("/represent", json=data)
Expand Down Expand Up @@ -155,16 +152,17 @@ def test_analyze(self):
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")
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
encoded_image,
]

results = []
Expand All @@ -189,25 +187,38 @@ def test_analyze_inputformats(self):
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"]
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",
"img2_path": "dataset/invalid_2.jpg",
"img1": "dataset/invalid_1.jpg",
"img2": "dataset/invalid_2.jpg",
}
response = self.app.post("/verify", json=data)
assert response.status_code == 400
Expand All @@ -227,3 +238,78 @@ def test_invalid_analyze(self):
}
response = self.app.post("/analyze", json=data)
assert response.status_code == 400

def test_analyze_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img_file:
response = self.app.post(
"/analyze",
content_type="multipart/form-data",
data={
"img": (img_file, "test_image.jpg"),
"actions": '["age", "gender"]',
"detector_backend": "mtcnn",
},
)
assert response.status_code == 200
result = response.json
assert isinstance(result, dict)
assert result.get("age") is not True
assert result.get("dominant_gender") is not True
logger.info("✅ analyze api for multipart form data test is done")

def test_verify_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img1_file:
with open("dataset/img2.jpg", "rb") as img2_file:
response = self.app.post(
"/verify",
content_type="multipart/form-data",
data={
"img1": (img1_file, "first_image.jpg"),
"img2": (img2_file, "second_image.jpg"),
"model_name": "Facenet",
"detector_backend": "mtcnn",
"distance_metric": "euclidean",
},
)
assert response.status_code == 200
result = response.json
assert isinstance(result, dict)
assert result.get("verified") is not None
assert result.get("model") == "Facenet"
assert result.get("similarity_metric") is not None
assert result.get("detector_backend") == "mtcnn"
assert result.get("threshold") is not None
assert result.get("facial_areas") is not None

logger.info("✅ verify api for multipart form data test is done")

def test_represent_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img_file:
response = self.app.post(
"/represent",
content_type="multipart/form-data",
data={
"img": (img_file, "first_image.jpg"),
"model_name": "Facenet",
"detector_backend": "mtcnn",
},
)
assert response.status_code == 200
result = response.json
assert isinstance(result, dict)
logger.info("✅ represent api for multipart form data test is done")

def test_represent_for_multipart_form_data_and_filepath(self):
response = self.app.post(
"/represent",
content_type="multipart/form-data",
data={
"img": "dataset/img1.jpg",
"model_name": "Facenet",
"detector_backend": "mtcnn",
},
)
assert response.status_code == 200
result = response.json
assert isinstance(result, dict)
logger.info("✅ represent api for multipart form data and file path test is done")

0 comments on commit b19fce5

Please sign in to comment.