Download src/preprocessing/downscale_int.py from slityak/kpo: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/slityak/kpo/resolve/main/src/preprocessing/downscale_int.py
- Command line
-
hf download hf://spaces/slityak/kpo/src/preprocessing/downscale_int.py
-
curl -L -o downscale_int.py https://huggingface.co/spaces/slityak/kpo/resolve/main/src/preprocessing/downscale_int.py
1.49 kB
| """ | |
| 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) | |