🤖 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

+24 -3
View File
@@ -6,6 +6,7 @@ import pytest
from PIL import Image
from ldap2nextcloud.carddav import CardDavError
from ldap2nextcloud.photo import PreparedPhoto
from ldap2nextcloud.config import Settings, load_directions, load_mapping
from ldap2nextcloud.model import contact_href
from ldap2nextcloud.planner import GuardError
@@ -115,6 +116,26 @@ def test_birthday_only_for_selected_keys(tmp_path):
assert "BDAY" not in client.cards[contact_href("lvernet")][1]
def test_photo_without_face_is_cropped_to_top_square_and_counted(tmp_path, caplog):
tall = BytesIO()
Image.new("RGB", (400, 600), (10, 20, 30)).save(tall, format="JPEG")
class TallPhotoSource(FakeSource):
def fetch_entries(self, attributes):
entries = super().fetch_entries(attributes)
for entry in entries:
entry["jpegPhoto"] = [tall.getvalue()]
return entries
client = FakeClient()
with caplog.at_level(logging.DEBUG):
run(tmp_path, client, TallPhotoSource())
import vobject
card = vobject.readOne(client.cards[contact_href("omalmare")][1])
assert Image.open(BytesIO(card.photo.value)).size == (400, 400)
assert "Photos sans visage détecté (carré aligné en haut) : 3" in caplog.text
def test_photo_is_resized(tmp_path):
client = FakeClient()
run(tmp_path, client)
@@ -217,13 +238,13 @@ def test_unexpected_photo_failure_does_not_stop_the_run(tmp_path, monkeypatch):
calls = {"n": 0}
def flaky(data, size, quality):
def flaky(data, size, quality, crop):
calls["n"] += 1
if calls["n"] == 1:
raise RuntimeError("bombe")
return b"\xff\xd8ok"
return PreparedPhoto(b"\xff\xd8ok", False)
monkeypatch.setattr(sync, "resize_photo", flaky)
monkeypatch.setattr(sync, "prepare_photo", flaky)
client = FakeClient({"ldap2vcard-old.vcf": ("x", "BEGIN:VCARD")})
summary = run(tmp_path, client, initial=True)
assert summary.errors == 1 and summary.created == 2 and summary.deleted == 1