A webcam watches for attention, raised hands and nods, and announces each change.
Built and tested by Shahan Fathima K · Industrial Robotics Institute
Python 3.11, OpenCV (opencv-python), MediaPipe, pyttsx3
A computer-vision system that checks a student’s attention in real time using a webcam. MediaPipe tracks face landmarks and hands; the program uses eye position, head nods and raised hands to label the student as Attentive, Distracted, Absent, having a Doubt, or Understanding. A voice alert announces each change so the teacher hears it without watching the screen.
| Component | Qty |
|---|---|
| Laptop / PC | 1 |
| Webcam | 1 |
| Speaker / Headphones | 1 |
Complete, tested Python code for this project. Copy it into Python 3.11 and upload it and run it.
import cv2
import mediapipe as mp
import pyttsx3
import time
import threading
# -------------------- VOICE ENGINE --------------------
engine = pyttsx3.init()
engine.setProperty('rate', 150)
def speak(text):
def run():
engine.say(text)
engine.runAndWait()
threading.Thread(target=run).start()
# -------------------- MEDIAPIPE SETUP --------------------
mp_face = mp.solutions.face_mesh
face_mesh = mp_face.FaceMesh(refine_landmarks=True)
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
# -------------------- CAMERA --------------------
cap = cv2.VideoCapture(0)
# -------------------- VARIABLES --------------------
last_spoken_time = 0
speak_interval = 3 # seconds
prev_status = ""
prev_engagement = ""
nod_counter = 0
prev_nose_y = None
# -------------------- MAIN LOOP --------------------
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_results = face_mesh.process(rgb)
hand_results = hands.process(rgb)
status = "Attentive"
engagement = "Listening"
hand_raised = False
# Hand detection
if hand_results.multi_hand_landmarks:
hand_raised = True
engagement = "Doubt"
# Face detection
if face_results.multi_face_landmarks:
for face_landmarks in face_results.multi_face_landmarks:
lm = face_landmarks.landmark
left_eye = lm[33]
right_eye = lm[263]
nose = lm[1]
eye_diff = abs(left_eye.x - right_eye.x)
face_center = (left_eye.x + right_eye.x) / 2
# Nod detection
if prev_nose_y is not None:
if abs(nose.y - prev_nose_y) > 0.02:
nod_counter += 1
prev_nose_y = nose.y
# Attention check
if eye_diff < 0.12 and 0.25 < face_center < 0.75:
status = "Attentive"
else:
status = "Distracted"
# Engagement check
if nod_counter > 5:
engagement = "Understanding"
nod_counter = 0
elif hand_raised:
engagement = "Doubt"
else:
engagement = "Listening"
else:
status = "Absent"
engagement = "No Face"
# Voice alert system
current_time = time.time()
should_speak = False
if status != prev_status or engagement != prev_engagement:
should_speak = True
elif current_time - last_spoken_time > speak_interval:
should_speak = True
if should_speak:
if status == "Absent":
speech_text = "Student is not present"
elif engagement == "Doubt":
speech_text = "Student has a doubt"
elif status == "Distracted":
speech_text = "Student is distracted"
elif engagement == "Understanding":
speech_text = "Student understands the topic"
else:
speech_text = "Student is attentive"
speak(speech_text)
last_spoken_time = current_time
prev_status = status
prev_engagement = engagement
# Display
cv2.putText(frame, f"Status: {status}", (20, 40),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f"Engagement: {engagement}", (20, 80),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
cv2.imshow("Smart Attention AI", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()A computer-vision system that checks a student’s attention in real time using a webcam. MediaPipe tracks face landmarks and hands; the program uses eye position, head nods and raised hands to label the student as Attentive, Distracted, Absent, having a Doubt, or Understanding.
You need: Laptop / PC (1), Webcam (1), Speaker / Headphones (1). Software: Python 3.11, OpenCV (opencv-python), MediaPipe, pyttsx3.
It is rated intermediate and takes about 2–3 hours to build, using the Laptop / PC with webcam. The guide on this page explains every connection and step.
The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.
No kit is needed. This project runs on a laptop or PC with webcam. For help with the setup, message IRI on WhatsApp at +91 86060 82634.