Improve type hints in dlib_face_identify (#145423)

This commit is contained in:
epenet 2025-05-22 10:37:44 +02:00 committed by GitHub
parent a7f6a6f22c
commit 3d53bdc6c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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(