evaluation: resize segmentation input with OpenCV to reduce difference with Android results

This commit is contained in:
Pierre-Yves Nicolas
2026-02-16 20:44:35 +01:00
parent a2ae0440b6
commit 75b677ebbc
2 changed files with 17 additions and 11 deletions

View File

@@ -2,7 +2,7 @@
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
from PIL import Image, ImageOps import cv2
from pathlib import Path from pathlib import Path
SEG_MODEL_FILE_PATH = "../../app/build/downloads/fairscan-segmentation-model.tflite" SEG_MODEL_FILE_PATH = "../../app/build/downloads/fairscan-segmentation-model.tflite"
@@ -12,15 +12,21 @@ INPUT_WIDTH = 256
INPUT_HEIGHT = 256 INPUT_HEIGHT = 256
def get_resized_image(image_path): def get_resized_image(image_path):
img = Image.open(image_path).convert("RGB") img = cv2.imread(str(image_path), cv2.IMREAD_COLOR)
img = ImageOps.exif_transpose(img) if img is None:
img = img.convert("RGB").resize((INPUT_WIDTH, INPUT_HEIGHT), Image.BILINEAR) raise ValueError(f"Failed to load image: {image_path}")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(
img,
(INPUT_WIDTH, INPUT_HEIGHT),
interpolation=cv2.INTER_LINEAR
)
return img return img
def preprocess_image(img: Image.Image) -> np.ndarray: def preprocess_image(img):
img_np = np.asarray(img).astype(np.float32) img = img.astype(np.float32)
img_np = (img_np - 127.5) / 127.5 # Normalize to [-1, 1] img = img / 127.5 - 1.0
return np.expand_dims(img_np, axis=0) return img[np.newaxis, ...]
def postprocess_output(output: np.ndarray) -> np.ndarray: def postprocess_output(output: np.ndarray) -> np.ndarray:
output = np.squeeze(output).astype(np.float32) # Shape: (256, 256) output = np.squeeze(output).astype(np.float32) # Shape: (256, 256)
@@ -45,7 +51,7 @@ mask_input_dir = DATASET_DIR / "masks"
for image_path in sorted(img_input_dir.glob("*.jpg")): for image_path in sorted(img_input_dir.glob("*.jpg")):
print(f"Generating mask for {image_path}") print(f"Generating mask for {image_path}")
img = get_resized_image(image_path) img = get_resized_image(image_path)
img = ImageOps.exif_transpose(img)
mask = get_segmentation_mask(img) mask = get_segmentation_mask(img)
mask_path = mask_input_dir / (image_path.stem + ".png") mask_path = mask_input_dir / (image_path.stem + ".png")
Image.fromarray((mask * 255).astype(np.uint8)).save(mask_path) mask_uint8 = (mask * 255.0).round().astype(np.uint8)
cv2.imwrite(str(mask_path), mask_uint8)

View File

@@ -1,3 +1,3 @@
tensorflow tensorflow
numpy numpy
Pillow opencv-python