This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

@@ -6,29 +6,40 @@ import voluptuous as vol
from homeassistant.core import split_entity_id
from homeassistant.components.image_processing import (
ImageProcessingFaceEntity, PLATFORM_SCHEMA, CONF_SOURCE, CONF_ENTITY_ID,
CONF_NAME, CONF_CONFIDENCE)
ImageProcessingFaceEntity,
PLATFORM_SCHEMA,
CONF_SOURCE,
CONF_ENTITY_ID,
CONF_NAME,
CONF_CONFIDENCE,
)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
ATTR_NAME = 'name'
CONF_FACES = 'faces'
ATTR_NAME = "name"
CONF_FACES = "faces"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_FACES): {cv.string: cv.isfile},
vol.Optional(CONF_CONFIDENCE, default=0.6): vol.Coerce(float),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_FACES): {cv.string: cv.isfile},
vol.Optional(CONF_CONFIDENCE, default=0.6): vol.Coerce(float),
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Dlib Face detection platform."""
entities = []
for camera in config[CONF_SOURCE]:
entities.append(DlibFaceIdentifyEntity(
camera[CONF_ENTITY_ID], config[CONF_FACES], camera.get(CONF_NAME),
config[CONF_CONFIDENCE]
))
entities.append(
DlibFaceIdentifyEntity(
camera[CONF_ENTITY_ID],
config[CONF_FACES],
camera.get(CONF_NAME),
config[CONF_CONFIDENCE],
)
)
add_entities(entities)
@@ -40,6 +51,7 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity):
"""Initialize Dlib face identify entry."""
# pylint: disable=import-error
import face_recognition
super().__init__()
self._camera = camera_entity
@@ -47,15 +59,13 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity):
if name:
self._name = name
else:
self._name = "Dlib Face {0}".format(
split_entity_id(camera_entity)[1])
self._name = "Dlib Face {0}".format(split_entity_id(camera_entity)[1])
self._faces = {}
for face_name, face_file in faces.items():
try:
image = face_recognition.load_image_file(face_file)
self._faces[face_name] = \
face_recognition.face_encodings(image)[0]
self._faces[face_name] = face_recognition.face_encodings(image)[0]
except IndexError as err:
_LOGGER.error("Failed to parse %s. Error: %s", face_file, err)
@@ -77,7 +87,7 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity):
import face_recognition
fak_file = io.BytesIO(image)
fak_file.name = 'snapshot.jpg'
fak_file.name = "snapshot.jpg"
fak_file.seek(0)
image = face_recognition.load_image_file(fak_file)
@@ -87,12 +97,9 @@ class DlibFaceIdentifyEntity(ImageProcessingFaceEntity):
for unknown_face in unknowns:
for name, face in self._faces.items():
result = face_recognition.compare_faces(
[face],
unknown_face,
tolerance=self._tolerance)
[face], unknown_face, tolerance=self._tolerance
)
if result[0]:
found.append({
ATTR_NAME: name
})
found.append({ATTR_NAME: name})
self.process_faces(found, len(unknowns))