import cv2 import mediapipe as mp import numpy as np import gradio as gr mp_face_mesh = mp.solutions.face_mesh face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True) # Eye landmark indices LEFT_EYE = [33, 133] RIGHT_EYE = [362, 263] def calculate_focus_state(left_eye, right_eye): left_x, _ = left_eye right_x, _ = right_eye if 0.3 < left_x < 0.7 and 0.3 < right_x < 0.7: return "🟢 Focused" elif left_x < 0.2 or right_x > 0.8: return "🟡 Distracted" else: return "🔴 Drowsy" def detect_productivity(image): image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_mesh.process(image_rgb) h, w, _ = image.shape if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: landmarks = face_landmarks.landmark left_eye = (landmarks[LEFT_EYE[0]].x, landmarks[LEFT_EYE[0]].y) right_eye = (landmarks[RIGHT_EYE[0]].x, landmarks[RIGHT_EYE[0]].y) state = calculate_focus_state(left_eye, right_eye) return state return "Face not detected. Please try again." # Gradio UI interface = gr.Interface( fn=detect_productivity, inputs=gr.Image(type="numpy", label="Upload a Snapshot"), outputs=gr.Textbox(label="Productivity Status"), title="👁️ VisionPing – Live Productivity Monitor", description="Take a snapshot. VisionPing will analyze your eye direction and predict your current productivity state." ) interface.launch()