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 detect processing."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 
10  ATTR_AGE,
11  ATTR_GENDER,
12  ATTR_GLASSES,
13  PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA,
14  ImageProcessingFaceEntity,
15 )
16 from homeassistant.components.microsoft_face import DATA_MICROSOFT_FACE
17 from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE
18 from homeassistant.core import HomeAssistant, split_entity_id
19 from homeassistant.exceptions import HomeAssistantError
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 SUPPORTED_ATTRIBUTES = [ATTR_AGE, ATTR_GENDER, ATTR_GLASSES]
27 
28 CONF_ATTRIBUTES = "attributes"
29 DEFAULT_ATTRIBUTES = [ATTR_AGE, ATTR_GENDER]
30 
31 
32 def validate_attributes(list_attributes):
33  """Validate face attributes."""
34  for attr in list_attributes:
35  if attr not in SUPPORTED_ATTRIBUTES:
36  raise vol.Invalid(f"Invalid attribute {attr}")
37  return list_attributes
38 
39 
40 PLATFORM_SCHEMA = IMAGE_PROCESSING_PLATFORM_SCHEMA.extend(
41  {
42  vol.Optional(CONF_ATTRIBUTES, default=DEFAULT_ATTRIBUTES): vol.All(
43  cv.ensure_list, validate_attributes
44  )
45  }
46 )
47 
48 
50  hass: HomeAssistant,
51  config: ConfigType,
52  async_add_entities: AddEntitiesCallback,
53  discovery_info: DiscoveryInfoType | None = None,
54 ) -> None:
55  """Set up the Microsoft Face detection platform."""
56  api = hass.data[DATA_MICROSOFT_FACE]
57  attributes = config[CONF_ATTRIBUTES]
58 
61  camera[CONF_ENTITY_ID], api, attributes, camera.get(CONF_NAME)
62  )
63  for camera in config[CONF_SOURCE]
64  )
65 
66 
68  """Microsoft Face API entity for identify."""
69 
70  def __init__(self, camera_entity, api, attributes, name=None):
71  """Initialize Microsoft Face."""
72  super().__init__()
73 
74  self._api_api = api
75  self._camera_camera = camera_entity
76  self._attributes_attributes = attributes
77 
78  if name:
79  self._name_name = name
80  else:
81  self._name_name = f"MicrosoftFace {split_entity_id(camera_entity)[1]}"
82 
83  @property
84  def camera_entity(self):
85  """Return camera entity id from process pictures."""
86  return self._camera_camera
87 
88  @property
89  def name(self):
90  """Return the name of the entity."""
91  return self._name_name
92 
93  async def async_process_image(self, image):
94  """Process image.
95 
96  This method is a coroutine.
97  """
98  face_data = None
99  try:
100  face_data = await self._api_api.call_api(
101  "post",
102  "detect",
103  image,
104  binary=True,
105  params={"returnFaceAttributes": ",".join(self._attributes_attributes)},
106  )
107 
108  except HomeAssistantError as err:
109  _LOGGER.error("Can't process image on microsoft face: %s", err)
110  return
111 
112  if not face_data:
113  face_data = []
114 
115  faces = []
116  for face in face_data:
117  face_attr = {}
118  for attr in self._attributes_attributes:
119  if attr in face["faceAttributes"]:
120  face_attr[attr] = face["faceAttributes"][attr]
121 
122  if face_attr:
123  faces.append(face_attr)
124 
125  self.async_process_facesasync_process_faces(faces, len(face_data))
None async_process_faces(self, list[FaceInformation] faces, int total)
Definition: __init__.py:242
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)