Skip to content

kshitiz-arc/Script-Cognition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

   _____           _       __        ______                  _ __  _           
  / ___/__________(_)___  / /_      / ____/___  ____ _____  (_) /_(_)___  ____ 
  \__ \/ ___/ ___/ / __ \/ __/_____/ /   / __ \/ __ `/ __ \/ / __/ / __ \/ __ \
 ___/ / /__/ /  / / /_/ / /_/_____/ /___/ /_/ / /_/ / / / / / /_/ / /_/ / / / /
/____/\___/_/  /_/ .___/\__/      \____/\____/\__, /_/ /_/_/\__/_/\____/_/ /_/ 
                /_/                          /____/                            

Your handwriting knows how you feel. Now the model does too.


Python PyTorch Streamlit OpenCV License Dataset: EMOTHAW


Depression. Anxiety. Stress. โ€” three invisible forces that shape lives.
This project reads them from the strokes of a pen.



๐Ÿ‘๏ธ What Is This?

EmotionDetectionUsingHandwriting is a full machine learning pipeline that detects emotional states from online handwriting signals captured on digital tablets. Pen trajectory data โ€” coordinates, pressure, velocity, tilt โ€” gets rendered into grayscale images and fed into a CNN that classifies the writer's emotional state.

It uses the EMOTHAW dataset (129 subjects, 7 handwriting tasks each) and labels derived from the DASS-21 (Depression Anxiety Stress Scales) questionnaire.

  .svc file  โ”€โ”€โ–ถ  parse pen data  โ”€โ”€โ–ถ  render trajectory  โ”€โ”€โ–ถ  CNN  โ”€โ”€โ–ถ  emotion

๐ŸŒ Live Demo

๐Ÿ‘‰ Web Application:
https://script-cognition.streamlit.app/

โš ๏ธ Deployment Notice
This Streamlit demo is intended for interface visualization and workflow demonstration only.
Due to deployment constraints (model size and training requirements), the hosted version does not include fully trained models.

For accurate predictions and complete functionality, please run the application locally after training the models as described below.



โœฆ Features at a Glance

Feature Detail
๐Ÿง  Dual CNN Architectures EmotionCNN (2.7M params) ยท ResNet18 transfer learning (11.2M params)
๐ŸŽฏ Three Emotion Dimensions Depression ยท Anxiety ยท Stress โ€” train each independently
๐Ÿ“Š DASS-21 Labels Binary (Low / High) or full 5-class severity
๐Ÿ”ฌ 29+ Handwriting Features Velocity, pressure, curvature, stroke count, pen-lift frequency
๐ŸŒ Streamlit Web App Upload .svc files ยท switch emotions ยท live trajectory viz
๐Ÿ–ฅ๏ธ Powerful CLI Train ยท predict ยท visualize ยท extract โ€” all from the terminal
โšก GPU Ready CUDA support out of the box

๐Ÿ“Š Dataset at a Glance

  โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
  โ•‘  EMOTHAW Dataset                                     โ•‘
  โ•‘                                                      โ•‘
  โ•‘  Subjects    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  129     โ•‘
  โ•‘  Tasks/user  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ           7        โ•‘
  โ•‘  Samples     โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ   900+     โ•‘
  โ•‘  HW Features โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ   29+      โ•‘
  โ•‘  Emotions    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ                 3        โ•‘
  โ•‘                                                      โ•‘
  โ•‘  Format: .svc  โ†’  X, Y, timestamp, pressure,         โ•‘
  โ•‘                    pen status, azimuth, altitude     โ•‘
  โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Emotion targets (binary Low / High per DASS-21 thresholds):

  • ๐ŸŸฃ Depression โ€” Normal โ†’ Extremely Severe
  • ๐Ÿ”ต Anxiety โ€” Normal โ†’ Extremely Severe
  • ๐Ÿฉท Stress โ€” Normal โ†’ Extremely Severe

๐Ÿ—‚๏ธ Project Structure

EmotionDetectionUsingHandwriting/
โ”‚
โ”œโ”€โ”€ โš™๏ธ  config.py                    central configuration
โ”œโ”€โ”€ ๐Ÿš€  main.py                      unified CLI entry point
โ”œโ”€โ”€ ๐ŸŒ  app.py                       Streamlit web interface
โ”œโ”€โ”€ ๐Ÿƒ  run_app.py                   web app launcher helper
โ”œโ”€โ”€ ๐Ÿ“‹  requirements.txt
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  preprocessing/
โ”‚   โ””โ”€โ”€  svc_parser.py              .svc โ†’ NumPy arrays
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  data/
โ”‚   โ”œโ”€โ”€  label_loader.py            DASS scores โ†’ class labels
โ”‚   โ””โ”€โ”€  dataset.py                 PyTorch Dataset + DataLoader
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  features/
โ”‚   โ”œโ”€โ”€  image_generator.py         trajectories โ†’ images
โ”‚   โ””โ”€โ”€  signal_features.py         29+ handwriting features
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  models/
โ”‚   โ””โ”€โ”€  cnn_model.py               EmotionCNN + EmotionResNet
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  training/
โ”‚   โ””โ”€โ”€  train_cnn.py               training loop + early stopping
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  evaluation/
โ”‚   โ””โ”€โ”€  evaluate.py                metrics, confusion matrix, plots
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  inference/
โ”‚   โ””โ”€โ”€  predict.py                 run inference on new samples
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚  utils/
โ”‚   โ””โ”€โ”€  visualization.py           trajectory & dataset plots
โ”‚
โ””โ”€โ”€ ๐Ÿ“‚  outputs/
    โ”œโ”€โ”€  models/                    saved checkpoints (.pth)
    โ”œโ”€โ”€  plots/                     generated figures
    โ””โ”€โ”€  image_cache/               cached trajectory images

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.10+
  • EMOTHAW dataset at ../archive/DataEmothaw/
  • CUDA (optional, for GPU acceleration)

Installation

# Clone the repo
git clone https://github.com/kshitiz-arc/EmotionDetectionUsingHandwriting.git
cd EmotionDetectionUsingHandwriting

# Install dependencies
pip install -r requirements.txt

Expected dataset layout:

../archive/DataEmothaw/
โ”œโ”€โ”€ DASS_scores.xls
โ”œโ”€โ”€ user00001/
โ”œโ”€โ”€ user00002/
โ””โ”€โ”€ ...

๐ŸŽฎ Usage

1 ยท Train

# Single emotion
python main.py train --model cnn --emotion anxiety
python main.py train --model cnn --emotion depression
python main.py train --model cnn --emotion stress

# All three in one shot
python main.py train_all --model cnn

# Custom hyperparams
python main.py train --model resnet --emotion anxiety \
    --epochs 50 --batch_size 32 --lr 0.0001

Checkpoints save to outputs/models/ as best_cnn_anxiety.pth, etc.


2 ยท Launch the Web App

python run_app.py
# or
streamlit run app.py

The app lets you switch emotion targets, upload .svc files, see confidence scores, and explore dataset statistics โ€” all without touching the terminal again.


3 ยท Predict from the CLI

python main.py predict --svc_path "path/to/handwriting.svc" --emotion anxiety
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  HANDWRITING EMOTION PREDICTION
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  Input file  : u00001s00001_hw00001.svc
  Data points : 1474
  Dimension   : Anxiety

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  PREDICTED   : LOW
  CONFIDENCE  : 84.12%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  Low   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  0.8412
  High  โ–ˆโ–ˆโ–ˆโ–ˆ                          0.1588

4 ยท Visualize

python main.py visualize --statistics     # class distribution
python main.py visualize --trajectory 1   # specific user's trajectory
python main.py visualize --all            # everything

5 ยท Extract Features

python main.py extract --svc_path file.svc   # single file
python main.py extract --dataset              # full dataset

6 ยท Python API

from handwriting_emotion_detection import (
    load_all_svc_files, load_labels, get_dataloaders,
    EmotionCNN, extract_signal_features, plot_trajectory
)

samples  = load_all_svc_files(DATASET_ROOT)
labels   = load_labels(DASS_SCORES_PATH, target_emotion="anxiety")
train_loader, val_loader, test_loader = get_dataloaders(samples, labels)

model    = EmotionCNN(num_classes=2)
features = extract_signal_features(samples[0]["data"])

๐Ÿ—๏ธ Model Architectures

EmotionCNN โ€” fast & lean

Input (224ร—224)
    โ”‚
    โ–ผ
Conv Block ร—4  (BatchNorm + ReLU + MaxPool + Dropout)
    โ”‚
    โ–ผ
Fully Connected  โ†’  Softmax  โ†’  [Low, High]

Parameters : ~2.7M
Best for   : small datasets, fast iteration

EmotionResNet โ€” powerful & accurate

Input (224ร—224)
    โ”‚
    โ–ผ
ResNet18 backbone  (pretrained on ImageNet)
    โ”‚
    โ–ผ
Fine-tuned FC head  โ†’  Softmax  โ†’  [Low, High]

Parameters : ~11.2M
Best for   : maximum accuracy, transfer learning

Training setup โ€” Adam optimizer ยท ReduceLROnPlateau scheduler ยท Cross-Entropy with class weights ยท Early stopping ยท 70 / 15 / 15 train/val/test split


โš™๏ธ Configuration

All tunable params live in config.py:

Parameter Default Description
TARGET_EMOTION "anxiety" depression ยท anxiety ยท stress
USE_BINARY True True = Low/High ยท False = 5-class
IMAGE_SIZE 224 Trajectory render resolution (px)
BATCH_SIZE 16 Training batch size
NUM_EPOCHS 30 Max training epochs
LEARNING_RATE 1e-4 Initial Adam LR
EARLY_STOPPING_PATIENCE 7 Patience before stopping

๐Ÿ”ง Troubleshooting

Problem Fix
No trained model found Run python main.py train --model cnn --emotion anxiety first
Dataset not found Check config.py paths, ensure DASS_scores.xls exists
streamlit: not found pip install streamlit
Out of GPU memory Lower BATCH_SIZE in config.py or switch to CPU
Poor accuracy More epochs ยท lower LR ยท switch to --model resnet

๐Ÿค Contributing

git checkout -b feature/your-idea
# make changes, write tests, update docs
git commit -m "feat: your idea"
git push origin feature/your-idea
# open a Pull Request

Please follow PEP 8 and add docstrings to any new functions.


๐Ÿ“š References


๐Ÿ“„ License

APACHE 2.0 โ€” see LICENSE for details.


Built by kshitiz-arc

The pen is mightier than the word โ€” and now, smarter too.

About

A computational approach to understanding cognitive and emotional states through handwriting. It bridges behavioral patterns with intelligent analysis for deeper human insights.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors