[image_processing/microsoft_face_identify] face recognition for automation (#5472)
* [image_processing/microsoft_face_verify] face recognition for automation * Add platform for microsoft face identify * add unittest for demo * Add unittest for platform
This commit is contained in:
1 parent
c355def154
commit
b57f5728c5
15 files changed
+1259
-20
No files matched your search
@@ -36,6 +36,7 @@ CONF_SOURCE = 'source'
|
||||
CONF_CONFIDENCE = 'confidence'
|
||||
|
||||
DEFAULT_TIMEOUT = 10
|
||||
DEFAULT_CONFIDENCE = 80
|
||||
|
||||
SOURCE_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
@@ -44,6 +45,8 @@ SOURCE_SCHEMA = vol.Schema({
|
||||
|
||||
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_SOURCE): vol.All(cv.ensure_list, [SOURCE_SCHEMA]),
|
||||
vol.Optional(CONF_CONFIDENCE, default=DEFAULT_CONFIDENCE):
|
||||
vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
||||
})
|
||||
|
||||
SERVICE_SCAN_SCHEMA = vol.Schema({
|
||||
@@ -95,6 +98,11 @@ class ImageProcessingEntity(Entity):
|
||||
"""Return camera entity id from process pictures."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def confidence(self):
|
||||
"""Return minimum confidence for do some things."""
|
||||
return None
|
||||
|
||||
def process_image(self, image):
|
||||
"""Process image."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@@ -8,13 +8,17 @@ https://home-assistant.io/components/demo/
|
||||
from homeassistant.components.image_processing import ImageProcessingEntity
|
||||
from homeassistant.components.image_processing.openalpr_local import (
|
||||
ImageProcessingAlprEntity)
|
||||
from homeassistant.components.image_processing.microsoft_face_identify import (
|
||||
ImageProcessingFaceIdentifyEntity)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the demo image_processing platform."""
|
||||
add_devices([
|
||||
DemoImageProcessing('camera.demo_camera', "Demo"),
|
||||
DemoImageProcessingAlpr('camera.demo_camera', "Demo Alpr")
|
||||
DemoImageProcessingAlpr('camera.demo_camera', "Demo Alpr"),
|
||||
DemoImageProcessingFaceIdentify(
|
||||
'camera.demo_camera', "Demo Face Identify")
|
||||
])
|
||||
|
||||
|
||||
@@ -82,3 +86,39 @@ class DemoImageProcessingAlpr(ImageProcessingAlprEntity):
|
||||
}
|
||||
|
||||
self.process_plates(demo_data, 1)
|
||||
|
||||
|
||||
class DemoImageProcessingFaceIdentify(ImageProcessingFaceIdentifyEntity):
|
||||
"""Demo face identify image processing entity."""
|
||||
|
||||
def __init__(self, camera_entity, name):
|
||||
"""Initialize demo alpr."""
|
||||
super().__init__()
|
||||
|
||||
self._name = name
|
||||
self._camera = camera_entity
|
||||
|
||||
@property
|
||||
def camera_entity(self):
|
||||
"""Return camera entity id from process pictures."""
|
||||
return self._camera
|
||||
|
||||
@property
|
||||
def confidence(self):
|
||||
"""Return minimum confidence for send events."""
|
||||
return 80
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
def process_image(self, image):
|
||||
"""Process image."""
|
||||
demo_data = {
|
||||
'Hans': 98.34,
|
||||
'Helena': 82.53,
|
||||
'Luna': 62.53,
|
||||
}
|
||||
|
||||
self.process_faces(demo_data, 4)
|
||||
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
Component that will help set the microsoft face for verify processing.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/image_processing.microsoft_face_identify/
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import split_entity_id, callback
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.components.microsoft_face import DATA_MICROSOFT_FACE
|
||||
from homeassistant.components.image_processing import (
|
||||
PLATFORM_SCHEMA, ImageProcessingEntity, CONF_CONFIDENCE, CONF_SOURCE,
|
||||
CONF_ENTITY_ID, CONF_NAME, ATTR_ENTITY_ID, ATTR_CONFIDENCE)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
|
||||
DEPENDENCIES = ['microsoft_face']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
EVENT_IDENTIFY_FACE = 'identify_face'
|
||||
|
||||
ATTR_NAME = 'name'
|
||||
ATTR_TOTAL_FACES = 'total_faces'
|
||||
ATTR_KNOWN_FACES = 'known_faces'
|
||||
CONF_GROUP = 'group'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_GROUP): cv.slugify,
|
||||
})
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
"""Set up the microsoft face identify platform."""
|
||||
api = hass.data[DATA_MICROSOFT_FACE]
|
||||
face_group = config[CONF_GROUP]
|
||||
confidence = config[CONF_CONFIDENCE]
|
||||
|
||||
entities = []
|
||||
for camera in config[CONF_SOURCE]:
|
||||
entities.append(MicrosoftFaceIdentifyEntity(
|
||||
camera[CONF_ENTITY_ID], api, face_group, confidence,
|
||||
camera.get(CONF_NAME)
|
||||
))
|
||||
|
||||
yield from async_add_devices(entities)
|
||||
|
||||
|
||||
class ImageProcessingFaceIdentifyEntity(ImageProcessingEntity):
|
||||
"""Base entity class for face identify/verify image processing."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize base face identify/verify entity."""
|
||||
self.known_faces = {} # last scan data
|
||||
self.total_faces = 0 # face count
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the entity."""
|
||||
confidence = 0
|
||||
face_name = STATE_UNKNOWN
|
||||
|
||||
# search high verify face
|
||||
for i_name, i_co in self.known_faces.items():
|
||||
if i_co > confidence:
|
||||
confidence = i_co
|
||||
face_name = i_name
|
||||
return face_name
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return device specific state attributes."""
|
||||
attr = {
|
||||
ATTR_KNOWN_FACES: self.known_faces,
|
||||
ATTR_TOTAL_FACES: self.total_faces,
|
||||
}
|
||||
|
||||
return attr
|
||||
|
||||
def process_faces(self, known, total):
|
||||
"""Send event with detected faces and store data."""
|
||||
run_callback_threadsafe(
|
||||
self.hass.loop, self.async_process_faces, known, total
|
||||
).result()
|
||||
|
||||
@callback
|
||||
def async_process_faces(self, known, total):
|
||||
"""Send event with detected faces and store data.
|
||||
|
||||
known are a dict in follow format:
|
||||
{ 'name': confidence }
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
detect = {name: confidence for name, confidence in known.items()
|
||||
if confidence >= self.confidence}
|
||||
|
||||
# send events
|
||||
for name, confidence in detect.items():
|
||||
self.hass.async_add_job(
|
||||
self.hass.bus.async_fire, EVENT_IDENTIFY_FACE, {
|
||||
ATTR_NAME: name,
|
||||
ATTR_ENTITY_ID: self.entity_id,
|
||||
ATTR_CONFIDENCE: confidence,
|
||||
}
|
||||
)
|
||||
|
||||
# update entity store
|
||||
self.known_faces = detect
|
||||
self.total_faces = total
|
||||
|
||||
|
||||
class MicrosoftFaceIdentifyEntity(ImageProcessingFaceIdentifyEntity):
|
||||
"""Microsoft face api entity for identify."""
|
||||
|
||||
def __init__(self, camera_entity, api, face_group, confidence, name=None):
|
||||
"""Initialize openalpr local api."""
|
||||
super().__init__()
|
||||
|
||||
self._api = api
|
||||
self._camera = camera_entity
|
||||
self._confidence = confidence
|
||||
self._face_group = face_group
|
||||
|
||||
if name:
|
||||
self._name = name
|
||||
else:
|
||||
self._name = "MicrosoftFace {0}".format(
|
||||
split_entity_id(camera_entity)[1])
|
||||
|
||||
@property
|
||||
def confidence(self):
|
||||
"""Return minimum confidence for send events."""
|
||||
return self._confidence
|
||||
|
||||
@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
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_process_image(self, image):
|
||||
"""Process image.
|
||||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
detect = None
|
||||
try:
|
||||
face_data = yield from self._api.call_api(
|
||||
'post', 'detect', image, binary=True)
|
||||
|
||||
face_ids = [data['faceId'] for data in face_data]
|
||||
|
||||
detect = yield from self._api.call_api(
|
||||
'post', 'identify',
|
||||
{'faceIds': face_ids, 'personGroupId': self._face_group})
|
||||
|
||||
except HomeAssistantError as err:
|
||||
_LOGGER.error("Can't process image on microsoft face: %s", err)
|
||||
return
|
||||
|
||||
# parse data
|
||||
knwon_faces = {}
|
||||
total = 0
|
||||
for face in detect:
|
||||
total += 1
|
||||
if len(face['candidates']) == 0:
|
||||
continue
|
||||
|
||||
data = face['candidates'][0]
|
||||
name = ''
|
||||
for s_name, s_id in self._api.store[self._face_group].items():
|
||||
if data['personId'] == s_id:
|
||||
name = s_name
|
||||
break
|
||||
|
||||
knwon_faces[name] = data['confidence'] * 100
|
||||
|
||||
# process data
|
||||
self.async_process_faces(knwon_faces, total)
|
||||
@@ -41,14 +41,11 @@ OPENALPR_REGIONS = [
|
||||
]
|
||||
|
||||
CONF_REGION = 'region'
|
||||
DEFAULT_CONFIDENCE = 80
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_REGION):
|
||||
vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
vol.Optional(CONF_CONFIDENCE, default=DEFAULT_CONFIDENCE):
|
||||
vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -50,14 +50,11 @@ CONF_REGION = 'region'
|
||||
CONF_ALPR_BIN = 'alp_bin'
|
||||
|
||||
DEFAULT_BINARY = 'alpr'
|
||||
DEFAULT_CONFIDENCE = 80
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_REGION):
|
||||
vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
vol.Optional(CONF_ALPR_BIN, default=DEFAULT_BINARY): cv.string,
|
||||
vol.Optional(CONF_CONFIDENCE, default=DEFAULT_CONFIDENCE):
|
||||
vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
||||
})
|
||||
|
||||
|
||||
@@ -84,11 +81,6 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
|
||||
self.plates = {} # last scan data
|
||||
self.vehicles = 0 # vehicles count
|
||||
|
||||
@property
|
||||
def confidence(self):
|
||||
"""Return minimum confidence for send events."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the entity."""
|
||||
|
||||
Reference in new issue
Block a user