File size: 1,560 Bytes
2ae995f
 
668a6a4
 
2ae995f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
668a6a4
 
2ae995f
 
8f0fd46
2ae995f
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()