AI & MLIntermediatePRJ-096

AI Smart Classroom Attention Monitor

A webcam watches for attention, raised hands and nods, and announces each change.

Built and tested by Shahan Fathima K · Industrial Robotics Institute

Laptop / PC with webcam

Python 3.11, OpenCV (opencv-python), MediaPipe, pyttsx3

LevelIntermediate
Build time2–3 hours
BoardLaptop / PC with webcam
Parts3 components

What is the AI Smart Classroom Attention Monitor?

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.

How it works

  1. The webcam captures live video frames.
  2. MediaPipe Face Mesh finds the eyes and nose; MediaPipe Hands looks for a raised hand.
  3. If the eyes are level and centred the student is Attentive, otherwise Distracted; no face means Absent.
  4. Repeated up-and-down nose movement counts as nodding (Understanding); a raised hand means a Doubt.
  5. When the status changes, pyttsx3 speaks an alert such as ‘Student has a doubt’ in the background.
  6. The current status and engagement are drawn on the video window. Press q to quit.

Parts list

ComponentQty
Laptop / PC1
Webcam1
Speaker / Headphones1

Software

  • Python 3.11
  • OpenCV (opencv-python)
  • MediaPipe
  • pyttsx3

How to build the AI Smart Classroom Attention Monitor

  1. Install Python 3.11 and tick ‘Add Python to PATH’.
  2. Open a terminal and run: pip install opencv-python mediapipe pyttsx3
  3. Paste the code into a new Python file.
  4. Make sure the webcam is connected and not in use by another app.
  5. Run the file; a video window titled ‘Smart Attention AI’ opens.
  6. Look at the camera, look away, raise a hand and nod to see the status change and hear the alerts.
  7. Press q in the video window to close it.

AI Smart Classroom Attention Monitor code

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()

Real-world uses

  • Classrooms and tuition centres
  • Online class monitoring
  • Teacher feedback tools
  • Learning computer vision

Frequently asked questions

What is a AI Smart Classroom Attention Monitor?

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.

What components are needed for the AI Smart Classroom Attention Monitor?

You need: Laptop / PC (1), Webcam (1), Speaker / Headphones (1). Software: Python 3.11, OpenCV (opencv-python), MediaPipe, pyttsx3.

Is the AI Smart Classroom Attention Monitor project good for beginners?

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.

Where can I get the code for the AI Smart Classroom Attention Monitor?

The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.

Do I need any hardware for the AI Smart Classroom Attention Monitor?

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.

Industrial Robotics Institute
Logo