| from PIL import Image |
| import torch |
| from typing import IO |
| from model_loader import models |
|
|
| class ImagePreprocessor: |
|
|
| def __init__(self): |
| self.preprocess = models.clip_preprocess |
| self.device = models.device |
|
|
| def process(self, image_file: IO) -> torch.Tensor: |
| """ |
| Opens an image file, preprocesses it, and returns it as a tensor. |
| |
| Args: |
| image_file (IO): The image file object (e.g., from a file upload). |
| |
| Returns: |
| torch.Tensor: The preprocessed image as a tensor, ready for the model. |
| """ |
| try: |
| |
| image = Image.open(image_file).convert("RGB") |
| except Exception as e: |
| print(f"Error opening image: {e}") |
| |
| raise ValueError("Invalid or corrupted image file.") |
|
|
| |
| image_tensor = self.preprocess(image).unsqueeze(0).to(self.device) |
| return image_tensor |
| |
| preprocessor = ImagePreprocessor() |
|
|