From 32aae7017ecafd58808a1740abaac955b199e92d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 22 Dec 2019 10:32:42 +0100 Subject: [PATCH] Remove requirement from entity integration (#30113) --- .../components/doods/image_processing.py | 2 +- homeassistant/components/doods/manifest.json | 14 +++--- .../components/image_processing/__init__.py | 42 ----------------- .../components/image_processing/manifest.json | 8 +--- .../components/seven_segments/manifest.json | 2 +- .../components/tensorflow/image_processing.py | 2 +- .../components/tensorflow/manifest.json | 7 ++- homeassistant/util/pil.py | 47 +++++++++++++++++++ requirements_all.txt | 4 +- requirements_test_all.txt | 5 -- 10 files changed, 67 insertions(+), 66 deletions(-) create mode 100644 homeassistant/util/pil.py diff --git a/homeassistant/components/doods/image_processing.py b/homeassistant/components/doods/image_processing.py index 5e4d211d497..9525f9e8ddf 100644 --- a/homeassistant/components/doods/image_processing.py +++ b/homeassistant/components/doods/image_processing.py @@ -14,12 +14,12 @@ from homeassistant.components.image_processing import ( CONF_SOURCE, PLATFORM_SCHEMA, ImageProcessingEntity, - draw_box, ) from homeassistant.const import CONF_TIMEOUT from homeassistant.core import split_entity_id from homeassistant.helpers import template import homeassistant.helpers.config_validation as cv +from homeassistant.util.pil import draw_box _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/doods/manifest.json b/homeassistant/components/doods/manifest.json index e0dcb48527f..551af839b5c 100644 --- a/homeassistant/components/doods/manifest.json +++ b/homeassistant/components/doods/manifest.json @@ -1,10 +1,8 @@ { - "domain": "doods", - "name": "DOODS - Distributed Outside Object Detection Service", - "documentation": "https://www.home-assistant.io/integrations/doods", - "requirements": [ - "pydoods==1.0.2" - ], - "dependencies": [], - "codeowners": [] + "domain": "doods", + "name": "DOODS - Distributed Outside Object Detection Service", + "documentation": "https://www.home-assistant.io/integrations/doods", + "requirements": ["pydoods==1.0.2", "pillow==6.2.1"], + "dependencies": [], + "codeowners": [] } diff --git a/homeassistant/components/image_processing/__init__.py b/homeassistant/components/image_processing/__init__.py index 78ae15eb537..a8f5f0f097e 100644 --- a/homeassistant/components/image_processing/__init__.py +++ b/homeassistant/components/image_processing/__init__.py @@ -2,9 +2,7 @@ import asyncio from datetime import timedelta import logging -from typing import Tuple -from PIL import ImageDraw import voluptuous as vol from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, CONF_ENTITY_ID, CONF_NAME @@ -65,46 +63,6 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend( 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 = 3 - font_height = 8 - 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 - font_height)), text, fill=color - ) - - async def async_setup(hass, config): """Set up the image processing.""" component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL) diff --git a/homeassistant/components/image_processing/manifest.json b/homeassistant/components/image_processing/manifest.json index e986ac6f4ca..1e9f2963a38 100644 --- a/homeassistant/components/image_processing/manifest.json +++ b/homeassistant/components/image_processing/manifest.json @@ -2,11 +2,7 @@ "domain": "image_processing", "name": "Image processing", "documentation": "https://www.home-assistant.io/integrations/image_processing", - "requirements": [ - "pillow==6.2.1" - ], - "dependencies": [ - "camera" - ], + "requirements": [], + "dependencies": ["camera"], "codeowners": [] } diff --git a/homeassistant/components/seven_segments/manifest.json b/homeassistant/components/seven_segments/manifest.json index 0bf49d4b906..6e375da0322 100644 --- a/homeassistant/components/seven_segments/manifest.json +++ b/homeassistant/components/seven_segments/manifest.json @@ -2,7 +2,7 @@ "domain": "seven_segments", "name": "Seven segments", "documentation": "https://www.home-assistant.io/integrations/seven_segments", - "requirements": [], + "requirements": ["pillow==6.2.1"], "dependencies": [], "codeowners": [] } diff --git a/homeassistant/components/tensorflow/image_processing.py b/homeassistant/components/tensorflow/image_processing.py index 5f576f176e8..dee2a021829 100644 --- a/homeassistant/components/tensorflow/image_processing.py +++ b/homeassistant/components/tensorflow/image_processing.py @@ -15,11 +15,11 @@ from homeassistant.components.image_processing import ( CONF_SOURCE, PLATFORM_SCHEMA, ImageProcessingEntity, - draw_box, ) from homeassistant.core import split_entity_id from homeassistant.helpers import template import homeassistant.helpers.config_validation as cv +from homeassistant.util.pil import draw_box _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tensorflow/manifest.json b/homeassistant/components/tensorflow/manifest.json index 278a92a481f..627fa8d6bd6 100644 --- a/homeassistant/components/tensorflow/manifest.json +++ b/homeassistant/components/tensorflow/manifest.json @@ -2,7 +2,12 @@ "domain": "tensorflow", "name": "Tensorflow", "documentation": "https://www.home-assistant.io/integrations/tensorflow", - "requirements": ["tensorflow==1.13.2", "numpy==1.17.4", "protobuf==3.6.1"], + "requirements": [ + "tensorflow==1.13.2", + "numpy==1.17.4", + "protobuf==3.6.1", + "pillow==6.2.1" + ], "dependencies": [], "codeowners": [] } diff --git a/homeassistant/util/pil.py b/homeassistant/util/pil.py new file mode 100644 index 00000000000..80c11c9c410 --- /dev/null +++ b/homeassistant/util/pil.py @@ -0,0 +1,47 @@ +"""PIL utilities. + +Can only be used by integrations that have pillow in their requirements. +""" +from typing import Tuple + +from PIL import ImageDraw + + +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 = 3 + font_height = 8 + 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 - font_height)), text, fill=color + ) diff --git a/requirements_all.txt b/requirements_all.txt index 822e92ef759..19b97e2521f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -982,9 +982,11 @@ piglow==1.2.4 # homeassistant.components.pilight pilight==0.1.1 -# homeassistant.components.image_processing +# homeassistant.components.doods # homeassistant.components.proxy # homeassistant.components.qrcode +# homeassistant.components.seven_segments +# homeassistant.components.tensorflow pillow==6.2.1 # homeassistant.components.dominos diff --git a/requirements_test_all.txt b/requirements_test_all.txt index a7f443e0ccf..ab84629f12e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -323,11 +323,6 @@ pexpect==4.6.0 # homeassistant.components.pilight pilight==0.1.1 -# homeassistant.components.image_processing -# homeassistant.components.proxy -# homeassistant.components.qrcode -pillow==6.2.1 - # homeassistant.components.plex plexapi==3.3.0