import cv2 import numpy as np import os import sys from pathlib import Path # Add project root to path project_root = str(Path(__file__).parent.parent.parent) if project_root not in sys.path: sys.path.append(project_root) from src.utils.preprocess import GazePreprocessor def debug_filters(img_path, output_dir='data/verification/denoise_test'): os.makedirs(output_dir, exist_ok=True) # Load original image frame = cv2.imread(img_path) if frame is None: print(f"Error: Could not load image at {img_path}") return preprocessor = GazePreprocessor() landmarks = preprocessor.get_landmarks(frame) if landmarks is None: print("No landmarks detected.") return # 1. Test Raw Normalization (No additional filters - simulate old state) # We bypass the current preprocess.py by doing a manual warp for comparison def raw_normalize(eye_side='left'): h, w, _ = frame.shape indices = preprocessor.LEFT_CORNERS if eye_side == 'left' else preprocessor.RIGHT_CORNERS p1 = np.array([landmarks[indices[0]].x * w, landmarks[indices[0]].y * h]) p2 = np.array([landmarks[indices[1]].x * w, landmarks[indices[1]].y * h]) center = (p1 + p2) / 2 dx, dy = p2 - p1 angle = np.degrees(np.arctan2(dy, dx)) dist = np.linalg.norm(p2 - p1) scale = (64 * 0.7) / (dist + 1e-6) M = cv2.getRotationMatrix2D(tuple(center), angle, scale) M[0, 2] += (64 / 2) - center[0] M[1, 2] += (32 / 2) - center[1] raw = cv2.warpAffine(frame, M, (64, 32), flags=cv2.INTER_LINEAR) return cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY) # 2. Get Processed result (Current code with Bilateral + CLAHE) processed_eye, _ = preprocessor.normalize_eye(frame, landmarks, 'left') # Save for comparison raw_eye = raw_normalize('left') # Magnify for inspection (16x16 zoom) raw_zoom = cv2.resize(raw_eye, (256, 128), interpolation=cv2.INTER_NEAREST) proc_zoom = cv2.resize(processed_eye, (256, 128), interpolation=cv2.INTER_NEAREST) cv2.imwrite(os.path.join(output_dir, '0_raw_eye.png'), raw_eye) cv2.imwrite(os.path.join(output_dir, '1_processed_eye.png'), processed_eye) cv2.imwrite(os.path.join(output_dir, '2_raw_zoom.png'), raw_zoom) cv2.imwrite(os.path.join(output_dir, '3_processed_zoom.png'), proc_zoom) print(f"Denoising debug images saved to {output_dir}") if __name__ == "__main__": # Sử dụng ảnh test Gaze360 có sẵn test_img = 'data/verification/test_gaze.jpg' if os.path.exists(test_img): debug_filters(test_img) else: print(f"Please provide a valid test image at {test_img}")