Spaces:
Sleeping
Sleeping
File size: 13,182 Bytes
d0c8d86 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | """
Image Processing Module
Handles image validation, preprocessing, and optimization for caption generation.
Ensures images meet model requirements while maintaining quality.
"""
import io
import hashlib
from pathlib import Path
from typing import Tuple, Union
from PIL import Image, ImageOps
from config import image_config
class ImageProcessingError(Exception):
"""Custom exception for image processing errors"""
pass
class ImageProcessor:
"""
Enterprise-grade image processor for caption generation pipeline
Responsibilities:
- Validate image format and size
- Resize and optimize images
- Generate cache keys
- Handle edge cases and errors gracefully
"""
def __init__(self):
"""Initialize image processor with configuration"""
self.max_size = image_config.MAX_FILE_SIZE_BYTES
self.max_dimension = image_config.MAX_DIMENSION
self.min_dimension = image_config.MIN_DIMENSION
self.allowed_formats = image_config.ALLOWED_FORMATS
self.quality = image_config.RESIZE_QUALITY
def validate_image(self, image: Union[str, Path, Image.Image, bytes]) -> Tuple[bool, str]:
"""
Validate image meets all requirements
Args:
image: Image path, PIL Image, or bytes
Returns:
Tuple[bool, str]: (is_valid, error_message)
"""
try:
# Load image if path or bytes provided
if isinstance(image, (str, Path)):
img = Image.open(image)
elif isinstance(image, bytes):
img = Image.open(io.BytesIO(image))
elif isinstance(image, Image.Image):
img = image
else:
return False, f"Unsupported image type: {type(image)}"
# Check format (handle None format from Gradio)
# When Gradio passes PIL images with type="pil", format can be None
if hasattr(img, 'format') and img.format is not None:
if img.format.upper() not in [fmt.upper() for fmt in self.allowed_formats]:
return False, f"Unsupported format: {img.format}. Allowed: {', '.join(self.allowed_formats)}"
else:
# Format is None - likely from Gradio's PIL conversion
# We'll validate by checking if it's a valid PIL image
print(f"DEBUG: Image format is None (from Gradio), skipping format check")
# Check dimensions
width, height = img.size
if width < self.min_dimension or height < self.min_dimension:
return False, f"Image too small. Minimum: {self.min_dimension}x{self.min_dimension}px"
if width > 10000 or height > 10000:
return False, "Image dimensions too large (max: 10000x10000px)"
# Check file size (if path provided)
if isinstance(image, (str, Path)):
file_size = Path(image).stat().st_size
if file_size > self.max_size:
max_mb = self.max_size / (1024 * 1024)
actual_mb = file_size / (1024 * 1024)
return False, f"File too large: {actual_mb:.1f}MB (max: {max_mb}MB)"
# Try to verify image integrity (skip if format is None)
if hasattr(img, 'format') and img.format is not None:
# Create a copy before verify (verify closes the file)
img_copy = img.copy()
img_copy.verify()
return True, ""
except Exception as e:
return False, f"Image validation failed: {str(e)}"
def preprocess_image(
self,
image: Union[str, Path, Image.Image, bytes]
) -> Tuple[Image.Image, dict]:
"""
Preprocess image for model input
Args:
image: Image path, PIL Image, or bytes
Returns:
Tuple[Image.Image, dict]: (processed_image, metadata)
Raises:
ImageProcessingError: If preprocessing fails
"""
try:
print(f"DEBUG: Preprocessing image of type: {type(image)}")
# Validate first
is_valid, error_msg = self.validate_image(image)
if not is_valid:
print(f"DEBUG: Validation failed: {error_msg}")
raise ImageProcessingError(error_msg)
# Load image
if isinstance(image, (str, Path)):
img = Image.open(image)
elif isinstance(image, bytes):
img = Image.open(io.BytesIO(image))
elif isinstance(image, Image.Image):
img = image.copy() # Don't modify original
else:
raise ImageProcessingError(f"Unsupported image type: {type(image)}")
# Store original metadata
original_size = img.size
original_format = img.format if hasattr(img, 'format') else 'Unknown'
original_mode = img.mode
print(f"DEBUG: Original format: {original_format}, mode: {original_mode}, size: {original_size}")
# Convert to RGB if needed (handles RGBA, grayscale, etc.)
if img.mode != "RGB":
if img.mode == "RGBA":
# Create white background for transparent images
background = Image.new("RGB", img.size, (255, 255, 255))
background.paste(img, mask=img.split()[-1]) # Use alpha channel as mask
img = background
else:
img = img.convert("RGB")
# Auto-orient based on EXIF data
img = ImageOps.exif_transpose(img)
# Resize if needed
if max(img.size) > self.max_dimension:
img = self._resize_image(img)
# Generate metadata
metadata = {
"original_size": original_size,
"original_format": original_format,
"original_mode": original_mode,
"processed_size": img.size,
"processed_mode": img.mode,
"was_resized": original_size != img.size,
"was_converted": original_mode != img.mode
}
print(f"DEBUG: Preprocessing complete. Final size: {img.size}, mode: {img.mode}")
return img, metadata
except ImageProcessingError:
raise
except Exception as e:
print(f"DEBUG: Exception during preprocessing: {str(e)}")
raise ImageProcessingError(f"Preprocessing failed: {str(e)}")
def _resize_image(self, img: Image.Image) -> Image.Image:
"""
Resize image maintaining aspect ratio
Args:
img: PIL Image
Returns:
Image.Image: Resized image
"""
width, height = img.size
if image_config.MAINTAIN_ASPECT_RATIO:
# Calculate new dimensions maintaining aspect ratio
if width > height:
new_width = self.max_dimension
new_height = int((height / width) * self.max_dimension)
else:
new_height = self.max_dimension
new_width = int((width / height) * self.max_dimension)
else:
new_width = self.max_dimension
new_height = self.max_dimension
# Use high-quality resampling
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
return img
def generate_image_hash(
self,
image: Union[str, Path, Image.Image, bytes],
algorithm: str = "md5"
) -> str:
"""
Generate unique hash for image (for caching)
Args:
image: Image path, PIL Image, or bytes
algorithm: Hash algorithm (md5, sha256)
Returns:
str: Hexadecimal hash string
"""
try:
# Convert to bytes
if isinstance(image, (str, Path)):
with open(image, "rb") as f:
image_bytes = f.read()
elif isinstance(image, bytes):
image_bytes = image
elif isinstance(image, Image.Image):
buffer = io.BytesIO()
image.save(buffer, format="PNG")
image_bytes = buffer.getvalue()
else:
raise ValueError(f"Unsupported type for hashing: {type(image)}")
# Generate hash
if algorithm == "md5":
return hashlib.md5(image_bytes).hexdigest()
elif algorithm == "sha256":
return hashlib.sha256(image_bytes).hexdigest()
else:
raise ValueError(f"Unsupported hash algorithm: {algorithm}")
except Exception as e:
raise ImageProcessingError(f"Hash generation failed: {str(e)}")
def image_to_bytes(self, img: Image.Image, format: str = "PNG") -> bytes:
"""
Convert PIL Image to bytes
Args:
img: PIL Image
format: Output format (PNG, JPEG)
Returns:
bytes: Image bytes
"""
buffer = io.BytesIO()
img.save(buffer, format=format, quality=self.quality)
return buffer.getvalue()
def get_image_info(self, image: Union[str, Path, Image.Image]) -> dict:
"""
Get detailed image information
Args:
image: Image path or PIL Image
Returns:
dict: Image information
"""
try:
if isinstance(image, (str, Path)):
img = Image.open(image)
file_size = Path(image).stat().st_size
elif isinstance(image, Image.Image):
img = image
file_size = len(self.image_to_bytes(img))
else:
raise ValueError(f"Unsupported type: {type(image)}")
return {
"format": img.format,
"mode": img.mode,
"size": img.size,
"width": img.size[0],
"height": img.size[1],
"file_size": file_size,
"file_size_mb": file_size / (1024 * 1024),
"aspect_ratio": img.size[0] / img.size[1],
"megapixels": (img.size[0] * img.size[1]) / 1_000_000
}
except Exception as e:
raise ImageProcessingError(f"Failed to get image info: {str(e)}")
# ============================================================================
# SINGLETON INSTANCE AND CONVENIENCE FUNCTIONS
# ============================================================================
_image_processor = None
def get_image_processor() -> ImageProcessor:
"""Get singleton ImageProcessor instance"""
global _image_processor
if _image_processor is None:
_image_processor = ImageProcessor()
return _image_processor
# Convenience wrapper functions for backward compatibility
def validate_image(image: Union[str, Path, Image.Image, bytes]) -> Tuple[bool, str]:
"""
Convenience function: Validate image using singleton processor
Args:
image: Image path, PIL Image, or bytes
Returns:
Tuple[bool, str]: (is_valid, error_message)
"""
return get_image_processor().validate_image(image)
def preprocess_image(
image: Union[str, Path, Image.Image, bytes]
) -> Tuple[Image.Image, dict]:
"""
Convenience function: Preprocess image using singleton processor
Args:
image: Image path, PIL Image, or bytes
Returns:
Tuple[Image.Image, dict]: (processed_image, metadata)
"""
return get_image_processor().preprocess_image(image)
def generate_image_hash(
image: Union[str, Path, Image.Image, bytes],
algorithm: str = "md5"
) -> str:
"""
Convenience function: Generate image hash using singleton processor
Args:
image: Image path, PIL Image, or bytes
algorithm: Hash algorithm (md5, sha256)
Returns:
str: Hexadecimal hash string
"""
return get_image_processor().generate_image_hash(image, algorithm)
if __name__ == "__main__":
# Test the image processor
print("=" * 60)
print("IMAGE PROCESSOR - TEST MODE")
print("=" * 60)
processor = get_image_processor()
print(f"✓ ImageProcessor initialized")
print(f" - Max file size: {processor.max_size / (1024*1024):.1f}MB")
print(f" - Max dimension: {processor.max_dimension}px")
print(f" - Allowed formats: {', '.join(processor.allowed_formats)}")
print(f" - Quality: {processor.quality}")
print("=" * 60)
print("Ready for testing with actual images")
print("=" * 60) |