feat(seven_segments): add flags for debug image generation & input image saving
Signed-off-by: Arian Nasr <arian@2ari.ca>
This commit is contained in:
@@ -30,6 +30,8 @@ CONF_THRESHOLD = "threshold"
|
||||
CONF_WIDTH = "width"
|
||||
CONF_X_POS = "x_position"
|
||||
CONF_Y_POS = "y_position"
|
||||
CONF_WRITE_FILE = "write_file"
|
||||
CONF_DEBUG_IMAGE = "debug_image"
|
||||
|
||||
DEFAULT_BINARY = "ssocr"
|
||||
|
||||
@@ -44,6 +46,8 @@ PLATFORM_SCHEMA = IMAGE_PROCESSING_PLATFORM_SCHEMA.extend(
|
||||
vol.Optional(CONF_WIDTH, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_X_POS, default=0): cv.string,
|
||||
vol.Optional(CONF_Y_POS, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_WRITE_FILE, default=True): cv.boolean,
|
||||
vol.Optional(CONF_DEBUG_IMAGE, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -83,10 +87,19 @@ class ImageProcessingSsocr(ImageProcessingEntity):
|
||||
self._attr_name = f"SevenSegment OCR {split_entity_id(camera_entity)[1]}"
|
||||
self._attr_state = None
|
||||
|
||||
self._write_file = config[CONF_WRITE_FILE]
|
||||
self._debug_image = config[CONF_DEBUG_IMAGE]
|
||||
|
||||
safe_name = self._attr_name.replace(' ', '_')
|
||||
self.filepath = os.path.join(
|
||||
hass.config.config_dir,
|
||||
f"ssocr-{self._attr_name.replace(' ', '_')}.png",
|
||||
f"ssocr-{safe_name}.png",
|
||||
)
|
||||
self.debug_filepath = os.path.join(
|
||||
hass.config.config_dir,
|
||||
f"ssocr-debug-{safe_name}.png",
|
||||
)
|
||||
|
||||
crop = [
|
||||
"crop",
|
||||
str(config[CONF_X_POS]),
|
||||
@@ -107,21 +120,36 @@ class ImageProcessingSsocr(ImageProcessingEntity):
|
||||
*rotate,
|
||||
*extra_arguments,
|
||||
]
|
||||
self._command.append(self.filepath)
|
||||
|
||||
if self._debug_image:
|
||||
self._command.append(f"--debug-image={self.debug_filepath}")
|
||||
|
||||
if self._write_file:
|
||||
self._command.append(self.filepath)
|
||||
else:
|
||||
self._command.append("-")
|
||||
|
||||
def process_image(self, image: bytes) -> None:
|
||||
"""Process the image."""
|
||||
stream = io.BytesIO(image)
|
||||
img = Image.open(stream)
|
||||
img.save(self.filepath, "png")
|
||||
input_data = None
|
||||
|
||||
with io.BytesIO(image) as stream:
|
||||
with Image.open(stream) as img:
|
||||
if self._write_file:
|
||||
img.save(self.filepath, "png")
|
||||
else:
|
||||
with io.BytesIO() as out_stream:
|
||||
img.save(out_stream, "png")
|
||||
input_data = out_stream.getvalue()
|
||||
|
||||
with subprocess.Popen(
|
||||
self._command,
|
||||
stdin=subprocess.PIPE if not self._write_file else None,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
close_fds=False, # Required for posix_spawn
|
||||
) as ocr:
|
||||
out = ocr.communicate()
|
||||
out = ocr.communicate(input=input_data)
|
||||
if out[0] != b"":
|
||||
self._attr_state = out[0].strip().decode("utf-8")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user