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 
7 import face_recognition
8 
10  PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA,
11  ImageProcessingFaceEntity,
12 )
13 from homeassistant.const import ATTR_LOCATION, CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE
14 from homeassistant.core import HomeAssistant, split_entity_id
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
17 
18 PLATFORM_SCHEMA = IMAGE_PROCESSING_PLATFORM_SCHEMA
19 
20 
22  hass: HomeAssistant,
23  config: ConfigType,
24  add_entities: AddEntitiesCallback,
25  discovery_info: DiscoveryInfoType | None = None,
26 ) -> None:
27  """Set up the Dlib Face detection platform."""
29  DlibFaceDetectEntity(camera[CONF_ENTITY_ID], camera.get(CONF_NAME))
30  for camera in config[CONF_SOURCE]
31  )
32 
33 
35  """Dlib Face API entity for identify."""
36 
37  def __init__(self, camera_entity, name=None):
38  """Initialize Dlib face entity."""
39  super().__init__()
40 
41  self._camera_camera = camera_entity
42 
43  if name:
44  self._name_name = name
45  else:
46  self._name_name = f"Dlib Face {split_entity_id(camera_entity)[1]}"
47 
48  @property
49  def camera_entity(self):
50  """Return camera entity id from process pictures."""
51  return self._camera_camera
52 
53  @property
54  def name(self):
55  """Return the name of the entity."""
56  return self._name_name
57 
58  def process_image(self, image):
59  """Process image."""
60 
61  fak_file = io.BytesIO(image)
62  fak_file.name = "snapshot.jpg"
63  fak_file.seek(0)
64 
65  image = face_recognition.load_image_file(fak_file)
66  face_locations = face_recognition.face_locations(image)
67 
68  face_locations = [{ATTR_LOCATION: location} for location in face_locations]
69 
70  self.process_facesprocess_faces(face_locations, len(face_locations))
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)