Skip to content

kunoong/brainbuddy_AI

Repository files navigation

🧠 BrainBuddy β€” Real-time Human Focus Analytics System

K-SoftVation Showcase 2025 β€” Grand Prize (Minister of Science and ICT Award) Real-time human focus analytics system with optimized AI inference serving and WebSocket-based streaming architecture.


πŸ† Award

Item Detail
Competition K-Software Empowerment BootCamp β€” K-SoftVation Showcase 2025
Prize Grand Prize (μ •λ³΄ν†΅μ‹ κΈ°νšν‰κ°€μ›μž₯상)
Team Queen Never Cry
Date August 28, 2025

πŸ“Œ Overview

BrainBuddy is a real-time human focus analytics system that estimates user engagement levels from webcam streams.

The project was designed to address the gap between high offline model performance and unstable real-world webcam inference environments.

The system combines:

  • Computer Vision
  • Real-time inference serving
  • ONNX optimization
  • WebSocket streaming
  • Data-centric AI pipeline design

πŸš€ Key Highlights

Metric Result
AUC 0.997
F1-Score 0.963
Accuracy 0.9505
Recall 0.9778
CPU Latency ~890ms
Serving Framework FastAPI + WebSocket + ONNX Runtime

🧠 My Role β€” AI & Data Lead

  • Identified domain gap between AI Hub training data and real-world webcam environments
  • Designed a 4-stage automated labeling pipeline using CLIP (ViT-B/32)
  • Trained CNN (ResNet18) + Bi-LSTM sequence model
  • Built real-time FastAPI + WebSocket serving pipeline
  • Solved preprocessing mismatch issues causing unstable production inference
  • Optimized deployment using ONNX Runtime

πŸ—οΈ System Architecture

Webcam Stream
      ↓
WebSocket Streaming
      ↓
Preprocessing Pipeline
  β”œβ”€ BGR β†’ RGB Conversion
  β”œβ”€ Face Cropping
  └─ ImageNet Normalization
      ↓
Sliding Window Buffer (30 Frames)
      ↓
ONNX Runtime Inference
      ↓
Focus Score (%) + Latency Output

πŸ“‚ Project Structure

brainbuddy_AI/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py
β”‚   └── services/
β”‚       β”œβ”€β”€ preprocessor.py
β”‚       └── inference.py
β”‚
β”œβ”€β”€ scripts/
β”‚   └── export_onnx.py
β”‚
β”œβ”€β”€ onnx_model/
β”‚
β”œβ”€β”€ preprocessing/
β”‚   β”œβ”€β”€ clip1_1.py
β”‚   β”œβ”€β”€ clip1_2.py
β”‚   β”œβ”€β”€ clip1_3.py
β”‚   └── clip1_4.py
β”‚
β”œβ”€β”€ models/
β”œβ”€β”€ datasets/
β”œβ”€β”€ EDA/
β”‚
β”œβ”€β”€ train.py
β”œβ”€β”€ real_time.py
└── requirements.txt

πŸ”¬ AI Modeling Pipeline

Problem β€” Domain Gap

Models trained on the AI Hub eye-tracking dataset failed completely in real webcam environments.

Root causes identified:

  • unreliable labels
  • noisy engagement annotations
  • mismatch between training and inference distributions

Examples:

  • head-turning labeled as β€œunfocused”
  • smartphone viewing labeled as β€œfocused”

Solution β€” CLIP-based Automated Labeling Pipeline

Step Description Result
Step 1 CLIP zero-shot labeling 1,464 folders processed
Step 2 Confidence interval filtering 147 high-confidence samples
Step 3 Linear Probe training ROC-AUC 0.9806
Step 4 Full dataset relabeling Focused 890 / Unfocused 574

🧩 Model Architecture

CNN (ResNet18, ImageNet pretrained)
        ↓
Frame-level feature extraction
        ↓
Bi-LSTM sequence modeling
        ↓
Fully Connected Layer
        ↓
Sigmoid β†’ Focused / Unfocused

⚑ Real-time Serving Pipeline

Engineering Challenge

During the original competition integration phase, the backend inference pipeline consistently returned:

prob β‰ˆ 0.02

As a result, the model always predicted the β€œunfocused” state regardless of input.

Due to time constraints, another model was used for the final competition submission.

After the competition, I independently rebuilt the serving pipeline and identified the root causes.


Root Cause Analysis

The issue was caused by preprocessing inconsistency between training and inference environments.

Key issues identified

  • Missing BGR β†’ RGB conversion
  • Missing face cropping pipeline
  • Missing ImageNet normalization

These mismatches caused severe inference distribution shift.


πŸ”§ Serving Pipeline Reconstruction

β‘  Preprocessing Engine (preprocessor.py)

Implemented:

  • BGR β†’ RGB conversion
  • face cropping
  • ImageNet normalization

Additional troubleshooting:

  • MediaPipe compatibility issues in Python 3.13
  • Replaced with OpenCV Haar Cascade pipeline

β‘‘ ONNX Optimization (export_onnx.py)

  • Converted PyTorch model to ONNX Runtime
  • Embedded sigmoid operation into ServingWrapper
  • Configured dynamic batch axes

β‘’ Inference Engine (inference.py)

  • ONNX Runtime CPUExecutionProvider
  • Real-time latency measurement logic

Result

  • Achieved approximately 890ms latency
  • Measured using 30-frame sliding window inference

β‘£ Real-time Streaming Server (main.py)

  • FastAPI + WebSocket architecture
  • 10+ FPS real-time frame streaming
  • Sliding window buffering:
collections.deque(maxlen=30)

Benefits

  • memory optimization
  • stable sequence inference
  • reduced redundant processing

πŸ“Š Performance Evaluation

Metric Score
AUC 0.997
F1-Score 0.963
Accuracy 0.9505
Recall 0.9778
CPU Inference Latency ~890ms

🐳 Deployment

Run FastAPI Server

pip install -r requirements.txt
python scripts/export_onnx.py
uvicorn app.main:app --reload

Connect WebSocket client to:

ws://localhost:8000/ws

πŸ›£οΈ Roadmap

  • CLIP-based labeling pipeline
  • CNN + Bi-LSTM training pipeline
  • FastAPI + WebSocket real-time serving
  • ONNX optimization
  • Real-time latency optimization
  • Personal repository migration
  • Docker Compose deployment
  • Monitoring & logging
  • Architecture visualization
  • Production benchmark testing

🧰 Tech Stack

Backend & Infrastructure

FastAPI Β· WebSocket Β· ONNX Runtime Β· Docker

AI / Modeling

PyTorch Β· ResNet18 Β· Bi-LSTM Β· CLIP (ViT-B/32)

Data & Analytics

scikit-learn Β· UMAP Β· NumPy Β· Pandas

Computer Vision

OpenCV Β· Haar Cascade Β· Torchvision


πŸ“š Lessons Learned

This project taught me that reliable AI systems depend not only on model accuracy, but also on:

  • preprocessing consistency
  • inference environment alignment
  • deployment stability
  • real-time serving architecture
  • production debugging capability
  • latency optimization

πŸ‘€ Author

Kunoong

Applied AI Engineer focused on:

  • real-time AI systems
  • AI infrastructure
  • human-centered analytics
  • deployable ML serving systems

About

Real-time human focus analytics system with optimized AI inference serving and WebSocket streaming architecture.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages