Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -11,9 +11,15 @@ import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.components.image_processing import (
|
||||
PLATFORM_SCHEMA, CONF_CONFIDENCE, CONF_SOURCE, CONF_ENTITY_ID, CONF_NAME)
|
||||
PLATFORM_SCHEMA,
|
||||
CONF_CONFIDENCE,
|
||||
CONF_SOURCE,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
)
|
||||
from homeassistant.components.openalpr_local.image_processing import (
|
||||
ImageProcessingAlprEntity)
|
||||
ImageProcessingAlprEntity,
|
||||
)
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -21,44 +27,47 @@ _LOGGER = logging.getLogger(__name__)
|
||||
OPENALPR_API_URL = "https://api.openalpr.com/v1/recognize"
|
||||
|
||||
OPENALPR_REGIONS = [
|
||||
'au',
|
||||
'auwide',
|
||||
'br',
|
||||
'eu',
|
||||
'fr',
|
||||
'gb',
|
||||
'kr',
|
||||
'kr2',
|
||||
'mx',
|
||||
'sg',
|
||||
'us',
|
||||
'vn2'
|
||||
"au",
|
||||
"auwide",
|
||||
"br",
|
||||
"eu",
|
||||
"fr",
|
||||
"gb",
|
||||
"kr",
|
||||
"kr2",
|
||||
"mx",
|
||||
"sg",
|
||||
"us",
|
||||
"vn2",
|
||||
]
|
||||
|
||||
CONF_REGION = 'region'
|
||||
CONF_REGION = "region"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_REGION): vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_REGION): vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the OpenALPR cloud API platform."""
|
||||
confidence = config[CONF_CONFIDENCE]
|
||||
params = {
|
||||
'secret_key': config[CONF_API_KEY],
|
||||
'tasks': "plate",
|
||||
'return_image': 0,
|
||||
'country': config[CONF_REGION],
|
||||
"secret_key": config[CONF_API_KEY],
|
||||
"tasks": "plate",
|
||||
"return_image": 0,
|
||||
"country": config[CONF_REGION],
|
||||
}
|
||||
|
||||
entities = []
|
||||
for camera in config[CONF_SOURCE]:
|
||||
entities.append(OpenAlprCloudEntity(
|
||||
camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME)
|
||||
))
|
||||
entities.append(
|
||||
OpenAlprCloudEntity(
|
||||
camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME)
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
@@ -77,8 +86,7 @@ class OpenAlprCloudEntity(ImageProcessingAlprEntity):
|
||||
if name:
|
||||
self._name = name
|
||||
else:
|
||||
self._name = "OpenAlpr {0}".format(
|
||||
split_entity_id(camera_entity)[1])
|
||||
self._name = "OpenAlpr {0}".format(split_entity_id(camera_entity)[1])
|
||||
|
||||
@property
|
||||
def confidence(self):
|
||||
@@ -103,9 +111,7 @@ class OpenAlprCloudEntity(ImageProcessingAlprEntity):
|
||||
websession = async_get_clientsession(self.hass)
|
||||
params = self._params.copy()
|
||||
|
||||
body = {
|
||||
'image_bytes': str(b64encode(image), 'utf-8')
|
||||
}
|
||||
body = {"image_bytes": str(b64encode(image), "utf-8")}
|
||||
|
||||
try:
|
||||
with async_timeout.timeout(self.timeout):
|
||||
@@ -116,8 +122,7 @@ class OpenAlprCloudEntity(ImageProcessingAlprEntity):
|
||||
data = await request.json()
|
||||
|
||||
if request.status != 200:
|
||||
_LOGGER.error("Error %d -> %s.",
|
||||
request.status, data.get('error'))
|
||||
_LOGGER.error("Error %d -> %s.", request.status, data.get("error"))
|
||||
return
|
||||
|
||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||
@@ -128,13 +133,12 @@ class OpenAlprCloudEntity(ImageProcessingAlprEntity):
|
||||
vehicles = 0
|
||||
result = {}
|
||||
|
||||
for row in data['plate']['results']:
|
||||
for row in data["plate"]["results"]:
|
||||
vehicles += 1
|
||||
|
||||
for p_data in row['candidates']:
|
||||
for p_data in row["candidates"]:
|
||||
try:
|
||||
result.update(
|
||||
{p_data['plate']: float(p_data['confidence'])})
|
||||
result.update({p_data["plate"]: float(p_data["confidence"])})
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
|
||||
Reference in new issue
Block a user