Spaces:
Build error
Build error
| # file: app.py | |
| import cv2 | |
| import streamlit as st | |
| import mediapipe as mp | |
| st.title("👀 Screen Distance Detector – NeuroNudge Mini") | |
| st.markdown("**Make sure your webcam is on and you’re sitting in front of your laptop.**") | |
| run = st.checkbox('Start Camera') | |
| mp_face_mesh = mp.solutions.face_mesh | |
| face_mesh = mp_face_mesh.FaceMesh() | |
| def estimate_distance(landmarks, frame_width): | |
| left_eye = landmarks[33] | |
| right_eye = landmarks[263] | |
| pixel_dist = abs(right_eye.x - left_eye.x) * frame_width | |
| return pixel_dist | |
| FRAME_WINDOW = st.image([]) | |
| if run: | |
| cap = cv2.VideoCapture(0) | |
| while cap.isOpened(): | |
| success, image = cap.read() | |
| if not success: | |
| break | |
| image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| results = face_mesh.process(image_rgb) | |
| warning = "" | |
| if results.multi_face_landmarks: | |
| for face_landmarks in results.multi_face_landmarks: | |
| dist = estimate_distance(face_landmarks.landmark, image.shape[1]) | |
| # Debug: Show pixel distance | |
| cv2.putText(image, f"Face width: {int(dist)} px", (30, 30), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2) | |
| if dist > 220: # You can tune this threshold | |
| warning = "🚨 You're too close to the screen!" | |
| cv2.putText(image, warning, (30, 60), | |
| cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) | |
| image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| FRAME_WINDOW.image(image) | |
| cap.release() | |