Created
May 2, 2017 03:07
-
-
Save Teagan42/4e64b4f97680d468d7c06a4e985b3aac to your computer and use it in GitHub Desktop.
OpenCV camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Component that performs OpenCV classification on images. | |
For more details about this component, please refer to the documentation at | |
https://home-assistant.io/components/camera.opencv/ | |
""" | |
import asyncio | |
import logging | |
from homeassistant.core import split_entity_id | |
from homeassistant.components.camera import Camera, PLATFORM_SCHEMA | |
from homeassistant.components.opencv import ( | |
process_image, | |
CLASSIFIER_GROUP_CONFIG, | |
CONF_CLASSIFIER, | |
CONF_ENTITY_ID, | |
CONF_NAME | |
) | |
from homeassistant.exceptions import HomeAssistantError | |
from homeassistant.loader import get_component | |
_LOGGER = logging.getLogger(__name__) | |
DEFAULT_NAME = 'OpenCV' | |
DEFAULT_TIMEOUT = 10 | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(CLASSIFIER_GROUP_CONFIG) | |
def _create_camera_from_config(hass, camera_entity, config): | |
"""Create an OpenCV camera from configurtaion.""" | |
classifier_config = config[CONF_CLASSIFIER] | |
name = '{} {}'.format( | |
config[CONF_NAME], | |
split_entity_id(camera_entity)[1]) | |
processor = OpenCVCamera( | |
hass, | |
camera_entity, | |
name, | |
classifier_config, | |
) | |
return processor | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
"""Setup a OpenCV Camera.""" | |
if not discovery_info: | |
return | |
entities = [] | |
_LOGGER.info(str(discovery_info)) | |
# Create camera from discovery info | |
for camera_entity in discovery_info[CONF_ENTITY_ID]: | |
entities.append( | |
_create_camera_from_config( | |
hass, | |
camera_entity, | |
discovery_info)) | |
add_devices(entities) | |
class OpenCVCamera(Camera): | |
"""OpenCV camera entity.""" | |
timeout = DEFAULT_TIMEOUT | |
def __init__(self, hass, camera_entity, name, classifier_group): | |
"""Initialize OpenCV Camera component.""" | |
super().__init__() | |
self._classifier_group = classifier_group | |
self._matches = {} | |
self._hass = hass | |
self._name = name | |
self._camera_entity = camera_entity | |
@property | |
def name(self): | |
"""Return the name of this camera.""" | |
return self._name | |
@asyncio.coroutine | |
def async_camera_image(self): | |
"""Perform the update asynchronously.""" | |
camera = get_component('camera') | |
try: | |
image = yield from camera.async_get_image(self._hass, | |
self._camera_entity, | |
timeout=self.timeout) | |
except HomeAssistantError as err: | |
_LOGGER.error("Error on receive image from entity: %s", err) | |
return | |
return process_image(image, self._classifier_group, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment