Spaces:
Running
Running
Updated README.md
Browse files
README.md
CHANGED
@@ -1,14 +1,142 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Face Segmentation Tool
|
2 |
+
|
3 |
+
A deep learning-based tool for precise face segmentation using BiSeNet trained on the CelebAMask-HQ dataset. This tool extracts faces from images with a transparent background, perfect for creating profile pictures, avatars, or creative photo editing.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
|
7 |
+
- Accurate face and hair segmentation with transparent background
|
8 |
+
- 19-class facial attribute segmentation (skin, eyes, eyebrows, nose, lips, hair, etc.)
|
9 |
+
- User-friendly Streamlit web interface
|
10 |
+
- MediaPipe face detection to identify and focus on faces
|
11 |
+
- Support for downloading the segmented result
|
12 |
+
|
13 |
+
## Technical Details
|
14 |
+
|
15 |
+
This project uses:
|
16 |
+
|
17 |
+
- **BiSeNet** (Bilateral Segmentation Network) for semantic segmentation
|
18 |
+
- **CelebAMask-HQ dataset** trained model with 19 facial attribute classes
|
19 |
+
- **MediaPipe** for initial face detection and bounding box estimation
|
20 |
+
- **PyTorch** for the deep learning components
|
21 |
+
- **Streamlit** for the web interface
|
22 |
+
|
23 |
+
## Installation
|
24 |
+
|
25 |
+
### Prerequisites
|
26 |
+
|
27 |
+
- Python 3.7 or newer
|
28 |
+
- CUDA-compatible GPU (optional, but recommended for faster processing)
|
29 |
+
|
30 |
+
### Setup
|
31 |
+
|
32 |
+
1. Clone the repository:
|
33 |
+
|
34 |
+
```bash
|
35 |
+
git clone https://github.com/yourusername/face-segmentation.git
|
36 |
+
cd face-segmentation
|
37 |
+
```
|
38 |
+
|
39 |
+
2. Create and activate a virtual environment (recommended):
|
40 |
+
|
41 |
+
```bash
|
42 |
+
# On Windows
|
43 |
+
python -m venv venv
|
44 |
+
venv\Scripts\activate
|
45 |
+
|
46 |
+
# On macOS/Linux
|
47 |
+
python -m venv venv
|
48 |
+
source venv/bin/activate
|
49 |
+
```
|
50 |
+
|
51 |
+
3. Install the required dependencies:
|
52 |
+
|
53 |
+
```bash
|
54 |
+
pip install -r requirements.txt
|
55 |
+
```
|
56 |
+
|
57 |
+
4. Ensure you have the BiSeNet model weights file (`bisenet.pth`) in the project root directory.
|
58 |
+
(The file should already be included in the repository)
|
59 |
+
|
60 |
+
## Usage
|
61 |
+
|
62 |
+
1. Start the Streamlit app:
|
63 |
+
|
64 |
+
```bash
|
65 |
+
streamlit run app.py
|
66 |
+
```
|
67 |
+
|
68 |
+
2. Open your web browser and go to the URL shown in the console (typically http://localhost:8501)
|
69 |
+
|
70 |
+
3. Upload an image with a face
|
71 |
+
|
72 |
+
4. Click the "Segment Face" button
|
73 |
+
|
74 |
+
5. View and download the segmented result
|
75 |
+
|
76 |
+
## Class Labels in CelebAMask-HQ
|
77 |
+
|
78 |
+
The model recognizes 19 different facial attributes:
|
79 |
+
|
80 |
+
| ID | Class | Description |
|
81 |
+
| --- | ---------- | ------------------------- |
|
82 |
+
| 0 | background | Non-face background areas |
|
83 |
+
| 1 | skin | Face skin |
|
84 |
+
| 2 | nose | Nose |
|
85 |
+
| 3 | eye_g | Eyeglasses |
|
86 |
+
| 4 | l_eye | Left eye |
|
87 |
+
| 5 | r_eye | Right eye |
|
88 |
+
| 6 | l_brow | Left eyebrow |
|
89 |
+
| 7 | r_brow | Right eyebrow |
|
90 |
+
| 8 | l_ear | Left ear |
|
91 |
+
| 9 | r_ear | Right ear |
|
92 |
+
| 10 | mouth | Mouth |
|
93 |
+
| 11 | u_lip | Upper lip |
|
94 |
+
| 12 | l_lip | Lower lip |
|
95 |
+
| 13 | hair | Hair |
|
96 |
+
| 14 | hat | Hat |
|
97 |
+
| 15 | ear_r | Ear rings |
|
98 |
+
| 16 | neck_l | Neck area |
|
99 |
+
| 17 | neck | Neck |
|
100 |
+
| 18 | cloth | Clothing |
|
101 |
+
|
102 |
+
## How It Works
|
103 |
+
|
104 |
+
1. The app uses MediaPipe to detect faces in the uploaded image
|
105 |
+
2. It crops and processes the face region using BiSeNet
|
106 |
+
3. BiSeNet performs semantic segmentation to classify each pixel into one of 19 classes
|
107 |
+
4. Selected facial features are preserved while background, neck, and clothes are made transparent
|
108 |
+
5. The result is an RGBA image with the face and hair intact and a transparent background
|
109 |
+
|
110 |
+
## Customization
|
111 |
+
|
112 |
+
You can modify which facial attributes to keep by adjusting the `keep_classes` list in the `FaceHairSegmenter` class:
|
113 |
+
|
114 |
+
```python
|
115 |
+
# Current configuration - keeps all face parts except background, clothes, and neck
|
116 |
+
self.keep_classes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 18]
|
117 |
+
```
|
118 |
+
|
119 |
+
## Troubleshooting
|
120 |
+
|
121 |
+
- **No face detected**: Ensure the image contains a clearly visible face.
|
122 |
+
- **Multiple faces detected**: The app works best with a single face per image.
|
123 |
+
- **Poor segmentation**: For best results, use images with good lighting and a clear face.
|
124 |
+
- **CUDA out of memory**: Try using a smaller image or run on CPU if your GPU has limited memory.
|
125 |
+
- **PyTorch class path error**: If you encounter an error like "Tried to instantiate class '**path**.\_path', but it does not exist!", try updating your PyTorch version to 1.9.0 or newer using `pip install torch==1.9.0 torchvision==0.10.0`. This is a known issue with certain PyTorch versions when loading models.
|
126 |
+
|
127 |
+
## PyTorch Version Compatibility
|
128 |
+
|
129 |
+
This project is tested with PyTorch 1.7.0 to 1.13.0. If you encounter model loading issues with newer PyTorch versions (2.x), try downgrading to PyTorch 1.13.0:
|
130 |
+
|
131 |
+
```bash
|
132 |
+
pip install torch==1.13.0 torchvision==0.14.0
|
133 |
+
```
|
134 |
+
|
135 |
+
## References
|
136 |
+
|
137 |
+
- [BiSeNet: Bilateral Segmentation Network for Real-time Semantic Segmentation](https://arxiv.org/abs/1808.00897)
|
138 |
+
- [CelebAMask-HQ Dataset](https://github.com/switchablenorms/CelebAMask-HQ)
|
139 |
+
|
140 |
+
## License
|
141 |
+
|
142 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|