chetantiwari/Computer-Vision-Project-Work
1
1from PIL import Image2import numpy as np3from typing import Tuple4 5def preprocess_image(image: Image.Image, target_size: Tuple[int, int] = (512, 512)) -> Image.Image:6 """Preprocess image for model inference"""7 # Convert to RGB if necessary8 if image.mode != 'RGB':9 image = image.convert('RGB')10 11 # Resize while maintaining aspect ratio12 image.thumbnail(target_size, Image.LANCZOS)13 14 # Create a new image with the target size and paste the resized image15 new_image = Image.new('RGB', target_size, (255, 255, 255))16 paste_position = ((target_size[0] - image.width) // 2, 17 (target_size[1] - image.height) // 2)18 new_image.paste(image, paste_position)19 20 return new_image21 22def enhance_image_quality(image: Image.Image) -> Image.Image:23 """Enhance image quality for better analysis"""24 import cv225 26 # Convert to numpy array27 img_array = np.array(image)28 29 # Apply slight gaussian blur to reduce noise30 blurred = cv2.GaussianBlur(img_array, (3, 3), 0)31 32 # Enhance contrast33 enhanced = cv2.addWeighted(img_array, 1.5, blurred, -0.5, 0)34 35 return Image.fromarray(enhanced)36 