File size: 4,065 Bytes
141d3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
8abc170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
license: cc-by-nc-4.0
language:
  - en
  - zh
tags:
  - micro-expression
  - facial-expression-recognition
  - video-classification
  - spotting
  - pytorch
pipeline_tag: video-classification
---

# Micro-Expression Spotting and Recognition

**[δΈ­ζ–‡ζ–‡ζ‘£](README_zh.md)** | English

A PyTorch-based end-to-end framework for **micro-expression recognition** (short clips) and **long-video micro-expression spotting**, with a built-in web visualization platform.

Supports datasets: CASME2 / SAMM / SMIC-HS / CAS(ME)Β³

---

## Quick Start

### 1. Set up environment

```bash
conda create -n me-env python=3.10
conda activate me-env

# GPU (CUDA 12.1)
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

# CPU only
# conda install pytorch torchvision torchaudio cpuonly -c pytorch

pip install -r requirements.txt
```

### 2. Download model weights

```python
from huggingface_hub import snapshot_download

snapshot_download(
    repo_id="ghy-cmd/micro-expression-weights",
    local_dir=".",  # restores checkpoints/ and weights/ under project root
)
```

### 3. Start the web app

```bash
python -m webapp.app
# open http://localhost:5001
```

---

## Features

| Feature | Description |
|---------|-------------|
| **Micro-Expression Recognition** | Upload a short video clip, auto-preprocess and classify emotion via PGCAN model |
| **Long-Video Spotting** | Upload any long video, auto face-crop + sliding-window inference, locates all micro-expression intervals |

---

## Model Description

### Task 1 Β· Micro-Expression Recognition

Classifies short clips into **3 categories**: Positive / Negative / Surprise

- HRNet feature extractor on onset & apex frames β†’ (2, 256, 64, 64)
- TV-L1 optical flow (u, v, strain) β†’ (3, 256, 256)
- Transformer encoder with attention fusion (16 heads, 4 stages, full guidance)
- Trained with LOSO cross-validation on Composite dataset (CASME2 + SMIC-HS + SAMM)

### Task 2 Β· Long-Video Micro-Expression Spotting

Detects all micro-expression intervals in arbitrary-length videos, classifies each into **4 categories**: Positive / Negative / Surprise / Others

- Sliding-window inference (window = 0.4s, stride = 0.2s @ 30 fps)
- Multi-scale temporal features (6 scales)
- Dual-task head: detection score + emotion classification
- Trained on CAS(ME)Β³-LV dataset
- Evaluation metric: overlap-based F1 (any frame overlap = TP)

---

## Preprocessing Pipeline

Both tasks share the same pipeline, consistent with dataset preprocessing:

1. **Face crop** (dlib 68-point landmarks) β†’ 256Γ—256
2. **Apex frame detection** (optical strain + UPC) β€” recognition only
3. **HRNet feature extraction** β†’ (2, 256, 64, 64)
4. **Optical flow** (TV-L1) β†’ (3, 256, 256)

---

## Training

```bash
# Recognition β€” LOSO training
python train_loso.py --config configs/train_loso.yaml --dataset composite

# Detection β€” LOSO training
python detection/train_detection.py --config configs/detection_config.yaml
```

See [docs/TRAINING_GUIDE.md](docs/TRAINING_GUIDE.md) for full training guide.

---

## Project Structure

```
β”œβ”€β”€ webapp/                  # Web visualization platform (Flask)
β”‚   β”œβ”€β”€ app.py               # Entry point β†’ http://localhost:5000
β”‚   β”œβ”€β”€ api/                 # REST API (recognition + detection)
β”‚   └── core/                # Inference wrappers
β”œβ”€β”€ models/                  # Model architectures
β”œβ”€β”€ detection/               # Spotting training & evaluation
β”œβ”€β”€ trainers/                # Recognition training utilities
β”œβ”€β”€ configs/                 # YAML config files
β”œβ”€β”€ datasets/                # Dataset loaders
β”œβ”€β”€ utils/                   # Shared utilities
β”œβ”€β”€ docs/                    # Documentation & visualization scripts
β”œβ”€β”€ train_loso.py            # Recognition LOSO training script
└── requirements.txt
```

---

## License

[CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/) β€” free for academic & personal use, **commercial use prohibited**.