[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
@@ -213,3 +213,64 @@ class TestImageProcessingAlpr(object):
|
||||
assert event_data[0]['plate'] == 'AC3829'
|
||||
assert event_data[0]['confidence'] == 98.3
|
||||
assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'
|
||||
|
||||
|
||||
class TestImageProcessingFaceIdentify(object):
|
||||
"""Test class for image processing."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
config = {
|
||||
ip.DOMAIN: {
|
||||
'platform': 'demo'
|
||||
},
|
||||
'camera': {
|
||||
'platform': 'demo'
|
||||
},
|
||||
}
|
||||
|
||||
with patch('homeassistant.components.image_processing.demo.'
|
||||
'DemoImageProcessingFaceIdentify.should_poll',
|
||||
new_callable=PropertyMock(return_value=False)):
|
||||
setup_component(self.hass, ip.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('camera.demo_camera')
|
||||
self.url = "{0}{1}".format(
|
||||
self.hass.config.api.base_url,
|
||||
state.attributes.get(ATTR_ENTITY_PICTURE))
|
||||
|
||||
self.face_events = []
|
||||
|
||||
@callback
|
||||
def mock_face_event(event):
|
||||
"""Mock event."""
|
||||
self.face_events.append(event)
|
||||
|
||||
self.hass.bus.listen('identify_face', mock_face_event)
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_face_event_call(self, aioclient_mock):
|
||||
"""Setup and scan a picture and test faces from event."""
|
||||
aioclient_mock.get(self.url, content=b'image')
|
||||
|
||||
ip.scan(self.hass, entity_id='image_processing.demo_face_identify')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('image_processing.demo_face_identify')
|
||||
|
||||
assert len(self.face_events) == 2
|
||||
assert state.state == 'Hans'
|
||||
assert state.attributes['total_faces'] == 4
|
||||
|
||||
event_data = [event.data for event in self.face_events if
|
||||
event.data.get('name') == 'Hans']
|
||||
assert len(event_data) == 1
|
||||
assert event_data[0]['name'] == 'Hans'
|
||||
assert event_data[0]['confidence'] == 98.34
|
||||
assert event_data[0]['entity_id'] == \
|
||||
'image_processing.demo_face_identify'
|
||||
@@ -0,0 +1,163 @@
|
||||
"""The tests for the microsoft face identify platform."""
|
||||
from unittest.mock import patch, PropertyMock
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import ATTR_ENTITY_PICTURE
|
||||
from homeassistant.bootstrap import setup_component
|
||||
import homeassistant.components.image_processing as ip
|
||||
import homeassistant.components.microsoft_face as mf
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component, load_fixture, mock_coro)
|
||||
|
||||
|
||||
class TestMicrosoftFaceIdentifySetup(object):
|
||||
"""Test class for image processing."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_setup_platform(self, store_mock):
|
||||
"""Setup platform with one entity."""
|
||||
config = {
|
||||
ip.DOMAIN: {
|
||||
'platform': 'microsoft_face_identify',
|
||||
'source': {
|
||||
'entity_id': 'camera.demo_camera'
|
||||
},
|
||||
'group': 'Test Group1',
|
||||
},
|
||||
'camera': {
|
||||
'platform': 'demo'
|
||||
},
|
||||
mf.DOMAIN: {
|
||||
'api_key': '12345678abcdef6',
|
||||
}
|
||||
}
|
||||
|
||||
with assert_setup_component(1, ip.DOMAIN):
|
||||
setup_component(self.hass, ip.DOMAIN, config)
|
||||
|
||||
assert self.hass.states.get(
|
||||
'image_processing.microsoftface_demo_camera')
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_setup_platform_name(self, store_mock):
|
||||
"""Setup platform with one entity and set name."""
|
||||
config = {
|
||||
ip.DOMAIN: {
|
||||
'platform': 'microsoft_face_identify',
|
||||
'source': {
|
||||
'entity_id': 'camera.demo_camera',
|
||||
'name': 'test local'
|
||||
},
|
||||
'group': 'Test Group1',
|
||||
},
|
||||
'camera': {
|
||||
'platform': 'demo'
|
||||
},
|
||||
mf.DOMAIN: {
|
||||
'api_key': '12345678abcdef6',
|
||||
}
|
||||
}
|
||||
|
||||
with assert_setup_component(1, ip.DOMAIN):
|
||||
setup_component(self.hass, ip.DOMAIN, config)
|
||||
|
||||
assert self.hass.states.get('image_processing.test_local')
|
||||
|
||||
|
||||
class TestMicrosoftFaceIdentify(object):
|
||||
"""Test class for image processing."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
self.config = {
|
||||
ip.DOMAIN: {
|
||||
'platform': 'microsoft_face_identify',
|
||||
'source': {
|
||||
'entity_id': 'camera.demo_camera',
|
||||
'name': 'test local'
|
||||
},
|
||||
'group': 'Test Group1',
|
||||
},
|
||||
'camera': {
|
||||
'platform': 'demo'
|
||||
},
|
||||
mf.DOMAIN: {
|
||||
'api_key': '12345678abcdef6',
|
||||
}
|
||||
}
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('homeassistant.components.image_processing.microsoft_face_identify.'
|
||||
'MicrosoftFaceIdentifyEntity.should_poll',
|
||||
new_callable=PropertyMock(return_value=False))
|
||||
def test_openalpr_process_image(self, poll_mock, aioclient_mock):
|
||||
"""Setup and scan a picture and test plates from event."""
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups"),
|
||||
text=load_fixture('microsoft_face_persongroups.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group1/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group2/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
|
||||
setup_component(self.hass, ip.DOMAIN, self.config)
|
||||
|
||||
state = self.hass.states.get('camera.demo_camera')
|
||||
url = "{0}{1}".format(
|
||||
self.hass.config.api.base_url,
|
||||
state.attributes.get(ATTR_ENTITY_PICTURE))
|
||||
|
||||
face_events = []
|
||||
|
||||
@callback
|
||||
def mock_face_event(event):
|
||||
"""Mock event."""
|
||||
face_events.append(event)
|
||||
|
||||
self.hass.bus.listen('identify_face', mock_face_event)
|
||||
|
||||
aioclient_mock.get(url, content=b'image')
|
||||
|
||||
aioclient_mock.post(
|
||||
mf.FACE_API_URL.format("detect"),
|
||||
text=load_fixture('microsoft_face_detect.json')
|
||||
)
|
||||
aioclient_mock.post(
|
||||
mf.FACE_API_URL.format("identify"),
|
||||
text=load_fixture('microsoft_face_identify.json')
|
||||
)
|
||||
|
||||
ip.scan(self.hass, entity_id='image_processing.test_local')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('image_processing.test_local')
|
||||
|
||||
assert len(face_events) == 1
|
||||
assert state.attributes.get('total_faces') == 2
|
||||
assert state.state == 'David'
|
||||
|
||||
assert face_events[0].data['name'] == 'David'
|
||||
assert face_events[0].data['confidence'] == float(92)
|
||||
assert face_events[0].data['entity_id'] == \
|
||||
'image_processing.test_local'
|
||||
@@ -0,0 +1,263 @@
|
||||
"""The tests for the microsoft face platform."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import homeassistant.components.microsoft_face as mf
|
||||
from homeassistant.bootstrap import setup_component
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component, mock_coro, load_fixture)
|
||||
|
||||
|
||||
class TestMicrosoftFaceSetup(object):
|
||||
"""Test the microsoft face component."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
self.config = {
|
||||
mf.DOMAIN: {
|
||||
'api_key': '12345678abcdef',
|
||||
}
|
||||
}
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_setup_component(self, mock_update):
|
||||
"""Setup component."""
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_setup_component_wrong_api_key(self, mock_update):
|
||||
"""Setup component without api key."""
|
||||
with assert_setup_component(0, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, {mf.DOMAIN: {}})
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_setup_component_test_service(self, mock_update):
|
||||
"""Setup component."""
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'create_group')
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'delete_group')
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'train_group')
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'create_person')
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'delete_person')
|
||||
assert self.hass.services.has_service(mf.DOMAIN, 'face_person')
|
||||
|
||||
def test_setup_component_test_entities(self, aioclient_mock):
|
||||
"""Setup component."""
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups"),
|
||||
text=load_fixture('microsoft_face_persongroups.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group1/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group2/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
|
||||
entity_group1 = self.hass.states.get('microsoft_face.test_group1')
|
||||
entity_group2 = self.hass.states.get('microsoft_face.test_group2')
|
||||
|
||||
assert entity_group1 is not None
|
||||
assert entity_group2 is not None
|
||||
|
||||
assert entity_group1.attributes['Ryan'] == \
|
||||
'25985303-c537-4467-b41d-bdb45cd95ca1'
|
||||
assert entity_group1.attributes['David'] == \
|
||||
'2ae4935b-9659-44c3-977f-61fac20d0538'
|
||||
|
||||
assert entity_group2.attributes['Ryan'] == \
|
||||
'25985303-c537-4467-b41d-bdb45cd95ca1'
|
||||
assert entity_group2.attributes['David'] == \
|
||||
'2ae4935b-9659-44c3-977f-61fac20d0538'
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_service_groups(self, mock_update, aioclient_mock):
|
||||
"""Setup component, test groups services."""
|
||||
aioclient_mock.put(
|
||||
mf.FACE_API_URL.format("persongroups/service_group"),
|
||||
status=200, text="{}"
|
||||
)
|
||||
aioclient_mock.delete(
|
||||
mf.FACE_API_URL.format("persongroups/service_group"),
|
||||
status=200, text="{}"
|
||||
)
|
||||
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
mf.create_group(self.hass, 'Service Group')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('microsoft_face.service_group')
|
||||
assert entity is not None
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
|
||||
mf.delete_group(self.hass, 'Service Group')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('microsoft_face.service_group')
|
||||
assert entity is None
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_service_person(self, aioclient_mock):
|
||||
"""Setup component, test person services."""
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups"),
|
||||
text=load_fixture('microsoft_face_persongroups.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group1/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group2/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
|
||||
aioclient_mock.post(
|
||||
mf.FACE_API_URL.format("persongroups/test_group1/persons"),
|
||||
text=load_fixture('microsoft_face_create_person.json')
|
||||
)
|
||||
aioclient_mock.delete(
|
||||
mf.FACE_API_URL.format(
|
||||
"persongroups/test_group1/persons/"
|
||||
"25985303-c537-4467-b41d-bdb45cd95ca1"),
|
||||
status=200, text="{}"
|
||||
)
|
||||
|
||||
mf.create_person(self.hass, 'test group1', 'Hans')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity_group1 = self.hass.states.get('microsoft_face.test_group1')
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 4
|
||||
assert entity_group1 is not None
|
||||
assert entity_group1.attributes['Hans'] == \
|
||||
'25985303-c537-4467-b41d-bdb45cd95ca1'
|
||||
|
||||
mf.delete_person(self.hass, 'test group1', 'Hans')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity_group1 = self.hass.states.get('microsoft_face.test_group1')
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 5
|
||||
assert entity_group1 is not None
|
||||
assert 'Hans' not in entity_group1.attributes
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_service_train(self, mock_update, aioclient_mock):
|
||||
"""Setup component, test train groups services."""
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.post(
|
||||
mf.FACE_API_URL.format("persongroups/service_group/train"),
|
||||
status=200, text="{}"
|
||||
)
|
||||
|
||||
mf.train_group(self.hass, 'Service Group')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
|
||||
@patch('homeassistant.components.camera.async_get_image',
|
||||
return_value=mock_coro(return_value=b'Test')())
|
||||
def test_service_face(self, camera_mock, aioclient_mock):
|
||||
"""Setup component, test person face services."""
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups"),
|
||||
text=load_fixture('microsoft_face_persongroups.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group1/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
aioclient_mock.get(
|
||||
mf.FACE_API_URL.format("persongroups/test_group2/persons"),
|
||||
text=load_fixture('microsoft_face_persons.json')
|
||||
)
|
||||
|
||||
self.config['camera'] = {'platform': 'demo'}
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
|
||||
aioclient_mock.post(
|
||||
mf.FACE_API_URL.format(
|
||||
"persongroups/test_group2/persons/"
|
||||
"2ae4935b-9659-44c3-977f-61fac20d0538/persistedFaces"),
|
||||
status=200, text="{}"
|
||||
)
|
||||
|
||||
mf.face_person(
|
||||
self.hass, 'test_group2', 'David', 'camera.demo_camera')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 4
|
||||
assert aioclient_mock.mock_calls[3][2] == b'Test'
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_service_status_400(self, mock_update, aioclient_mock):
|
||||
"""Setup component, test groups services with error."""
|
||||
aioclient_mock.put(
|
||||
mf.FACE_API_URL.format("persongroups/service_group"),
|
||||
status=400, text="{'error': {'message': 'Error'}}"
|
||||
)
|
||||
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
mf.create_group(self.hass, 'Service Group')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('microsoft_face.service_group')
|
||||
assert entity is None
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
|
||||
@patch('homeassistant.components.microsoft_face.'
|
||||
'MicrosoftFace.update_store', return_value=mock_coro()())
|
||||
def test_service_status_timeout(self, mock_update, aioclient_mock):
|
||||
"""Setup component, test groups services with timeout."""
|
||||
aioclient_mock.put(
|
||||
mf.FACE_API_URL.format("persongroups/service_group"),
|
||||
status=400, exc=asyncio.TimeoutError()
|
||||
)
|
||||
|
||||
with assert_setup_component(2, mf.DOMAIN):
|
||||
setup_component(self.hass, mf.DOMAIN, self.config)
|
||||
|
||||
mf.create_group(self.hass, 'Service Group')
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('microsoft_face.service_group')
|
||||
assert entity is None
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
Reference in new issue
Block a user