File size: 14,567 Bytes
c4a514c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
---
license: mit
library_name: pytorch
pipeline_tag: image-to-image
tags:
  - facial-landmarks
  - mediapipe
  - opencv
  - pytorch
  - multimodal
  - cosmetic-surgery-simulation
---

# MFAF-Net: Multimodal Facial Analysis and Feature-guided Network

![Python 3.8+](https://img.shields.io/badge/Python-3.8%2B-blue?logo=python&logoColor=white)
![Flask](https://img.shields.io/badge/Flask-2.x-lightgrey?logo=flask)
![MediaPipe](https://img.shields.io/badge/MediaPipe-FaceMesh-green?logo=google)
![OpenCV](https://img.shields.io/badge/OpenCV-4.x-red?logo=opencv)

---

## Abstract

**MFAF-Net** (Multimodal Facial Analysis and Feature-guided Network) is a deep-learning-inspired computational framework designed to simulate and predict outcomes of common facial cosmetic surgical procedures. The system integrates facial landmark extraction via MediaPipe FaceMesh with patient demographic information to construct a rich multimodal feature representation. Procedure-specific attention mechanisms weight relevant anatomical regions, while a geometric-warp simulation engine renders a realistic post-operative approximation directly on the patient's photograph β€” all without relying on external generative AI or trained diffusion models.

The network is organized as a five-module pipeline: (1) Facial Feature Extraction, (2) Patient Information Analysis, (3) Feature Fusion with Attention, (4) Procedural Simulation, and (5) Quantitative Comparison. This architecture enables reproducible, interpretable predictions suitable for clinical communication and academic research. MFAF-Net is proposed as a proof-of-concept for integrating multimodal patient data with computer-vision-driven surgical outcome prediction, with applicability to rhinoplasty, blepharoplasty, rhytidectomy, genioplasty, forehead surgery, and brow lifting.

---

## Architecture

```
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚              MFAF-Net Pipeline                  β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

   Patient Photo                                         Patient Metadata
        β”‚                                               (age, sex, skin, Rx)
        β–Ό                                                        β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MODULE 1    β”‚                                      β”‚   MODULE 2      β”‚
β”‚  Facial        β”‚  468 landmarks                      β”‚  Patient Info   β”‚
β”‚  Feature       │──────────────────────────┐          β”‚  Analysis       β”‚
β”‚  Extraction    β”‚  Region measurements     β”‚          β”‚                 β”‚
β”‚  (MediaPipe)   β”‚                          β”‚          β”‚  Feature vector β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                          β”‚          β”‚  (12 dims)      β”‚
                                           β–Ό          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
                                  β”‚   MODULE 3      β”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚  Feature Fusion β”‚
                                  β”‚  + Procedure-   β”‚
                                  β”‚  aware Attentionβ”‚
                                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                           β”‚ Fused vector
                                           β–Ό
                                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                  β”‚   MODULE 4      β”‚
                                  β”‚  Facial         β”‚
                                  β”‚  Simulation     β”‚
                                  β”‚  (Delaunay warp)β”‚
                                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                           β”‚ Simulated image
                                           β–Ό
                                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                  β”‚   MODULE 5      β”‚
                                  β”‚  Result         β”‚
                                  β”‚  Comparison     β”‚
                                  β”‚  (SSIM/PSNR/MAE)β”‚
                                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                           β”‚
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚   Web Dashboard       β”‚
                               β”‚  (Flask + HTML/CSS/JS)β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## Installation

### Prerequisites
- Python 3.8 or later
- pip

### Steps

**1. Install Python dependencies**
```bash
pip install -r requirements.txt
```

**2. Create required directories**
```bash
python setup_dirs.py
```

**3. Launch the application**
```bash
python run.py
```

**4. Open in your browser**
```
http://localhost:5000
```

---

## Supported Procedures

| Procedure | Display Name | Description |
|-----------|-------------|-------------|
| `rhinoplasty` | Rhinoplasty (Nose Job) | Reshapes the nose β€” slims the bridge and refines the tip using nose landmark morphing |
| `blepharoplasty` | Blepharoplasty (Eyelid Surgery) | Lifts upper eyelids and reduces under-eye area by adjusting periorbital landmarks |
| `rhytidectomy` | Rhytidectomy (Facelift) | Tightens jawline and lifts cheek landmarks to simulate facial rejuvenation |
| `genioplasty` | Genioplasty (Chin Augmentation) | Projects the chin landmark forward and downward to enhance chin definition |
| `forehead_surgery` | Forehead Reduction Surgery | Smooths forehead landmarks upward with slight brow elevation |
| `brow_lift` | Brow Lift (Browplasty) | Raises brow landmarks to simulate a youthful, elevated brow position |

---

## Technology Stack

| Component | Technology | Version |
|-----------|-----------|---------|
| Web Framework | Flask | 2.x |
| Landmark Detection | MediaPipe FaceMesh | β‰₯ 0.10 |
| Image Processing | OpenCV | 4.x |
| Numerical Computing | NumPy | β‰₯ 1.21 |
| Image Quality Metrics | scikit-image | β‰₯ 0.19 |
| Visualization | Matplotlib | β‰₯ 3.5 |
| Image I/O | Pillow | β‰₯ 9.0 |
| Language | Python | 3.8+ |

---

## Module Descriptions

### Module 1 β€” Facial Feature Extraction (`mfaf_net/module1_feature_extraction.py`)
Uses MediaPipe FaceMesh to detect **468 facial landmarks** from the input photograph. Key anatomical regions (nose, eyes, eyebrows, chin, jawline, forehead) are isolated, and geometric measurements (nose width/length, eye width, face width, jaw width, chin projection) are computed as Euclidean distances between landmark pairs. An annotated overlay image is saved for review.

### Module 2 β€” Patient Information Analysis (`mfaf_net/module2_patient_analysis.py`)
Encodes patient metadata into a 12-dimensional numerical feature vector. Age is normalised (Γ· 100); sex is encoded as `{M: 0, F: 1, Other: 0.5}`; Fitzpatrick skin type (I–VI) maps to `[0.0 … 1.0]`; procedure is one-hot encoded across 6 dimensions; treatment history is binary. This vector is passed to Module 3 alongside the visual features.

### Module 3 β€” Procedure-aware Feature Fusion (`mfaf_net/module3_feature_fusion.py`)
Concatenates the flattened landmark array (936 floats), measurement values, and patient feature vector. Applies a **procedure-specific attention mask**: the anatomical region most relevant to the requested procedure receives 3Γ— weight, ensuring the downstream simulation focuses on the clinically salient area. Returns the fused vector and a human-readable attention-weight map.

### Module 4 β€” Facial Simulation (`mfaf_net/module4_simulation.py`)
Applies **procedural geometric transformations** directly to the uploaded photograph without any external AI model. Landmarks are displaced according to procedure-specific rules (e.g. rhinoplasty moves nose-side landmarks inward 8% and the tip upward 5%). The face is then subdivided into Delaunay triangles and an affine warp is applied per triangle, re-assembling a realistic post-operative appearance. Gaussian blurring smooths seam boundaries.

### Module 5 β€” Result Comparison (`mfaf_net/module5_comparison.py`)
Quantitatively evaluates the transformation by comparing the original and simulated images. Computes:
- **SSIM** β€” Structural Similarity Index (image quality measure)
- **PSNR** β€” Peak Signal-to-Noise Ratio (in dB)
- **MAE** β€” Mean Absolute Error (per-pixel intensity difference)
- **Changed Pixel %** β€” Fraction of pixels altered by > 15 intensity units

Generates a colourised difference heatmap and a 4-panel matplotlib figure for the dashboard.

---

## Dataset Compatibility

MFAF-Net is designed to operate on **per-patient Excel records** (`.xlsx` / `.csv`) containing the following columns:

| Column | Type | Description |
|--------|------|-------------|
| `patient_id` | string | Unique patient identifier |
| `image_path` | string | Relative path to the facial photograph |
| `age` | int | Patient age (years) |
| `sex` | string | `M` / `F` / `Other` |
| `skin_type` | int | Fitzpatrick scale I–VI |
| `procedure` | string | One of the 6 supported procedure keys |
| `treatment_history` | int | `0` = no prior treatment, `1` = prior treatment |

A batch-processing utility can be added to iterate over dataset rows and invoke the MFAF-Net pipeline for each patient, saving results to a structured output directory.

---

## Comparison with LandmarkDiff

MFAF-Net is designed as an improvement over **LandmarkDiff** (HuggingFace: `dreamlessx/LandmarkDiff`), the existing state-of-the-art approach for landmark-guided surgical simulation.

| Feature | LandmarkDiff | **MFAF-Net (Ours)** |
|---------|-------------|---------------------|
| Input modality | Image only | **Image + patient metadata (multimodal)** |
| Landmark model | 478-point 3D mesh | 468-point FaceMesh + patient fusion |
| Patient context | None | **Age, sex, Fitzpatrick skin type, treatment history** |
| Deformation model | Gaussian RBF | **Procedure-aware piecewise affine + patient-guided attention weights** |
| Output | Simulated image | **Image + difference heatmap + quantitative metrics** |
| Evaluation | Qualitative | **SSIM, PSNR, MAE, changed-pixel %** |
| GPU requirement | Yes (Stable Diffusion 1.5 + ControlNet) | **No β€” runs entirely on CPU** |
| Deployment | Docker + GPU required | **Flask, runs anywhere with `python run.py`** |
| Procedures supported | 6 (similar set) | 6 (rhinoplasty, blepharoplasty, rhytidectomy, genioplasty, forehead surgery, brow lift) |

### Key Advantages of MFAF-Net
- **Multimodal fusion**: patient metadata enriches the simulation via procedure-aware attention, something LandmarkDiff entirely omits.
- **Quantitative evaluation**: SSIM/PSNR/MAE metrics make outcomes comparable and reproducible.
- **No GPU required**: LandmarkDiff depends on Stable Diffusion 1.5 running on a CUDA-capable GPU. MFAF-Net runs on any CPU.
- **Lightweight deployment**: single Flask server, no Docker, no diffusion model weights to download.

---

## Dataset Setup

The project is compatible with the **Complete Plastic Surgery Dataset** (`Complete_PlasticSurgery_Datasets.zip`).

### Steps
1. Extract the ZIP archive into the `data/` folder inside the project root:
   ```
   aparna/
   └── data/
       β”œβ”€β”€ rhinoplasty/
       β”‚   β”œβ”€β”€ before/   ← pre-operative images
       β”‚   └── after/    ← post-operative images
       β”œβ”€β”€ blepharoplasty/
       β”‚   β”œβ”€β”€ before/
       β”‚   └── after/
       β”œβ”€β”€ rhytidectomy/
       β”œβ”€β”€ genioplasty/
       β”œβ”€β”€ forehead_surgery/
       β”œβ”€β”€ brow_lift/
       └── metadata.csv  ← optional patient metadata (age, sex, skin_type, etc.)
   ```

2. Run the dataset info route to verify detection:
   ```
   GET http://localhost:5000/api/dataset-info
   ```
   This will report the number of image pairs found per procedure.

### Dataset Loader (`mfaf_net/dataset_loader.py`)
A utility module scans the `data/` directory and:
- Discovers before/after image pairs organised by procedure subfolder
- Loads any `.csv` or `.xlsx` metadata files found
- Returns per-procedure counts and file paths for batch processing

---

## Conference Paper Note

> This project serves as a proof-of-concept implementation of the MFAF-Net architecture proposed for academic submission. The methodology, module design, and evaluation metrics are intended to accompany a conference or journal paper describing the MFAF-Net framework in detail.

---

## Disclaimer

> **⚠️ Educational Purposes Only.**
> MFAF-Net is an academic research prototype and is **not** a certified medical device. It must not be used for clinical decision-making, patient counselling, or surgical planning. All simulated images are approximations for illustrative purposes only. Always consult a qualified medical professional for any surgical considerations.

---

## License

This project is released under the [MIT License](https://opensource.org/licenses/MIT).

```
MIT License

Copyright (c) 2024 MFAF-Net Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
```