🤖 Recadrer les photos en carré autour du visage

Nextcloud et Android affichent la photo en carré découpé au milieu
d'une photo verticale, ce qui coupe les cheveux. La photo est
désormais recadrée en carré autour du visage, détecté localement par
YuNet (OpenCV, modèle MIT versionné dans le paquet) : les yeux sont
placés à la même hauteur sur toutes les photos. Sans visage détecté,
le carré est aligné en haut et compté dans le journal (-v).

Réglages dans mapping.yaml (fields.photo.crop : eyes_at, side, ou
false). Ils entrent dans l'empreinte des seules fiches avec photo :
celles-ci seront renvoyées une fois au prochain passage.

Les tests utilisent un visage généré par thispersondoesnotexist.com
(tests/data/README.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
zeguiguiandClaude Opus 5.5 committed 2026-11-17 19:13:45 +01:00
1 parent 2ada724160
commit 35f90b8fb1
15 files changed
+405 -21

No files matched your search

+33
View File
@@ -6,6 +6,7 @@ from ldap2nextcloud.config import (
ConfigError,
EmailRule,
PhoneRule,
PhotoCrop,
load_directions,
load_mapping,
load_settings,
@@ -97,9 +98,41 @@ def test_load_mapping_example_file():
assert m.emails == [EmailRule("mail", "work", "example.org")]
assert m.birthday_attr == "usrDateNaissance"
assert m.photo == "jpegPhoto"
assert m.photo_crop == PhotoCrop(eyes_at=0.42, side=5.5)
assert m.title == "usrCadre"
def mapping_with_photo(tmp_path, photo):
path = tmp_path / "mapping.yaml"
path.write_text(f"key: uid\nfields:\n given_name: givenName\n family_name: sn\n{photo}")
return load_mapping(path)
def test_photo_crop_is_on_by_default(tmp_path):
m = mapping_with_photo(tmp_path, " photo: jpegPhoto\n")
assert m.photo == "jpegPhoto" and m.photo_crop == PhotoCrop()
def test_photo_crop_can_be_disabled(tmp_path):
m = mapping_with_photo(tmp_path, " photo: {attr: jpegPhoto, crop: false}\n")
assert m.photo == "jpegPhoto" and m.photo_crop is None
def test_photo_crop_settings_are_read(tmp_path):
m = mapping_with_photo(tmp_path, " photo: {attr: jpegPhoto, crop: {eyes_at: 0.4, side: 6}}\n")
assert m.photo_crop == PhotoCrop(eyes_at=0.4, side=6.0)
def test_no_photo_means_no_crop(tmp_path):
assert mapping_with_photo(tmp_path, "").photo_crop is None
@pytest.mark.parametrize("crop", ["{eyes_at: 1.5}", "{eyes_at: 0}", "{side: 0}", "{side: abc}", "oui"])
def test_invalid_photo_crop_is_error(tmp_path, crop):
with pytest.raises(ConfigError, match="crop"):
mapping_with_photo(tmp_path, f" photo: {{attr: jpegPhoto, crop: {crop}}}\n")
def test_load_mapping_missing_key_is_error(tmp_path):
path = tmp_path / "mapping.example.yaml"
path.write_text("fields:\n given_name: givenName\n")