import os import numpy as np import argparse from closed_eyes_detection import run_on_webcam, ClosedEyesDetector from blurriness_estimation import BlurrinessEstimator from config import BlurrinessConfig, InferenceConfig import cv2 def parse_arguments() -> argparse.ArgumentParser: """ Parse the cli arguments given by the user. Returns: parser: parser object containing arguments and their values """ parser = argparse.ArgumentParser() parser.add_argument("-w", "--webcam", required=False, action="store_true", default=False, help="Runs eye blink detection on the user's webcam. Overrides other options.") parser.add_argument("-p", "--path", required=False, type=str, help="path of image to load") parser.add_argument("-e", "--eyes", required=False, action="store_true", default=False, help="Detects eye blink on the input image") parser.add_argument("-b", "--blur", required=False, action="store_true", default=False, help="Estimates image blur") return parser def detect_closed_eyes(img: np.ndarray) -> str: """ Runs the Closed Eyes Detector Args: img: input image Returns: str: Whether the eyes are opened or closed. """ detector = ClosedEyesDetector(InferenceConfig()) is_closed = detector(img) s = "Eyes are closed." if is_closed else "Eyes are open." return s def detect_blur(img: np.ndarray) -> str: """ Runs the Blurriness Detector Args: img: input image Returns: str: Whether the input image is blurry """ cfg = BlurrinessConfig() be = BlurrinessEstimator(cfg) is_blurry, score = be(img) s = "Image is blurry" if is_blurry else "Image is not blurry" s += f" - Score: {score:.0f} - Sharpness threshold: {cfg.threshold}" return s def main(): parser = parse_arguments() args = vars(parser.parse_args()) if args['webcam']: run_on_webcam() return if args['path'] is None: print('Image path required (-p).') parser.print_help() else: path = args['path'] if not os.path.exists(path): print(f"File not found at {path}") return img = cv2.imread(path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if args['eyes']: result = detect_closed_eyes(img) elif args['blur']: result = detect_blur(img) else: print('No task specified. Use either -b or -e.') parser.print_help() return print(result) if __name__ == "__main__": main()