| """
|
| Image preprocessing utilities for fingerprint images
|
| """
|
|
|
| import cv2
|
| import numpy as np
|
| from PIL import Image
|
|
|
|
|
| def preprocess_fingerprint(image: Image.Image, model_type: str = "gender", target_size: tuple = None) -> np.ndarray:
|
| """
|
| Preprocess fingerprint image for model prediction
|
|
|
| Args:
|
| image: PIL Image object
|
| model_type: "blood_group" or "gender" - determines preprocessing
|
| target_size: Override target size
|
|
|
| Returns:
|
| Preprocessed numpy array with correct shape for the model
|
| """
|
|
|
| if model_type == "blood_group":
|
| target_size = target_size or (150, 150)
|
| output_channels = 3
|
| else:
|
| target_size = target_size or (224, 224)
|
| output_channels = 1
|
|
|
|
|
| img_array = np.array(image)
|
|
|
|
|
| if len(img_array.shape) == 3:
|
| img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
| img_resized = cv2.resize(img_array, target_size, interpolation=cv2.INTER_AREA)
|
|
|
|
|
| img_normalized = img_resized.astype('float32') / 255.0
|
|
|
|
|
| img_normalized = cv2.equalizeHist((img_normalized * 255).astype(np.uint8)).astype(np.float32) / 255.0
|
|
|
|
|
| if output_channels == 3:
|
|
|
| img_output = np.stack([img_normalized, img_normalized, img_normalized], axis=-1)
|
| else:
|
|
|
| img_output = np.expand_dims(img_normalized, axis=-1)
|
|
|
| print(f"DEBUG preprocessing ({model_type}): Output shape = {img_output.shape}, min/max = {img_output.min()}/{img_output.max()}")
|
|
|
| return img_output
|
|
|
|
|
| def enhance_fingerprint(image: np.ndarray) -> np.ndarray:
|
| """
|
| Enhance fingerprint features using image processing techniques
|
|
|
| Args:
|
| image: Fingerprint image as numpy array
|
|
|
| Returns:
|
| Enhanced image array
|
| """
|
|
|
| if len(image.shape) == 3:
|
| image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
| kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
| enhanced = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
|
| enhanced = cv2.morphologyEx(enhanced, cv2.MORPH_OPEN, kernel)
|
|
|
|
|
| enhanced = cv2.medianBlur(enhanced, 5)
|
|
|
| return enhanced
|
|
|
|
|
| def normalize_fingerprint(image: np.ndarray) -> np.ndarray:
|
| """
|
| Normalize fingerprint image using standard normalization
|
|
|
| Args:
|
| image: Fingerprint image as numpy array
|
|
|
| Returns:
|
| Normalized image array
|
| """
|
| if len(image.shape) == 3 and image.shape[-1] == 3:
|
|
|
| mean = np.array([0.485, 0.456, 0.406])
|
| std = np.array([0.229, 0.224, 0.225])
|
| return (image - mean) / std
|
| else:
|
|
|
| mean = 0.5
|
| std = 0.5
|
| return (image - mean) / std
|
|
|