AMMICO/ammico/test/test_faces.py
Inga Ulusoy 894ad0938b
add disclaimer checks and tests (#202)
* add disclaimer checks and tests

* add changes to display

* webcolors library update changes

* disclosure instead of disclaimer

* change interface to disclosure

* update demo notebook

* improve explorer display of disclosure env variable, update docs notebook
2024-06-12 09:49:35 +02:00

116 строки
4.5 KiB
Python

import ammico.faces as fc
import json
import pytest
import os
def test_init_EmotionDetector(monkeypatch):
# standard input
monkeypatch.setattr("builtins.input", lambda _: "yes")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
assert ed.subdict["face"] == "No"
assert ed.subdict["multiple_faces"] == "No"
assert ed.subdict["wears_mask"] == ["No"]
assert ed.subdict["emotion"] == [None]
assert ed.subdict["age"] == [None]
assert ed.emotion_threshold == 50
assert ed.age_threshold == 50
assert ed.gender_threshold == 50
assert ed.race_threshold == 50
assert ed.emotion_categories["angry"] == "Negative"
assert ed.emotion_categories["happy"] == "Positive"
assert ed.emotion_categories["surprise"] == "Neutral"
assert ed.accept_disclosure == "OTHER_VAR"
assert os.environ.get(ed.accept_disclosure) == "True"
assert ed.accepted
monkeypatch.delenv(ed.accept_disclosure, raising=False)
# different thresholds
ed = fc.EmotionDetector(
{},
emotion_threshold=80,
race_threshold=30,
gender_threshold=70,
age_threshold=90,
accept_disclosure="OTHER_VAR",
)
assert ed.emotion_threshold == 80
assert ed.race_threshold == 30
assert ed.gender_threshold == 70
assert ed.age_threshold == 90
monkeypatch.delenv(ed.accept_disclosure, raising=False)
# do not accept disclosure
monkeypatch.setattr("builtins.input", lambda _: "no")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
assert os.environ.get(ed.accept_disclosure) == "False"
assert not ed.accepted
monkeypatch.delenv(ed.accept_disclosure, raising=False)
# now test the exceptions: thresholds
monkeypatch.setattr("builtins.input", lambda _: "yes")
with pytest.raises(ValueError):
fc.EmotionDetector({}, emotion_threshold=150)
with pytest.raises(ValueError):
fc.EmotionDetector({}, emotion_threshold=-50)
with pytest.raises(ValueError):
fc.EmotionDetector({}, race_threshold=150)
with pytest.raises(ValueError):
fc.EmotionDetector({}, race_threshold=-50)
with pytest.raises(ValueError):
fc.EmotionDetector({}, gender_threshold=150)
with pytest.raises(ValueError):
fc.EmotionDetector({}, gender_threshold=-50)
with pytest.raises(ValueError):
fc.EmotionDetector({}, age_threshold=150)
with pytest.raises(ValueError):
fc.EmotionDetector({}, age_threshold=-50)
# test pre-set variables: disclosure
monkeypatch.delattr("builtins.input", raising=False)
monkeypatch.setenv("OTHER_VAR", "something")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
assert not ed.accepted
monkeypatch.setenv("OTHER_VAR", "False")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
assert not ed.accepted
monkeypatch.setenv("OTHER_VAR", "True")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
assert ed.accepted
def test_define_actions(monkeypatch):
monkeypatch.setenv("OTHER_VAR", "True")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
actions = ed._define_actions({"wears_mask": True})
assert actions == ["age", "gender"]
actions = ed._define_actions({"wears_mask": False})
assert actions == ["age", "gender", "race", "emotion"]
monkeypatch.setenv("OTHER_VAR", "False")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
actions = ed._define_actions({"wears_mask": True})
assert actions == []
actions = ed._define_actions({"wears_mask": False})
assert actions == ["emotion"]
def test_ensure_deepface_models(monkeypatch):
monkeypatch.setenv("OTHER_VAR", "True")
ed = fc.EmotionDetector({}, accept_disclosure="OTHER_VAR")
ed._ensure_deepface_models(["age", "gender", "race", "emotion"])
def test_analyse_faces(get_path, monkeypatch):
mydict = {
"filename": get_path + "pexels-pixabay-415829.jpg",
}
monkeypatch.setenv("OTHER_VAR", "True")
mydict.update(
fc.EmotionDetector(mydict, accept_disclosure="OTHER_VAR").analyse_image()
)
with open(get_path + "example_faces.json", "r") as file:
out_dict = json.load(file)
# delete the filename key
mydict.pop("filename", None)
# do not test for age, as this is not a reliable metric
mydict.pop("age", None)
for key in mydict.keys():
assert mydict[key] == out_dict[key]