Black
This commit is contained in:
parent
da05dfe708
commit
4de97abc3a
2676 changed files with 163166 additions and 140084 deletions
|
@ -10,8 +10,15 @@ import homeassistant.helpers.config_validation as cv
|
|||
from homeassistant.core import split_entity_id, callback
|
||||
from homeassistant.const import CONF_REGION
|
||||
from homeassistant.components.image_processing import (
|
||||
PLATFORM_SCHEMA, ImageProcessingEntity, CONF_CONFIDENCE, CONF_SOURCE,
|
||||
CONF_ENTITY_ID, CONF_NAME, ATTR_ENTITY_ID, ATTR_CONFIDENCE)
|
||||
PLATFORM_SCHEMA,
|
||||
ImageProcessingEntity,
|
||||
CONF_CONFIDENCE,
|
||||
CONF_SOURCE,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_CONFIDENCE,
|
||||
)
|
||||
from homeassistant.util.async_ import run_callback_threadsafe
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -19,48 +26,51 @@ _LOGGER = logging.getLogger(__name__)
|
|||
RE_ALPR_PLATE = re.compile(r"^plate\d*:")
|
||||
RE_ALPR_RESULT = re.compile(r"- (\w*)\s*confidence: (\d*.\d*)")
|
||||
|
||||
EVENT_FOUND_PLATE = 'image_processing.found_plate'
|
||||
EVENT_FOUND_PLATE = "image_processing.found_plate"
|
||||
|
||||
ATTR_PLATE = 'plate'
|
||||
ATTR_PLATES = 'plates'
|
||||
ATTR_VEHICLES = 'vehicles'
|
||||
ATTR_PLATE = "plate"
|
||||
ATTR_PLATES = "plates"
|
||||
ATTR_VEHICLES = "vehicles"
|
||||
|
||||
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_ALPR_BIN = 'alp_bin'
|
||||
CONF_ALPR_BIN = "alp_bin"
|
||||
|
||||
DEFAULT_BINARY = 'alpr'
|
||||
DEFAULT_BINARY = "alpr"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_REGION): vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
vol.Optional(CONF_ALPR_BIN, default=DEFAULT_BINARY): cv.string,
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_REGION): vol.All(vol.Lower, vol.In(OPENALPR_REGIONS)),
|
||||
vol.Optional(CONF_ALPR_BIN, default=DEFAULT_BINARY): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
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 local platform."""
|
||||
command = [config[CONF_ALPR_BIN], '-c', config[CONF_REGION], '-']
|
||||
command = [config[CONF_ALPR_BIN], "-c", config[CONF_REGION], "-"]
|
||||
confidence = config[CONF_CONFIDENCE]
|
||||
|
||||
entities = []
|
||||
for camera in config[CONF_SOURCE]:
|
||||
entities.append(OpenAlprLocalEntity(
|
||||
camera[CONF_ENTITY_ID], command, confidence, camera.get(CONF_NAME)
|
||||
))
|
||||
entities.append(
|
||||
OpenAlprLocalEntity(
|
||||
camera[CONF_ENTITY_ID], command, confidence, camera.get(CONF_NAME)
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
@ -89,15 +99,12 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
|
|||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
return 'alpr'
|
||||
return "alpr"
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return device specific state attributes."""
|
||||
attr = {
|
||||
ATTR_PLATES: self.plates,
|
||||
ATTR_VEHICLES: self.vehicles
|
||||
}
|
||||
attr = {ATTR_PLATES: self.plates, ATTR_VEHICLES: self.vehicles}
|
||||
|
||||
return attr
|
||||
|
||||
|
@ -116,18 +123,23 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
|
|||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
plates = {plate: confidence for plate, confidence in plates.items()
|
||||
if confidence >= self.confidence}
|
||||
plates = {
|
||||
plate: confidence
|
||||
for plate, confidence in plates.items()
|
||||
if confidence >= self.confidence
|
||||
}
|
||||
new_plates = set(plates) - set(self.plates)
|
||||
|
||||
# Send events
|
||||
for i_plate in new_plates:
|
||||
self.hass.async_add_job(
|
||||
self.hass.bus.async_fire, EVENT_FOUND_PLATE, {
|
||||
self.hass.bus.async_fire,
|
||||
EVENT_FOUND_PLATE,
|
||||
{
|
||||
ATTR_PLATE: i_plate,
|
||||
ATTR_ENTITY_ID: self.entity_id,
|
||||
ATTR_CONFIDENCE: plates.get(i_plate),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Update entity store
|
||||
|
@ -149,8 +161,7 @@ class OpenAlprLocalEntity(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):
|
||||
|
@ -180,12 +191,12 @@ class OpenAlprLocalEntity(ImageProcessingAlprEntity):
|
|||
loop=self.hass.loop,
|
||||
stdin=asyncio.subprocess.PIPE,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.DEVNULL
|
||||
stderr=asyncio.subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
# Send image
|
||||
stdout, _ = await alpr.communicate(input=image)
|
||||
stdout = io.StringIO(str(stdout, 'utf-8'))
|
||||
stdout = io.StringIO(str(stdout, "utf-8"))
|
||||
|
||||
while True:
|
||||
line = stdout.readline()
|
||||
|
@ -203,8 +214,7 @@ class OpenAlprLocalEntity(ImageProcessingAlprEntity):
|
|||
# Found plate result
|
||||
if new_result:
|
||||
try:
|
||||
result.update(
|
||||
{new_result.group(1): float(new_result.group(2))})
|
||||
result.update({new_result.group(1): float(new_result.group(2))})
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue