Home Assistant Unofficial Reference 2024.12.1
image_processing.py
Go to the documentation of this file.
1 """Component that will help set the Dlib face detect processing."""
2 
3 from __future__ import annotations
4 
5 import io
6 import logging
7 
8 import face_recognition
9 import voluptuous as vol
10 
12  CONF_CONFIDENCE,
13  PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA,
14  ImageProcessingFaceEntity,
15 )
16 from homeassistant.const import ATTR_NAME, CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE
17 from homeassistant.core import HomeAssistant, split_entity_id
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 CONF_FACES = "faces"
25 
26 PLATFORM_SCHEMA = IMAGE_PROCESSING_PLATFORM_SCHEMA.extend(
27  {
28  vol.Required(CONF_FACES): {cv.string: cv.isfile},
29  vol.Optional(CONF_CONFIDENCE, default=0.6): vol.Coerce(float),
30  }
31 )
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  add_entities: AddEntitiesCallback,
38  discovery_info: DiscoveryInfoType | None = None,
39 ) -> None:
40  """Set up the Dlib Face detection platform."""
43  camera[CONF_ENTITY_ID],
44  config[CONF_FACES],
45  camera.get(CONF_NAME),
46  config[CONF_CONFIDENCE],
47  )
48  for camera in config[CONF_SOURCE]
49  )
50 
51 
53  """Dlib Face API entity for identify."""
54 
55  def __init__(self, camera_entity, faces, name, tolerance):
56  """Initialize Dlib face identify entry."""
57 
58  super().__init__()
59 
60  self._camera_camera = camera_entity
61 
62  if name:
63  self._name_name = name
64  else:
65  self._name_name = f"Dlib Face {split_entity_id(camera_entity)[1]}"
66 
67  self._faces_faces = {}
68  for face_name, face_file in faces.items():
69  try:
70  image = face_recognition.load_image_file(face_file)
71  self._faces_faces[face_name] = face_recognition.face_encodings(image)[0]
72  except IndexError as err:
73  _LOGGER.error("Failed to parse %s. Error: %s", face_file, err)
74 
75  self._tolerance_tolerance = tolerance
76 
77  @property
78  def camera_entity(self):
79  """Return camera entity id from process pictures."""
80  return self._camera_camera
81 
82  @property
83  def name(self):
84  """Return the name of the entity."""
85  return self._name_name
86 
87  def process_image(self, image):
88  """Process image."""
89 
90  fak_file = io.BytesIO(image)
91  fak_file.name = "snapshot.jpg"
92  fak_file.seek(0)
93 
94  image = face_recognition.load_image_file(fak_file)
95  unknowns = face_recognition.face_encodings(image)
96 
97  found = []
98  for unknown_face in unknowns:
99  for name, face in self._faces_faces.items():
100  result = face_recognition.compare_faces(
101  [face], unknown_face, tolerance=self._tolerance_tolerance
102  )
103  if result[0]:
104  found.append({ATTR_NAME: name})
105 
106  self.process_facesprocess_faces(found, len(unknowns))
None process_faces(self, list[FaceInformation] faces, int total)
Definition: __init__.py:237
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)