Spaces:
Running
Running
File size: 852 Bytes
01710b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | """
Image preprocessing pipeline (matches validation / inference transforms).
"""
import cv2
import numpy as np
import torch
import albumentations as A
from albumentations.pytorch import ToTensorV2
from core.config import IMG_SIZE
_transform = A.Compose(
[
A.Resize(IMG_SIZE, IMG_SIZE),
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
ToTensorV2(),
]
)
def preprocess_image(image_bytes: bytes) -> torch.Tensor:
"""Decode uploaded bytes -> preprocessed tensor (1, 3, H, W)."""
arr = np.frombuffer(image_bytes, dtype=np.uint8)
image = cv2.imdecode(arr, cv2.IMREAD_COLOR)
if image is None:
raise ValueError("Could not decode image")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
tensor = _transform(image=image)["image"].unsqueeze(0) # (1,3,H,W)
return tensor
|