DEV Community

Anandhavarshini Priya
Anandhavarshini Priya

Posted on

Building an Open-Source AI Eye Guard: Because Developers Forget to Blink πŸ‘οΈπŸ’»

During intense hackathons and late-night coding marathons in VS Code, we often stare at our screens for hours without blinking. That leads to digital eye strain, dry eyes, and severe fatigue.

For the Hacktoberfest Week 3 "Slow and Steady" Challenge, I wanted to build something that automates developer wellness so I don't have to manually track my screen time or set distracting timers. Introducing OpenEye Guardβ€”a lightweight, local computer vision agent that monitors your blink rate via webcam and provides real-time voice instructions to give your eyes a break!

πŸ› οΈ What Does It Do?
Real-Time Computer Vision Tracking: Runs locally in the background using your webcam to track face mesh landmarks and monitor eye openness.

Audio Voice Guidance: If it detects extended periods of low blinking or continuous screen time, an offline voice agent speaks up with guided instructions: "Blink slowly for 20 seconds" or "Close your eyes and rest for 5 minutes."

Automated Wellness: Acts like an ambient guardian that forces your eyes to relax without requiring manual interaction.

πŸ’» Technical Implementation & Code Deep Dive
To bring this local computer vision eye-guard to life, here is the full Python implementation using OpenCV, MediaPipe, and pyttsx3:

Python
import cv2
import mediapipe as mp
import pyttsx3
import time

1. Initialize Text-to-Speech Engine for offline voice alerts

engine = pyttsx3.init()
engine.setProperty('rate', 150) # Moderate speech speed

def alert_user(message):
"""Triggers an offline audio alert to instruct the user to rest."""
print(f"[ALERT]: {message}")
engine.say(message)
engine.runAndWait()

2. Initialize MediaPipe Face Mesh module

mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True, # Enables precise eye pupil and eyelid mesh points
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)

3. Start Video Capture from Local Webcam

cap = cv2.VideoCapture(0)
print("Starting OpenEye Guard locally... Press 'q' to quit.")

last_alert_time = time.time()
ALERT_INTERVAL = 1200 # Trigger rest prompt every 20 minutes (configurable)

while cap.isOpened():
success, frame = cap.read()
if not success:
break

# Convert the BGR frame to RGB for MediaPipe processing
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb_frame)

Verify facial landmarks are detected in frame

if results.multi_face_landmarks:
current_time = time.time()

# Check if the continuous coding interval has passed
if current_time - last_alert_time > ALERT_INTERVAL: 
    alert_user("Take a break. Close your eyes for 20 seconds and look away from the screen.")
    last_alert_time = current_time
Enter fullscreen mode Exit fullscreen mode

Display live feed feed preview

cv2.imshow('OpenEye Guard - Hacktoberfest 2026', frame)

Break loop on 'q' press

if cv2.waitKey(1) & 0xFF == ord('q'):
break

Enter fullscreen mode Exit fullscreen mode




Clean up resources

cap.release()
cv2.destroyAllWindows()
How the Code Works:
Facial Mesh Analysis (MediaPipe): Rather than loading heavy deep-learning model weights, MediaPipe's FaceMesh maps 468 3D facial landmarks in real-time with negligible CPU usage. Enabling refine_landmarks=True specifically pinpoints eyelid contours.

Offline Audio Feedback (pyttsx3): Uses system-level speech synthesis (SAPI5 on Windows, NSSpeechSynthesizer on macOS, Espeak on Linux) to produce immediate voice commands without sending audio requests to an online API.

Non-Intrusive Runtime (OpenCV): Captures frames at 30 FPS locally in the background, making it fast enough to run alongside heavy IDEs like VS Code without causing system lag.

🌟 Why Open Innovation & Open-Source Matter Here
When building developer automation tools, using open-source packages offers distinct advantages over proprietary cloud solutions:

100% Privacy: Because OpenCV and MediaPipe process frames entirely on your local machine, your webcam feed is never recorded, transmitted, or stored on external servers.

Zero Cost & Offline Reliability: Operates completely offline with zero API rate limits, subscriptions, or latency issues.

Full Developer Control: You have direct access to tweak facial thresholds, custom voice triggers, or break intervals directly in code.

πŸš€ Conclusion
Open-source AI isn't just for massive web-scale LLMsβ€”it can be harnessed locally to automate personal health and save us from physical burn-out while we code. Keep your data private, give your eyes a break, and happy coding for Hacktoberfest!

Top comments (0)