CoolFace
Apppublic

txarst/pupillometry

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿ‘๏ธ PupilSense ๐Ÿ‘๏ธ๐Ÿ•ต๏ธโ€โ™‚๏ธ

PupilSense is a deep learning-powered application for estimating pupil diameter from images and videos. It uses trained ResNet models with Class Activation Mapping (CAM) for interpretable predictions.

Features

  • โ€”Image Processing: Upload images to get instant pupil diameter estimates
  • โ€”Video Processing: Analyze videos frame-by-frame for temporal pupil diameter analysis
  • โ€”Model Selection: Choose between ResNet18 and ResNet50 architectures
  • โ€”Pupil Selection: Analyze left pupil, right pupil, or both
  • โ€”Blink Detection: Automatically detect and handle blinks in the analysis
  • โ€”CAM Visualization: See which parts of the eye the model focuses on for predictions
  • โ€”API Access: Full Gradio API support for programmatic access

Usage

Web Interface

Simply upload an image or video file and configure your analysis parameters:

  • โ€”Select pupil(s) to analyze (left, right, or both)
  • โ€”Choose the model architecture (ResNet18 or ResNet50)
  • โ€”Enable/disable blink detection
  • โ€”Click process to get results

API Access

The Gradio interface provides automatic API endpoints. You can access the API documentation at /docs when the app is running.

Example API usage:

python
import requests
import json

# For image processing
files = {"image_input": open("your_image.jpg", "rb")}
data = {
    "pupil_selection": "both",
    "tv_model": "ResNet18",
    "blink_detection": True
}
response = requests.post("https://your-space-url/api/predict", files=files, data=data)

Model Information

The application uses pre-trained ResNet models specifically trained for pupil diameter estimation:

  • โ€”ResNet18: Faster inference, good accuracy
  • โ€”ResNet50: Higher accuracy, slower inference

Both models support:

  • โ€”Input resolution: 32x64 pixels (eye region)
  • โ€”Output: Pupil diameter in millimeters
  • โ€”CAM visualization for model interpretability

Technical Details

  • โ€”Face Detection: MediaPipe for robust face and eye detection
  • โ€”Preprocessing: Automatic eye region extraction and normalization
  • โ€”Deep Learning: PyTorch-based ResNet models
  • โ€”Visualization: Matplotlib for result plotting and CAM overlays
  • โ€”Video Support: Frame-by-frame analysis with temporal plotting

Installation & Setup

Local Development

  1. 1.Clone the repository
bash
git clone <repository-url>
cd pupilsense
  1. 1.Create virtual environment
bash
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 1.Install dependencies
bash
pip install -r requirements.txt
  1. 1.Run the application
bash
python app.py

The app will be available at http://localhost:7860

Hugging Face Spaces Deployment

  1. 1.Create a new Space on Hugging Face with Gradio SDK
  2. 2.Upload all files from the pupilsense directory
  3. 3.Ensure the following files are present:
  4. 4.app.py (main application file)
  5. 5.gradio_app.py (Gradio interface)
  6. 6.gradio_utils.py (utility functions)
  7. 7.requirements.txt (dependencies)
  8. 8.README.md (this file with proper YAML header)
  9. 9.pre_trained_models/ (model files)
  10. 10.All other supporting files

Known Issues & Troubleshooting

MediaPipe Issues

  • โ€”Issue: Segmentation fault or MediaPipe errors in headless environments
  • โ€”Solution: The app includes error handling for MediaPipe failures. In production environments, ensure proper GPU/display drivers are available.

Model Loading

  • โ€”Issue: Model files not found
  • โ€”Solution: Ensure pre_trained_models/ directory contains the required .pt files for both ResNet18 and ResNet50 models.

Memory Usage

  • โ€”Issue: High memory usage with large videos
  • โ€”Solution: The app automatically resizes frames to 640x480 to manage memory usage.

File Structure

pupilsense/
โ”œโ”€โ”€ app.py                 # Main application entry point
โ”œโ”€โ”€ gradio_app.py         # Gradio interface definition
โ”œโ”€โ”€ gradio_utils.py       # Utility functions (MediaPipe-free)
โ”œโ”€โ”€ app_utils.py          # Original Streamlit utilities (legacy)
โ”œโ”€โ”€ requirements.txt      # Python dependencies
โ”œโ”€โ”€ README.md            # This file
โ”œโ”€โ”€ config.yml           # Configuration file
โ”œโ”€โ”€ registry.py          # Model registry
โ”œโ”€โ”€ registry_utils.py    # Registry utilities
โ”œโ”€โ”€ utils.py             # General utilities
โ”œโ”€โ”€ pre_trained_models/  # Trained model files
โ”‚   โ”œโ”€โ”€ ResNet18/
โ”‚   โ”‚   โ”œโ”€โ”€ left_eye.pt
โ”‚   โ”‚   โ””โ”€โ”€ right_eye.pt
โ”‚   โ””โ”€โ”€ ResNet50/
โ”‚       โ”œโ”€โ”€ left_eye.pt
โ”‚       โ””โ”€โ”€ right_eye.pt
โ”œโ”€โ”€ preprocessing/       # Data preprocessing modules
โ”œโ”€โ”€ feature_extraction/  # Feature extraction modules
โ”œโ”€โ”€ registrations/       # Model registration modules
โ””โ”€โ”€ sample_videos/       # Sample video files

Contributing

  1. 1.Fork the repository
  2. 2.Create a feature branch
  3. 3.Make your changes
  4. 4.Test thoroughly
  5. 5.Submit a pull request

License

See LICENSE file for details.


Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference