0% found this document useful (0 votes)
1 views2 pages

Implementation Code

The document outlines an implementation code for a real-time attention prediction system using Streamlit and machine learning. It includes steps for importing libraries, loading a model, defining a prediction function, extracting features from data, and displaying the attention status. The system predicts user attention based on various features and provides live feedback on attentiveness.

Uploaded by

kajalkupale0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Implementation Code

The document outlines an implementation code for a real-time attention prediction system using Streamlit and machine learning. It includes steps for importing libraries, loading a model, defining a prediction function, extracting features from data, and displaying the attention status. The system predicts user attention based on various features and provides live feedback on attentiveness.

Uploaded by

kajalkupale0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Implementation Code

1. Importing Libraries
import streamlit as st
import cv2, time, joblib
import pandas as pd
import numpy as np
from pathlib import Path
from collections import deque

2. Model Loading and Setup


MODEL_PATH = Path("outputs/attention_model.pkl")
model = [Link](MODEL_PATH)

FEATURE_ORDER = [
"face_ratio", "eye_open_ratio", "head_center_ratio",
"mouse_activity", "mouse_movement_intensity",
"mouse_click_rate", "gaze_focus_score",
"attention_continuity"
]

ATTENTION_HISTORY = deque(maxlen=10)

if "attention_continuity" not in st.session_state:


st.session_state.attention_continuity = 0

3. Attention Prediction Function


def predict_attention(features):
features["attention_continuity"] = 1
X = [Link]([features], columns=FEATURE_ORDER)

probs = model.predict_proba(X)[0]
frame_pred = int([Link](probs))

# Rule-based correction
if features["face_ratio"] > 0.6 and features["gaze_focus_score"] > 0.5:
frame_pred = 1

ATTENTION_HISTORY.append(frame_pred)

attention_score = sum(ATTENTION_HISTORY) / len(ATTENTION_HISTORY)


final_pred = 1 if attention_score >= 0.7 else 0

return final_pred, round(attention_score, 2)

4. Feature Extraction from Data


DATA_FILE = Path("logs/attention_data.csv")

if DATA_FILE.exists():
df = pd.read_csv(DATA_FILE)

if len(df) >= 5:
window = [Link](5)

features = {
"face_ratio": window["face_detected"].mean(),
"eye_open_ratio": (window["eye_gaze"] != "not_detected").mean(),
"head_center_ratio": (window["eye_gaze"] == "center").mean(),
"mouse_activity": int(window["mouse_clicks"].sum() > 0),
"mouse_movement_intensity":
window["mouse_x"].diff().abs().sum() +
window["mouse_y"].diff().abs().sum(),
"mouse_click_rate": window["mouse_clicks"].iloc[-1] / 5,
"gaze_focus_score": (window["eye_gaze"] == "center").mean(),
"attention_continuity": st.session_state.attention_continuity
}

5. Real-Time Prediction Output


prediction, confidence = predict_attention(features)

st.session_state.attention_continuity = prediction
timestamp = [Link]("%H:%M:%S")

[Link]("Live Attention Status")


if prediction == 1:
[Link](f"ATTENTIVE ({confidence}) at {timestamp}")
else:
[Link](f"NOT ATTENTIVE ({confidence}) at {timestamp}")

You might also like