This request is to add support for the Microsoft Computer Vision API
It is similar to the current Microsoft Face API component, but instead it can describe an still image taken from a camera
I tried to create my own custom components, but having problems getting the image content, and async calls…here is my code as reference
import asyncio
import logging
import requests
import json
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.exceptions import HomeAssistantError
from homeassistant.const import CONF_API_KEY, CONF_TIMEOUT, ATTR_NAME
from homeassistant.components.microsoft_face import CONF_AZURE_REGION, ATTR_CAMERA_ENTITY
_LOGGER = logging.getLogger(name)
DOMAIN = ‘microsoft_vision’
VISION_URL = “http://eastus2.api.cognitive.microsoft.com/vision/v2.0/analyze?visualFeatures=Categories,Description”
DEPENDENCIES = [‘camera’]
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_AZURE_REGION, default=“eastus2”): cv.string,
}),
}, extra=vol.ALLOW_EXTRA)
SCHEMA_CALL_SERVICE = vol.Schema({
vol.Required(ATTR_CAMERA_ENTITY): cv.string,
})
async def async_setup(hass, config):
azure_region = config[DOMAIN].get(CONF_AZURE_REGION)
api_key = config[DOMAIN].get(CONF_API_KEY)
async def call_api(service):
entity_id = service.data[ATTR_CAMERA_ENTITY]
#component = hass.data.get('camera')
component = hass.components.camera
if component is None:
_LOGGER.error('Camera component not set up')
#camera = component.get_entity(entity_id)
#if camera is None:
# _LOGGER.error('Camera not found')
#if not camera.is_on:
# _LOGGER.error('Camera is off')
try:
image = await component.async_get_image(hass, entity_id)
except HomeAssistantError as err:
_LOGGER.error("Failed to get image from %s, with error: %s", entity_id, err)
try:
headers = {"Ocp-Apim-Subscription-Key": api_key,
"Content-Type": "application/octet-stream"}
response = requests.post(VISION_URL, headers=headers, data=image.content)
_LOGGER.error(response.text)
except HomeAssistantError as err:
_LOGGER.error("Failed to call Microsoft API with error: %s", err)
hass.services.async_register(DOMAIN, 'call_api', call_api,
schema=SCHEMA_CALL_SERVICE)
return True