Add support for face recognition with dlib (#7421)
* Add support for face recognition with dlib * fix lint * fix lint p2 * update library * dlib can not build * fix lint * Fix int p1 * Update dlib_face_detect.py * Update dlib_face_detect.py * Update dlib_face_detect.py
This commit is contained in:
parent
e3bb45c906
commit
7e539a3cb2
6 changed files with 174 additions and 3 deletions
|
@ -237,6 +237,8 @@ omit =
|
|||
homeassistant/components/foursquare.py
|
||||
homeassistant/components/hdmi_cec.py
|
||||
homeassistant/components/ifttt.py
|
||||
homeassistant/components/image_processing/dlib_face_detect.py
|
||||
homeassistant/components/image_processing/dlib_face_identify.py
|
||||
homeassistant/components/joaoapps_join.py
|
||||
homeassistant/components/keyboard.py
|
||||
homeassistant/components/keyboard_remote.py
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
"""
|
||||
Component that will help set the dlib face detect processing.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/image_processing.dlib_face_detect/
|
||||
"""
|
||||
import logging
|
||||
import io
|
||||
|
||||
from homeassistant.core import split_entity_id
|
||||
# pylint: disable=unused-import
|
||||
from homeassistant.components.image_processing import PLATFORM_SCHEMA # noqa
|
||||
from homeassistant.components.image_processing import (
|
||||
CONF_SOURCE, CONF_ENTITY_ID, CONF_NAME)
|
||||
from homeassistant.components.image_processing.microsoft_face_identify import (
|
||||
ImageProcessingFaceEntity)
|
||||
|
||||
REQUIREMENTS = ['face_recognition==0.1.14']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the Microsoft Face detection platform."""
|
||||
entities = []
|
||||
for camera in config[CONF_SOURCE]:
|
||||
entities.append(DlibFaceDetectEntity(
|
||||
camera[CONF_ENTITY_ID], camera.get(CONF_NAME)
|
||||
))
|
||||
|
||||
add_devices(entities)
|
||||
|
||||
|
||||
class DlibFaceDetectEntity(ImageProcessingFaceEntity):
|
||||
"""Dlib Face API entity for identify."""
|
||||
|
||||
def __init__(self, camera_entity, name=None):
|
||||
"""Initialize Dlib."""
|
||||
super().__init__()
|
||||
|
||||
self._camera = camera_entity
|
||||
|
||||
if name:
|
||||
self._name = name
|
||||
else:
|
||||
self._name = "Dlib Face {0}".format(
|
||||
split_entity_id(camera_entity)[1])
|
||||
|
||||
@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
|
||||
|
||||
def process_image(self, image):
|
||||
"""Process image."""
|
||||
# pylint: disable=import-error
|
||||
import face_recognition
|
||||
|
||||
fak_file = io.BytesIO(image)
|
||||
fak_file.name = "snapshot.jpg"
|
||||
fak_file.seek(0)
|
||||
|
||||
image = face_recognition.load_image_file(fak_file)
|
||||
face_locations = face_recognition.face_locations(image)
|
||||
|
||||
self.process_faces(face_locations, len(face_locations))
|
|
@ -0,0 +1,95 @@
|
|||
"""
|
||||
Component that will help set the dlib face detect processing.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/image_processing.dlib_face_identify/
|
||||
"""
|
||||
import logging
|
||||
import io
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.components.image_processing import (
|
||||
PLATFORM_SCHEMA, CONF_SOURCE, CONF_ENTITY_ID, CONF_NAME)
|
||||
from homeassistant.components.image_processing.microsoft_face_identify import (
|
||||
ImageProcessingFaceEntity)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['face_recognition==0.1.14']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_NAME = 'name'
|
||||
CONF_FACES = 'faces'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_FACES): {cv.string: cv.isfile},
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the Microsoft Face detection platform."""
|
||||
entities = []
|
||||
for camera in config[CONF_SOURCE]:
|
||||
entities.append(DlibFaceIdentifyEntity(
|
||||
camera[CONF_ENTITY_ID], config[CONF_FACES], camera.get(CONF_NAME)
|
||||
))
|
||||
|
||||
add_devices(entities)
|
||||
|
||||
|
||||
class DlibFaceIdentifyEntity(ImageProcessingFaceEntity):
|
||||
"""Dlib Face API entity for identify."""
|
||||
|
||||
def __init__(self, camera_entity, faces, name=None):
|
||||
"""Initialize Dlib."""
|
||||
# pylint: disable=import-error
|
||||
import face_recognition
|
||||
super().__init__()
|
||||
|
||||
self._camera = camera_entity
|
||||
|
||||
if name:
|
||||
self._name = name
|
||||
else:
|
||||
self._name = "Dlib Face {0}".format(
|
||||
split_entity_id(camera_entity)[1])
|
||||
|
||||
self._faces = {}
|
||||
for name, face_file in faces.items():
|
||||
image = face_recognition.load_image_file(face_file)
|
||||
self._faces[name] = face_recognition.face_encodings(image)[0]
|
||||
|
||||
@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
|
||||
|
||||
def process_image(self, image):
|
||||
"""Process image."""
|
||||
# pylint: disable=import-error
|
||||
import face_recognition
|
||||
|
||||
fak_file = io.BytesIO(image)
|
||||
fak_file.name = "snapshot.jpg"
|
||||
fak_file.seek(0)
|
||||
|
||||
image = face_recognition.load_image_file(fak_file)
|
||||
unknowns = face_recognition.face_encodings(image)
|
||||
|
||||
found = []
|
||||
for unknown_face in unknowns:
|
||||
for name, face in self._faces.items():
|
||||
result = face_recognition.compare_faces([face], unknown_face)
|
||||
if result[0]:
|
||||
found.append({
|
||||
ATTR_NAME: name
|
||||
})
|
||||
|
||||
self.process_faces(found, len(unknowns))
|
|
@ -22,8 +22,6 @@ DEPENDENCIES = ['microsoft_face']
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
EVENT_IDENTIFY_FACE = 'detect_face'
|
||||
|
||||
SUPPORTED_ATTRIBUTES = [
|
||||
ATTR_AGE,
|
||||
ATTR_GENDER,
|
||||
|
|
|
@ -174,6 +174,10 @@ envirophat==0.0.6
|
|||
# homeassistant.components.climate.honeywell
|
||||
evohomeclient==0.2.5
|
||||
|
||||
# homeassistant.components.image_processing.dlib_face_detect
|
||||
# homeassistant.components.image_processing.dlib_face_identify
|
||||
# face_recognition==0.1.14
|
||||
|
||||
# homeassistant.components.sensor.fastdotcom
|
||||
fastdotcom==0.0.1
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@ COMMENT_REQUIREMENTS = (
|
|||
'pycups',
|
||||
'python-eq3bt',
|
||||
'avion',
|
||||
'decora'
|
||||
'decora',
|
||||
'face_recognition'
|
||||
)
|
||||
|
||||
IGNORE_PACKAGES = (
|
||||
|
|
Loading…
Add table
Reference in a new issue