File size: 1,486 Bytes
f4586bd cfb83e6 f4586bd cfb83e6 f4586bd | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | """
Downscale preprocessor - resizes image to smaller resolution for faster processing.
"""
import cv2
import numpy as np
from core.base_processor import PreprocessingModule
from core import DataObject
class DownscaleWithInterpolationPreprocessor(PreprocessingModule):
"""
Downscales images to a maximum dimension while preserving aspect ratio.
Args:
max_size: Maximum width or height in pixels (default 640)
interpolation: OpenCV interpolation method (default INTER_AREA for downscaling)
"""
def __init__(self, max_size: int = 640, interpolation: int = cv2.INTER_AREA):
super().__init__(name=f"Downscale with interpolation({max_size})")
self.max_size = max_size
self.interpolation = interpolation
def _process(self, input: DataObject) -> DataObject:
image = input.data
if len(image.shape) == 2:
h, w = image.shape
else:
h, w = image.shape[:2]
# Check if downscaling is needed
if max(h, w) <= self.max_size:
return DataObject(image.copy())
# Calculate new dimensions preserving aspect ratio
if w > h:
new_w = self.max_size
new_h = int(h * (self.max_size / w))
else:
new_h = self.max_size
new_w = int(w * (self.max_size / h))
# Resize
resized = cv2.resize(image, (new_w, new_h), interpolation=self.interpolation)
return DataObject(resized)
|