From 3b0e06587aa462ececcc69711c1177b9085e4c7f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 8 Jul 2024 23:21:16 +0300 Subject: [PATCH] scripts/libraries: Fix Normalization image format. --- scripts/libraries/ml/ml/preprocessing.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/scripts/libraries/ml/ml/preprocessing.py b/scripts/libraries/ml/ml/preprocessing.py index 7cd37fb3b..4619b7844 100644 --- a/scripts/libraries/ml/ml/preprocessing.py +++ b/scripts/libraries/ml/ml/preprocessing.py @@ -11,17 +11,16 @@ import image class Normalization: def __init__( self, - image=None, scale=(0.0, 1.0), mean=(0.0, 0.0, 0.0), stdev=(1.0, 1.0, 1.0), roi=None, ): - self.image = image self.scale = scale self.mean = mean self.stdev = stdev self.roi = roi + self._image = None def __call__(self, *args): if len(args) == 1: @@ -30,12 +29,18 @@ class Normalization: raise ValueError("Expected an image input") if self.roi is None: self.roi = (0, 0, img.width(), img.height()) - return Normalization(img, self.scale, self.mean, self.stdev, self.roi) + n = Normalization(self.scale, self.mean, self.stdev, self.roi) + n._image = img + return n + buffer, shape, dtype = args # Create an image using the input tensor as buffer. - img = image.Image(shape[2], shape[1], self.image.format(), buffer=buffer) + pixfmt = image.GRAYSCALE if shape[3] == 1 else image.RGB565 + img = image.Image(shape[2], shape[1], pixfmt, buffer=buffer) + # Copy and scale (if needed) the input image to the input buffer. hints = image.BILINEAR | image.CENTER | image.SCALE_ASPECT_EXPAND | image.BLACK_BACKGROUND - img.draw_image(self.image, 0, 0, roi=self.roi, hint=hints) + img.draw_image(self._image, 0, 0, roi=self.roi, hint=hints) + # Scale and convert the image to input tensor data. img.unpack(buffer, dtype, scale=self.scale, mean=self.mean, stdev=self.stdev)