Home Assistant Unofficial Reference 2024.12.1
image_processing.py
Go to the documentation of this file.
1 """Component that will help set the Microsoft face for verify processing."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 
10  ATTR_CONFIDENCE,
11  CONF_CONFIDENCE,
12  PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA,
13  ImageProcessingFaceEntity,
14 )
15 from homeassistant.components.microsoft_face import DATA_MICROSOFT_FACE
16 from homeassistant.const import ATTR_NAME, CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE
17 from homeassistant.core import HomeAssistant, split_entity_id
18 from homeassistant.exceptions import HomeAssistantError
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 CONF_GROUP = "group"
26 
27 PLATFORM_SCHEMA = IMAGE_PROCESSING_PLATFORM_SCHEMA.extend(
28  {vol.Required(CONF_GROUP): cv.slugify}
29 )
30 
31 
33  hass: HomeAssistant,
34  config: ConfigType,
35  async_add_entities: AddEntitiesCallback,
36  discovery_info: DiscoveryInfoType | None = None,
37 ) -> None:
38  """Set up the Microsoft Face identify platform."""
39  api = hass.data[DATA_MICROSOFT_FACE]
40  face_group = config[CONF_GROUP]
41  confidence = config[CONF_CONFIDENCE]
42 
45  camera[CONF_ENTITY_ID],
46  api,
47  face_group,
48  confidence,
49  camera.get(CONF_NAME),
50  )
51  for camera in config[CONF_SOURCE]
52  )
53 
54 
56  """Representation of the Microsoft Face API entity for identify."""
57 
58  def __init__(self, camera_entity, api, face_group, confidence, name=None):
59  """Initialize the Microsoft Face API."""
60  super().__init__()
61 
62  self._api_api = api
63  self._camera_camera = camera_entity
64  self._confidence_confidence = confidence
65  self._face_group_face_group = face_group
66 
67  if name:
68  self._name_name = name
69  else:
70  self._name_name = f"MicrosoftFace {split_entity_id(camera_entity)[1]}"
71 
72  @property
73  def confidence(self):
74  """Return minimum confidence for send events."""
75  return self._confidence_confidence
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  async def async_process_image(self, image):
88  """Process image.
89 
90  This method is a coroutine.
91  """
92  detect = []
93  try:
94  face_data = await self._api_api.call_api("post", "detect", image, binary=True)
95 
96  if face_data:
97  face_ids = [data["faceId"] for data in face_data]
98  detect = await self._api_api.call_api(
99  "post",
100  "identify",
101  {"faceIds": face_ids, "personGroupId": self._face_group_face_group},
102  )
103 
104  except HomeAssistantError as err:
105  _LOGGER.error("Can't process image on Microsoft face: %s", err)
106  return
107 
108  # Parse data
109  known_faces = []
110  total = 0
111  for face in detect:
112  total += 1
113  if not face["candidates"]:
114  continue
115 
116  data = face["candidates"][0]
117  name = ""
118  for s_name, s_id in self._api_api.store[self._face_group_face_group].items():
119  if data["personId"] == s_id:
120  name = s_name
121  break
122 
123  known_faces.append(
124  {ATTR_NAME: name, ATTR_CONFIDENCE: data["confidence"] * 100}
125  )
126 
127  self.async_process_facesasync_process_faces(known_faces, total)
None async_process_faces(self, list[FaceInformation] faces, int total)
Definition: __init__.py:242
def __init__(self, camera_entity, api, face_group, confidence, name=None)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)