File size: 3,051 Bytes
36eb7b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import cv2
import numpy as np
import os
import pandas as pd
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk

atnd = []

# Train the face recognition model using the collected dataset
def train_model():
    data_path = 'data'
    face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    training_data = []
    labels = []

    for root, dirs, files in os.walk(data_path):
        for file in files:
            if file.endswith('jpg'):
                path = os.path.join(root, file)
                label = int(path.split('.')[1])
                image = cv2.imread(path)
                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                face = face_classifier.detectMultiScale(gray, 1.3, 5)
                if face is not ():
                    for (x, y, w, h) in face:
                        cropped_face = gray[y:y + h, x:x + w]
                        training_data.append(cropped_face)
                        labels.append(label)

    labels = np.array(labels)
    model = cv2.face.LBPHFaceRecognizer_create()
    model.train(training_data, labels)

    return model

# Implement the student attention monitoring system
def monitor_attention():
    global atnd
    atnd = []
    face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    eye_classifier = cv2.CascadeClassifier("haarcascade_eye.xml")
    model = train_model()
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_classifier.detectMultiScale(gray, 1.3, 5)
        if faces is not ():
            for (x, y, w, h) in faces:
                cropped_face = gray[y:y + h, x:x + w]
                label, confidence = model.predict(cropped_face)
                if confidence < 100:
                    eyes = eye_classifier.detectMultiScale(cropped_face)
                    if eyes is not ():
                        for (ex, ey, ew, eh) in eyes:
                            atnd.append(1)
                    else:
                        atnd.append(0)
        cv2.imshow('Student Attention', frame)
        if cv2.waitKey(1) == 13:
            break

    cap.release()
    cv2.destroyAllWindows()
    save_attendance()

def save_attendance():
    df = pd.DataFrame(atnd, columns=['Attention'])
    df.to_csv('./attention.csv')
    messagebox.showinfo("Attendance Saved", "Attendance data has been saved successfully.")

def start_monitoring():
    monitor_attention()

def stop_monitoring():
    messagebox.showinfo("Monitoring Stopped", "Monitoring has been stopped.")

def main():
    root = tk.Tk()
    root.title("Student Attentiveness")

    video_frame = tk.Label(root)
    video_frame.pack()

    start_button = tk.Button(root, text="Start Monitoring", command=start_monitoring)
    start_button.pack()

    stop_button = tk.Button(root, text="Stop Monitoring", command=stop_monitoring)
    stop_button.pack()

    root.mainloop()

if __name__ == "__main__":
    main()