From 3d53bdc6c5888335f1770e65bf0f48403e84443e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 22 May 2025 10:37:44 +0200 Subject: [PATCH] Improve type hints in dlib_face_identify (#145423) --- .../dlib_face_identify/image_processing.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/dlib_face_identify/image_processing.py b/homeassistant/components/dlib_face_identify/image_processing.py index fee9f8dab3c..c41dad863d4 100644 --- a/homeassistant/components/dlib_face_identify/image_processing.py +++ b/homeassistant/components/dlib_face_identify/image_processing.py @@ -11,6 +11,7 @@ import voluptuous as vol from homeassistant.components.image_processing import ( CONF_CONFIDENCE, PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA, + FaceInformation, ImageProcessingFaceEntity, ) from homeassistant.const import ATTR_NAME, CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE @@ -38,31 +39,40 @@ def setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Dlib Face detection platform.""" + confidence: float = config[CONF_CONFIDENCE] + faces: dict[str, str] = config[CONF_FACES] + source: list[dict[str, str]] = config[CONF_SOURCE] add_entities( DlibFaceIdentifyEntity( camera[CONF_ENTITY_ID], - config[CONF_FACES], + faces, camera.get(CONF_NAME), - config[CONF_CONFIDENCE], + confidence, ) - for camera in config[CONF_SOURCE] + for camera in source ) class DlibFaceIdentifyEntity(ImageProcessingFaceEntity): """Dlib Face API entity for identify.""" - def __init__(self, camera_entity, faces, name, tolerance): + def __init__( + self, + camera_entity: str, + faces: dict[str, str], + name: str | None, + tolerance: float, + ) -> None: """Initialize Dlib face identify entry.""" super().__init__() - self._camera = camera_entity + self._attr_camera_entity = camera_entity if name: - self._name = name + self._attr_name = name else: - self._name = f"Dlib Face {split_entity_id(camera_entity)[1]}" + self._attr_name = f"Dlib Face {split_entity_id(camera_entity)[1]}" self._faces = {} for face_name, face_file in faces.items(): @@ -74,17 +84,7 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity): self._tolerance = tolerance - @property - def camera_entity(self): - """Return camera entity id from process pictures.""" - return self._camera - - @property - def name(self): - """Return the name of the entity.""" - return self._name - - def process_image(self, image): + def process_image(self, image: bytes) -> None: """Process image.""" fak_file = io.BytesIO(image) @@ -94,7 +94,7 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity): image = face_recognition.load_image_file(fak_file) unknowns = face_recognition.face_encodings(image) - found = [] + found: list[FaceInformation] = [] for unknown_face in unknowns: for name, face in self._faces.items(): result = face_recognition.compare_faces(