1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 04:50:05 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -6,27 +6,32 @@ import requests
import voluptuous as vol
from homeassistant.components.image_processing import (
CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE, PLATFORM_SCHEMA,
ImageProcessingEntity)
CONF_ENTITY_ID,
CONF_NAME,
CONF_SOURCE,
PLATFORM_SCHEMA,
ImageProcessingEntity,
)
from homeassistant.core import split_entity_id
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
ATTR_MATCHES = 'matches'
ATTR_TOTAL_MATCHES = 'total_matches'
ATTR_MATCHES = "matches"
ATTR_TOTAL_MATCHES = "total_matches"
CASCADE_URL = \
'https://raw.githubusercontent.com/opencv/opencv/master/data/' + \
'lbpcascades/lbpcascade_frontalface.xml'
CASCADE_URL = (
"https://raw.githubusercontent.com/opencv/opencv/master/data/"
+ "lbpcascades/lbpcascade_frontalface.xml"
)
CONF_CLASSIFIER = 'classifier'
CONF_FILE = 'file'
CONF_MIN_SIZE = 'min_size'
CONF_NEIGHBORS = 'neighbors'
CONF_SCALE = 'scale'
CONF_CLASSIFIER = "classifier"
CONF_FILE = "file"
CONF_MIN_SIZE = "min_size"
CONF_NEIGHBORS = "neighbors"
CONF_SCALE = "scale"
DEFAULT_CLASSIFIER_PATH = 'lbp_frontalface.xml'
DEFAULT_CLASSIFIER_PATH = "lbp_frontalface.xml"
DEFAULT_MIN_SIZE = (30, 30)
DEFAULT_NEIGHBORS = 4
DEFAULT_SCALE = 1.1
@@ -34,31 +39,37 @@ DEFAULT_TIMEOUT = 10
SCAN_INTERVAL = timedelta(seconds=2)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_CLASSIFIER): {
cv.string: vol.Any(
cv.isfile,
vol.Schema({
vol.Required(CONF_FILE): cv.isfile,
vol.Optional(CONF_SCALE, DEFAULT_SCALE): float,
vol.Optional(CONF_NEIGHBORS, DEFAULT_NEIGHBORS):
cv.positive_int,
vol.Optional(CONF_MIN_SIZE, DEFAULT_MIN_SIZE):
vol.Schema((int, int))
})
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_CLASSIFIER): {
cv.string: vol.Any(
cv.isfile,
vol.Schema(
{
vol.Required(CONF_FILE): cv.isfile,
vol.Optional(CONF_SCALE, DEFAULT_SCALE): float,
vol.Optional(
CONF_NEIGHBORS, DEFAULT_NEIGHBORS
): cv.positive_int,
vol.Optional(CONF_MIN_SIZE, DEFAULT_MIN_SIZE): vol.Schema(
(int, int)
),
}
),
)
}
}
})
)
def _create_processor_from_config(hass, camera_entity, config):
"""Create an OpenCV processor from configuration."""
classifier_config = config.get(CONF_CLASSIFIER)
name = '{} {}'.format(
config[CONF_NAME], split_entity_id(camera_entity)[1].replace('_', ' '))
name = "{} {}".format(
config[CONF_NAME], split_entity_id(camera_entity)[1].replace("_", " ")
)
processor = OpenCVImageProcessor(
hass, camera_entity, name, classifier_config)
processor = OpenCVImageProcessor(hass, camera_entity, name, classifier_config)
return processor
@@ -67,7 +78,7 @@ def _get_default_classifier(dest_path):
"""Download the default OpenCV classifier."""
_LOGGER.info("Downloading default classifier")
req = requests.get(CASCADE_URL, stream=True)
with open(dest_path, 'wb') as fil:
with open(dest_path, "wb") as fil:
for chunk in req.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
fil.write(chunk)
@@ -82,21 +93,25 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
except ImportError:
_LOGGER.error(
"No OpenCV library found! Install or compile for your system "
"following instructions here: http://opencv.org/releases.html")
"following instructions here: http://opencv.org/releases.html"
)
return
entities = []
if CONF_CLASSIFIER not in config:
dest_path = hass.config.path(DEFAULT_CLASSIFIER_PATH)
_get_default_classifier(dest_path)
config[CONF_CLASSIFIER] = {
'Face': dest_path
}
config[CONF_CLASSIFIER] = {"Face": dest_path}
for camera in config[CONF_SOURCE]:
entities.append(OpenCVImageProcessor(
hass, camera[CONF_ENTITY_ID], camera.get(CONF_NAME),
config[CONF_CLASSIFIER]))
entities.append(
OpenCVImageProcessor(
hass,
camera[CONF_ENTITY_ID],
camera.get(CONF_NAME),
config[CONF_CLASSIFIER],
)
)
add_entities(entities)
@@ -135,18 +150,14 @@ class OpenCVImageProcessor(ImageProcessingEntity):
@property
def state_attributes(self):
"""Return device specific state attributes."""
return {
ATTR_MATCHES: self._matches,
ATTR_TOTAL_MATCHES: self._total_matches
}
return {ATTR_MATCHES: self._matches, ATTR_TOTAL_MATCHES: self._total_matches}
def process_image(self, image):
"""Process the image."""
import cv2 # pylint: disable=import-error
import numpy
cv_image = cv2.imdecode(
numpy.asarray(bytearray(image)), cv2.IMREAD_UNCHANGED)
cv_image = cv2.imdecode(numpy.asarray(bytearray(image)), cv2.IMREAD_UNCHANGED)
for name, classifier in self._classifiers.items():
scale = DEFAULT_SCALE
@@ -163,10 +174,8 @@ class OpenCVImageProcessor(ImageProcessingEntity):
cascade = cv2.CascadeClassifier(path)
detections = cascade.detectMultiScale(
cv_image,
scaleFactor=scale,
minNeighbors=neighbors,
minSize=min_size)
cv_image, scaleFactor=scale, minNeighbors=neighbors, minSize=min_size
)
matches = {}
total_matches = 0
regions = []