mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Put draw_box in image_processing (#26712)
* Put draw_box in image_processing * Update draw_box * Update __init__.py * run isort * Fix lints * Update __init__.py * Update requirements_all.txt * Adds type hints * Update gen_requirements_all.py * Update requirements_test_all.txt * rerun script/gen_requirements_all.py * Change Pillow to pillow * Update manifest.json * Run script/gen_requirements_all.py
This commit is contained in:
parent
53e6b8ade6
commit
1d60cccc21
@ -14,6 +14,7 @@ from homeassistant.components.image_processing import (
|
|||||||
CONF_SOURCE,
|
CONF_SOURCE,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
ImageProcessingEntity,
|
ImageProcessingEntity,
|
||||||
|
draw_box,
|
||||||
)
|
)
|
||||||
from homeassistant.core import split_entity_id
|
from homeassistant.core import split_entity_id
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
@ -68,24 +69,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def draw_box(draw, box, img_width, img_height, text="", color=(255, 255, 0)):
|
|
||||||
"""Draw bounding box on image."""
|
|
||||||
ymin, xmin, ymax, xmax = box
|
|
||||||
(left, right, top, bottom) = (
|
|
||||||
xmin * img_width,
|
|
||||||
xmax * img_width,
|
|
||||||
ymin * img_height,
|
|
||||||
ymax * img_height,
|
|
||||||
)
|
|
||||||
draw.line(
|
|
||||||
[(left, top), (left, bottom), (right, bottom), (right, top), (left, top)],
|
|
||||||
width=5,
|
|
||||||
fill=color,
|
|
||||||
)
|
|
||||||
if text:
|
|
||||||
draw.text((left, abs(top - 15)), text, fill=color)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Doods client."""
|
"""Set up the Doods client."""
|
||||||
url = config[CONF_URL]
|
url = config[CONF_URL]
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
from PIL import ImageDraw
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, CONF_ENTITY_ID, CONF_NAME
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, CONF_ENTITY_ID, CONF_NAME
|
||||||
@ -14,7 +16,6 @@ from homeassistant.helpers.entity import Entity
|
|||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.util.async_ import run_callback_threadsafe
|
from homeassistant.util.async_ import run_callback_threadsafe
|
||||||
|
|
||||||
|
|
||||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -64,6 +65,43 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
|
|||||||
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE.extend(PLATFORM_SCHEMA.schema)
|
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE.extend(PLATFORM_SCHEMA.schema)
|
||||||
|
|
||||||
|
|
||||||
|
def draw_box(
|
||||||
|
draw: ImageDraw,
|
||||||
|
box: Tuple[float, float, float, float],
|
||||||
|
img_width: int,
|
||||||
|
img_height: int,
|
||||||
|
text: str = "",
|
||||||
|
color: Tuple[int, int, int] = (255, 255, 0),
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Draw a bounding box on and image.
|
||||||
|
|
||||||
|
The bounding box is defined by the tuple (y_min, x_min, y_max, x_max)
|
||||||
|
where the coordinates are floats in the range [0.0, 1.0] and
|
||||||
|
relative to the width and height of the image.
|
||||||
|
|
||||||
|
For example, if an image is 100 x 200 pixels (height x width) and the bounding
|
||||||
|
box is `(0.1, 0.2, 0.5, 0.9)`, the upper-left and bottom-right coordinates of
|
||||||
|
the bounding box will be `(40, 10)` to `(180, 50)` (in (x,y) coordinates).
|
||||||
|
"""
|
||||||
|
|
||||||
|
line_width = 5
|
||||||
|
y_min, x_min, y_max, x_max = box
|
||||||
|
(left, right, top, bottom) = (
|
||||||
|
x_min * img_width,
|
||||||
|
x_max * img_width,
|
||||||
|
y_min * img_height,
|
||||||
|
y_max * img_height,
|
||||||
|
)
|
||||||
|
draw.line(
|
||||||
|
[(left, top), (left, bottom), (right, bottom), (right, top), (left, top)],
|
||||||
|
width=line_width,
|
||||||
|
fill=color,
|
||||||
|
)
|
||||||
|
if text:
|
||||||
|
draw.text((left + line_width, abs(top - line_width)), text, fill=color)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the image processing."""
|
"""Set up the image processing."""
|
||||||
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
|
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
"domain": "image_processing",
|
"domain": "image_processing",
|
||||||
"name": "Image processing",
|
"name": "Image processing",
|
||||||
"documentation": "https://www.home-assistant.io/components/image_processing",
|
"documentation": "https://www.home-assistant.io/components/image_processing",
|
||||||
"requirements": [],
|
"requirements": [
|
||||||
|
"pillow==6.1.0"
|
||||||
|
],
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"camera"
|
"camera"
|
||||||
],
|
],
|
||||||
|
@ -12,6 +12,7 @@ from homeassistant.components.image_processing import (
|
|||||||
CONF_SOURCE,
|
CONF_SOURCE,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
ImageProcessingEntity,
|
ImageProcessingEntity,
|
||||||
|
draw_box,
|
||||||
)
|
)
|
||||||
from homeassistant.core import split_entity_id
|
from homeassistant.core import split_entity_id
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
@ -67,24 +68,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def draw_box(draw, box, img_width, img_height, text="", color=(255, 255, 0)):
|
|
||||||
"""Draw bounding box on image."""
|
|
||||||
ymin, xmin, ymax, xmax = box
|
|
||||||
(left, right, top, bottom) = (
|
|
||||||
xmin * img_width,
|
|
||||||
xmax * img_width,
|
|
||||||
ymin * img_height,
|
|
||||||
ymax * img_height,
|
|
||||||
)
|
|
||||||
draw.line(
|
|
||||||
[(left, top), (left, bottom), (right, bottom), (right, top), (left, top)],
|
|
||||||
width=5,
|
|
||||||
fill=color,
|
|
||||||
)
|
|
||||||
if text:
|
|
||||||
draw.text((left, abs(top - 15)), text, fill=color)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the TensorFlow image processing platform."""
|
"""Set up the TensorFlow image processing platform."""
|
||||||
model_config = config.get(CONF_MODEL)
|
model_config = config.get(CONF_MODEL)
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
"requirements": [
|
"requirements": [
|
||||||
"tensorflow==1.13.2",
|
"tensorflow==1.13.2",
|
||||||
"numpy==1.17.1",
|
"numpy==1.17.1",
|
||||||
"pillow==6.1.0",
|
|
||||||
"protobuf==3.6.1"
|
"protobuf==3.6.1"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
|
@ -950,9 +950,9 @@ piglow==1.2.4
|
|||||||
# homeassistant.components.pilight
|
# homeassistant.components.pilight
|
||||||
pilight==0.1.1
|
pilight==0.1.1
|
||||||
|
|
||||||
|
# homeassistant.components.image_processing
|
||||||
# homeassistant.components.proxy
|
# homeassistant.components.proxy
|
||||||
# homeassistant.components.qrcode
|
# homeassistant.components.qrcode
|
||||||
# homeassistant.components.tensorflow
|
|
||||||
pillow==6.1.0
|
pillow==6.1.0
|
||||||
|
|
||||||
# homeassistant.components.dominos
|
# homeassistant.components.dominos
|
||||||
|
@ -252,6 +252,11 @@ pexpect==4.6.0
|
|||||||
# homeassistant.components.pilight
|
# homeassistant.components.pilight
|
||||||
pilight==0.1.1
|
pilight==0.1.1
|
||||||
|
|
||||||
|
# homeassistant.components.image_processing
|
||||||
|
# homeassistant.components.proxy
|
||||||
|
# homeassistant.components.qrcode
|
||||||
|
pillow==6.1.0
|
||||||
|
|
||||||
# homeassistant.components.plex
|
# homeassistant.components.plex
|
||||||
plexapi==3.0.6
|
plexapi==3.0.6
|
||||||
|
|
||||||
|
@ -111,6 +111,7 @@ TEST_REQUIREMENTS = (
|
|||||||
"paho-mqtt",
|
"paho-mqtt",
|
||||||
"pexpect",
|
"pexpect",
|
||||||
"pilight",
|
"pilight",
|
||||||
|
"pillow",
|
||||||
"plexapi",
|
"plexapi",
|
||||||
"pmsensor",
|
"pmsensor",
|
||||||
"prometheus_client",
|
"prometheus_client",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user