devkunalnaik commited on
Commit
acb9f1e
·
0 Parent(s):

Initial commit: Face & Body Swapper

Browse files
README.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Face & Body Swapper
3
+ emoji: 🔄
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # Face & Body Swapper
14
+
15
+ A tool for swapping faces and bodies in images and videos using AI.
16
+
17
+ ## Features
18
+ - **Face Swap (Image)** — Replace faces in a target image with a source face
19
+ - **Body Swap (Image)** — Transplant a person's body onto a target scene
20
+ - **Face Swap (Video)** — Apply face swap frame-by-frame on a video
21
+ - **Body Swap (Video)** — Apply body swap frame-by-frame on a video
22
+
23
+ ## Tech Stack
24
+ - [InsightFace](https://github.com/deepinsight/insightface) — Face detection & swapping
25
+ - [GFPGAN](https://github.com/TencentARC/GFPGAN) — Face enhancement
26
+ - [rembg](https://github.com/danielgatis/rembg) — Person segmentation
27
+ - [MediaPipe](https://developers.google.com/mediapipe) — Pose estimation
28
+ - [OpenCV](https://opencv.org/) — Image/video processing
29
+ - [FFmpeg](https://ffmpeg.org/) — Video encoding
30
+
31
+ ## Ethical Use
32
+
33
+ > ⚠️ **Important**: Only use with explicit consent of all persons involved.
34
+ > Do not use to create misleading, deceptive, non-consensual, or harmful content.
35
+ > The creators of this tool are not responsible for misuse.
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Face & Body Swapper — Gradio app for Hugging Face Spaces (ZeroGPU).
3
+
4
+ Tabs
5
+ ----
6
+ 1. Face Swap (Image)
7
+ 2. Body Swap (Image)
8
+ 3. Face Swap (Video)
9
+ 4. Body Swap (Video)
10
+ """
11
+
12
+ import cv2
13
+ import numpy as np
14
+ import gradio as gr
15
+ import spaces
16
+ from PIL import Image
17
+
18
+ from utils.image_utils import pil_to_bgr, bgr_to_pil
19
+
20
+ # ── Lazy processor singletons ─────────────────────────────────────────────────
21
+ _face_swapper = None
22
+ _body_swapper = None
23
+
24
+
25
+ def _get_face_swapper():
26
+ global _face_swapper
27
+ if _face_swapper is None:
28
+ from processors.face_swap import FaceSwapper
29
+ _face_swapper = FaceSwapper()
30
+ return _face_swapper
31
+
32
+
33
+ def _get_body_swapper():
34
+ global _body_swapper
35
+ if _body_swapper is None:
36
+ from processors.body_swap import BodySwapper
37
+ _body_swapper = BodySwapper()
38
+ return _body_swapper
39
+
40
+
41
+ # ── Processing functions (each decorated for ZeroGPU) ────────────────────────
42
+
43
+ @spaces.GPU()
44
+ def face_swap_image(
45
+ source_img: Image.Image,
46
+ target_img: Image.Image,
47
+ enhance: bool,
48
+ ) -> tuple[Image.Image | None, str]:
49
+ if source_img is None or target_img is None:
50
+ return None, "Please upload both a source and a target image."
51
+
52
+ result, msg = _get_face_swapper().swap(
53
+ pil_to_bgr(source_img),
54
+ pil_to_bgr(target_img),
55
+ enhance=enhance,
56
+ )
57
+ return (bgr_to_pil(result) if result is not None else None), msg
58
+
59
+
60
+ @spaces.GPU()
61
+ def body_swap_image(
62
+ source_img: Image.Image,
63
+ target_img: Image.Image,
64
+ blend_strength: float,
65
+ ) -> tuple[Image.Image | None, str]:
66
+ if source_img is None or target_img is None:
67
+ return None, "Please upload both a source and a target image."
68
+
69
+ result, msg = _get_body_swapper().swap(
70
+ pil_to_bgr(source_img),
71
+ pil_to_bgr(target_img),
72
+ blend_strength=blend_strength,
73
+ )
74
+ return (bgr_to_pil(result) if result is not None else None), msg
75
+
76
+
77
+ @spaces.GPU()
78
+ def face_swap_video(
79
+ source_img: Image.Image,
80
+ target_video: str,
81
+ enhance: bool,
82
+ progress=gr.Progress(),
83
+ ) -> tuple[str | None, str]:
84
+ if source_img is None or target_video is None:
85
+ return None, "Please upload a source face image and a target video."
86
+
87
+ from processors.video_processor import VideoProcessor
88
+ processor = VideoProcessor(face_swapper=_get_face_swapper())
89
+ output_path, msg = processor.process_video(
90
+ pil_to_bgr(source_img),
91
+ target_video,
92
+ mode="face",
93
+ enhance=enhance,
94
+ progress=progress,
95
+ )
96
+ return output_path, msg
97
+
98
+
99
+ @spaces.GPU()
100
+ def body_swap_video(
101
+ source_img: Image.Image,
102
+ target_video: str,
103
+ blend_strength: float,
104
+ progress=gr.Progress(),
105
+ ) -> tuple[str | None, str]:
106
+ if source_img is None or target_video is None:
107
+ return None, "Please upload a source body image and a target video."
108
+
109
+ from processors.video_processor import VideoProcessor
110
+ processor = VideoProcessor(body_swapper=_get_body_swapper())
111
+ output_path, msg = processor.process_video(
112
+ pil_to_bgr(source_img),
113
+ target_video,
114
+ mode="body",
115
+ blend_strength=blend_strength,
116
+ progress=progress,
117
+ )
118
+ return output_path, msg
119
+
120
+
121
+ # ── Gradio UI ─────────────────────────────────────────────────────────────────
122
+
123
+ DISCLAIMER = """
124
+ > ⚠️ **Ethical Use Only** — Only process images/videos of yourself or with
125
+ > explicit written consent of every person depicted. Do **not** use this tool
126
+ > to create misleading, deceptive, non-consensual, or harmful content.
127
+ """
128
+
129
+ with gr.Blocks(title="Face & Body Swapper", theme=gr.themes.Soft()) as demo:
130
+ gr.Markdown("# 🔄 Face & Body Swapper")
131
+ gr.Markdown(DISCLAIMER)
132
+
133
+ with gr.Tabs():
134
+
135
+ # ── Tab 1: Face Swap — Image ──────────────────────────────────────────
136
+ with gr.TabItem("Face Swap · Image"):
137
+ gr.Markdown(
138
+ "The **source** face is placed onto every detected face in the **target** image."
139
+ )
140
+ with gr.Row():
141
+ with gr.Column(scale=1):
142
+ fi_source = gr.Image(label="Source — face to use", type="pil")
143
+ fi_target = gr.Image(label="Target — image to modify", type="pil")
144
+ fi_enhance = gr.Checkbox(
145
+ label="Enhance output faces (GFPGAN — slower)",
146
+ value=True,
147
+ )
148
+ fi_btn = gr.Button("Swap Faces", variant="primary")
149
+ with gr.Column(scale=1):
150
+ fi_output = gr.Image(label="Result", type="pil")
151
+ fi_status = gr.Textbox(label="Status", interactive=False)
152
+
153
+ fi_btn.click(
154
+ face_swap_image,
155
+ inputs=[fi_source, fi_target, fi_enhance],
156
+ outputs=[fi_output, fi_status],
157
+ )
158
+
159
+ # ── Tab 2: Body Swap — Image ──────────────────────────────────────────
160
+ with gr.TabItem("Body Swap · Image"):
161
+ gr.Markdown(
162
+ "The **source** person's body is transplanted into the **target** scene."
163
+ )
164
+ with gr.Row():
165
+ with gr.Column(scale=1):
166
+ bi_source = gr.Image(label="Source — body to use", type="pil")
167
+ bi_target = gr.Image(label="Target — scene to modify", type="pil")
168
+ bi_blend = gr.Slider(
169
+ minimum=0.1, maximum=1.0, value=0.85, step=0.05,
170
+ label="Blend Strength",
171
+ )
172
+ bi_btn = gr.Button("Swap Body", variant="primary")
173
+ with gr.Column(scale=1):
174
+ bi_output = gr.Image(label="Result", type="pil")
175
+ bi_status = gr.Textbox(label="Status", interactive=False)
176
+
177
+ bi_btn.click(
178
+ body_swap_image,
179
+ inputs=[bi_source, bi_target, bi_blend],
180
+ outputs=[bi_output, bi_status],
181
+ )
182
+
183
+ # ── Tab 3: Face Swap — Video ──────────────────────────────────────────
184
+ with gr.TabItem("Face Swap · Video"):
185
+ gr.Markdown(
186
+ "Applies face swap to every frame of the **target** video. "
187
+ "Max video length: **300 frames** (~10 s at 30 fps)."
188
+ )
189
+ with gr.Row():
190
+ with gr.Column(scale=1):
191
+ fv_source = gr.Image(label="Source Face Image", type="pil")
192
+ fv_target = gr.Video(label="Target Video")
193
+ fv_enhance = gr.Checkbox(
194
+ label="Enhance faces (GFPGAN — much slower per frame)",
195
+ value=False,
196
+ )
197
+ fv_btn = gr.Button("Swap Faces in Video", variant="primary")
198
+ with gr.Column(scale=1):
199
+ fv_output = gr.Video(label="Result Video")
200
+ fv_status = gr.Textbox(label="Status", interactive=False)
201
+
202
+ fv_btn.click(
203
+ face_swap_video,
204
+ inputs=[fv_source, fv_target, fv_enhance],
205
+ outputs=[fv_output, fv_status],
206
+ )
207
+
208
+ # ── Tab 4: Body Swap — Video ──────────────────────────────────────────
209
+ with gr.TabItem("Body Swap · Video"):
210
+ gr.Markdown(
211
+ "Applies body swap to every frame of the **target** video. "
212
+ "Max video length: **300 frames** (~10 s at 30 fps)."
213
+ )
214
+ with gr.Row():
215
+ with gr.Column(scale=1):
216
+ bv_source = gr.Image(label="Source Body Image", type="pil")
217
+ bv_target = gr.Video(label="Target Video")
218
+ bv_blend = gr.Slider(
219
+ minimum=0.1, maximum=1.0, value=0.85, step=0.05,
220
+ label="Blend Strength",
221
+ )
222
+ bv_btn = gr.Button("Swap Body in Video", variant="primary")
223
+ with gr.Column(scale=1):
224
+ bv_output = gr.Video(label="Result Video")
225
+ bv_status = gr.Textbox(label="Status", interactive=False)
226
+
227
+ bv_btn.click(
228
+ body_swap_video,
229
+ inputs=[bv_source, bv_target, bv_blend],
230
+ outputs=[bv_output, bv_status],
231
+ )
232
+
233
+ # ── Entry point ───────────────────────────────────────────────────────────────
234
+ if __name__ == "__main__":
235
+ demo.launch()
models/.gitkeep ADDED
File without changes
processors/__init__.py ADDED
File without changes
processors/body_swap.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Body swap processor.
3
+
4
+ Pipeline
5
+ --------
6
+ 1. Segment the person from both images with *rembg* (U²-Net).
7
+ 2. Estimate body pose landmarks with *MediaPipe Pose*.
8
+ 3. Compute an affine warp that maps the source torso keypoints onto the
9
+ target torso keypoints, so the body roughly aligns with the target pose.
10
+ 4. Blend the warped source body onto the target background using the
11
+ segmentation mask + Gaussian feathering. A Poisson seamless-clone pass
12
+ is attempted for photorealistic colour blending.
13
+ """
14
+
15
+ import cv2
16
+ import numpy as np
17
+ from PIL import Image
18
+
19
+ from utils.image_utils import (
20
+ apply_color_correction,
21
+ feather_mask,
22
+ alpha_blend,
23
+ resize_to_max,
24
+ )
25
+
26
+
27
+ # ── Landmark indices used for rough torso alignment ───────────────────────────
28
+ # MediaPipe Pose: 11=left shoulder, 12=right shoulder,
29
+ # 23=left hip, 24=right hip
30
+ _TORSO_LANDMARKS = [11, 12, 23, 24]
31
+
32
+
33
+ class BodySwapper:
34
+ """
35
+ Replaces the body in *target_bgr* with the body from *source_bgr*.
36
+ """
37
+
38
+ def __init__(self):
39
+ import mediapipe as mp
40
+
41
+ self._mp_pose = mp.solutions.pose
42
+ self._pose = self._mp_pose.Pose(
43
+ static_image_mode=True,
44
+ model_complexity=2,
45
+ enable_segmentation=True,
46
+ min_detection_confidence=0.5,
47
+ )
48
+
49
+ # ── Private helpers ───────────────────────────────────────────────────────
50
+
51
+ def _segment(self, bgr: np.ndarray) -> np.ndarray:
52
+ """Return a uint8 single-channel person mask via rembg."""
53
+ from rembg import remove
54
+
55
+ pil = Image.fromarray(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB))
56
+ result = remove(pil, only_mask=True)
57
+ mask = np.array(result)
58
+ if mask.ndim == 3:
59
+ mask = mask[:, :, 0]
60
+ return mask
61
+
62
+ def _get_landmarks(self, bgr: np.ndarray):
63
+ """Return (x, y) pixel coords for all 33 pose landmarks, or None."""
64
+ rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
65
+ results = self._pose.process(rgb)
66
+ if not results.pose_landmarks:
67
+ return None
68
+ h, w = bgr.shape[:2]
69
+ return [
70
+ (int(lm.x * w), int(lm.y * h))
71
+ for lm in results.pose_landmarks.landmark
72
+ ]
73
+
74
+ @staticmethod
75
+ def _bbox(mask: np.ndarray):
76
+ """Bounding box (x1, y1, x2, y2) of non-zero mask region, or None."""
77
+ ys, xs = np.where(mask > 128)
78
+ if len(ys) == 0:
79
+ return None
80
+ return int(xs.min()), int(ys.min()), int(xs.max()), int(ys.max())
81
+
82
+ @staticmethod
83
+ def _torso_centroid(landmarks: list) -> tuple[float, float] | None:
84
+ """Return the mean (x, y) of the four torso landmarks."""
85
+ pts = [landmarks[i] for i in _TORSO_LANDMARKS if i < len(landmarks)]
86
+ if not pts:
87
+ return None
88
+ xs = [p[0] for p in pts]
89
+ ys = [p[1] for p in pts]
90
+ return float(np.mean(xs)), float(np.mean(ys))
91
+
92
+ # ── Public API ────────────────────────────────────────────────────────────
93
+
94
+ def swap(
95
+ self,
96
+ source_bgr: np.ndarray,
97
+ target_bgr: np.ndarray,
98
+ blend_strength: float = 0.85,
99
+ ) -> tuple[np.ndarray | None, str]:
100
+ """
101
+ Swap the source person's body into the target scene.
102
+
103
+ Returns:
104
+ (result_bgr, status_message)
105
+ """
106
+ try:
107
+ # ── 1. Segment both images ────────────────────────────────────────
108
+ src_mask = self._segment(source_bgr)
109
+ tgt_mask = self._segment(target_bgr)
110
+
111
+ src_bbox = self._bbox(src_mask)
112
+ tgt_bbox = self._bbox(tgt_mask)
113
+
114
+ if src_bbox is None:
115
+ return None, "No person detected in source image."
116
+ if tgt_bbox is None:
117
+ return None, "No person detected in target image."
118
+
119
+ sx1, sy1, sx2, sy2 = src_bbox
120
+ tx1, ty1, tx2, ty2 = tgt_bbox
121
+
122
+ # ── 2. Estimate poses ─────────────────────────────────────────────
123
+ src_lm = self._get_landmarks(source_bgr)
124
+ tgt_lm = self._get_landmarks(target_bgr)
125
+
126
+ # ── 3. Scale source to match target bounding-box size ─────────────
127
+ tgt_w, tgt_h = tx2 - tx1, ty2 - ty1
128
+
129
+ src_person = source_bgr[sy1:sy2, sx1:sx2]
130
+ src_mask_crop = src_mask[sy1:sy2, sx1:sx2]
131
+
132
+ src_resized = cv2.resize(src_person, (tgt_w, tgt_h), interpolation=cv2.INTER_LINEAR)
133
+ mask_resized = cv2.resize(src_mask_crop, (tgt_w, tgt_h), interpolation=cv2.INTER_LINEAR)
134
+
135
+ # ── 4. Optional pose-guided translation offset ────────────────────
136
+ offset_x, offset_y = 0, 0
137
+ if src_lm and tgt_lm:
138
+ src_c = self._torso_centroid(src_lm)
139
+ tgt_c = self._torso_centroid(tgt_lm)
140
+ if src_c and tgt_c:
141
+ # Map source centroid into the cropped-then-resized space
142
+ scale_x = tgt_w / max(sx2 - sx1, 1)
143
+ scale_y = tgt_h / max(sy2 - sy1, 1)
144
+ src_cx_resized = (src_c[0] - sx1) * scale_x
145
+ src_cy_resized = (src_c[1] - sy1) * scale_y
146
+ # Offset in target coordinates
147
+ tgt_cx_in_roi = tgt_c[0] - tx1
148
+ tgt_cy_in_roi = tgt_c[1] - ty1
149
+ offset_x = int(tgt_cx_in_roi - src_cx_resized)
150
+ offset_y = int(tgt_cy_in_roi - src_cy_resized)
151
+
152
+ # ── 5. Build a full-canvas composite ─────────────────────────────
153
+ h_tgt, w_tgt = target_bgr.shape[:2]
154
+ canvas_fg = np.zeros_like(target_bgr)
155
+ canvas_mask = np.zeros((h_tgt, w_tgt), dtype=np.uint8)
156
+
157
+ # Destination rectangle after applying offset
158
+ dst_x1 = np.clip(tx1 + offset_x, 0, w_tgt)
159
+ dst_y1 = np.clip(ty1 + offset_y, 0, h_tgt)
160
+ dst_x2 = np.clip(tx1 + offset_x + tgt_w, 0, w_tgt)
161
+ dst_y2 = np.clip(ty1 + offset_y + tgt_h, 0, h_tgt)
162
+
163
+ # Corresponding source slice
164
+ src_x1 = dst_x1 - (tx1 + offset_x)
165
+ src_y1 = dst_y1 - (ty1 + offset_y)
166
+ src_x2 = src_x1 + (dst_x2 - dst_x1)
167
+ src_y2 = src_y1 + (dst_y2 - dst_y1)
168
+
169
+ if dst_x2 <= dst_x1 or dst_y2 <= dst_y1:
170
+ return None, "Pose offset moved source body completely out of frame."
171
+
172
+ canvas_fg[dst_y1:dst_y2, dst_x1:dst_x2] = src_resized[src_y1:src_y2, src_x1:src_x2]
173
+ canvas_mask[dst_y1:dst_y2, dst_x1:dst_x2] = mask_resized[src_y1:src_y2, src_x1:src_x2]
174
+
175
+ # ── 6. Color-correct source to match target's region ──────────────
176
+ canvas_fg = apply_color_correction(canvas_fg, target_bgr, canvas_mask)
177
+
178
+ # ── 7. Feather the mask and alpha-blend ───────────────────────────
179
+ soft_mask = feather_mask(canvas_mask, blur_radius=21)
180
+ soft_mask = (soft_mask.astype(float) * blend_strength).clip(0, 255).astype(np.uint8)
181
+
182
+ result = alpha_blend(canvas_fg, target_bgr, soft_mask)
183
+
184
+ # ── 8. Seamless clone for photorealistic blending (best-effort) ───
185
+ try:
186
+ center_x = int((dst_x1 + dst_x2) / 2)
187
+ center_y = int((dst_y1 + dst_y2) / 2)
188
+ # seamlessClone requires a clean binary mask
189
+ sc_mask = (canvas_mask > 10).astype(np.uint8) * 255
190
+ result = cv2.seamlessClone(
191
+ canvas_fg, target_bgr, sc_mask,
192
+ (center_x, center_y), cv2.NORMAL_CLONE,
193
+ )
194
+ except Exception as e:
195
+ print(f"[BodySwapper] seamlessClone skipped: {e}")
196
+
197
+ return result, "Body swap completed successfully."
198
+
199
+ except Exception as exc:
200
+ return None, f"Body swap error: {exc}"
processors/face_swap.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Face swap processor using InsightFace (inswapper_128) with optional
3
+ GFPGAN face enhancement.
4
+
5
+ Model weights are downloaded automatically on first use and cached
6
+ in the local `models/` directory.
7
+ """
8
+
9
+ import os
10
+ import cv2
11
+ import numpy as np
12
+ import requests
13
+ from pathlib import Path
14
+
15
+ # ── Model paths ───────────────────────────────────────────────────────────────
16
+ MODELS_DIR = Path(__file__).parent.parent / "models"
17
+ MODELS_DIR.mkdir(exist_ok=True)
18
+
19
+ INSWAPPER_PATH = MODELS_DIR / "inswapper_128.onnx"
20
+ INSWAPPER_URL = (
21
+ "https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx"
22
+ )
23
+
24
+
25
+ # ── Helpers ───────────────────────────────────────────────────────────────────
26
+
27
+ def _download_file(url: str, dest: Path) -> None:
28
+ """Stream-download a file, showing a simple progress indicator."""
29
+ if dest.exists():
30
+ return
31
+ print(f"[FaceSwapper] Downloading {dest.name} …")
32
+ response = requests.get(url, stream=True, timeout=120)
33
+ response.raise_for_status()
34
+ with open(dest, "wb") as f:
35
+ for chunk in response.iter_content(chunk_size=65536):
36
+ f.write(chunk)
37
+ print(f"[FaceSwapper] Saved to {dest}")
38
+
39
+
40
+ # ── Main class ────────────────────────────────────────────────────────────────
41
+
42
+ class FaceSwapper:
43
+ """
44
+ Swaps the dominant face from a source image onto every detected face in
45
+ the target image. Optionally runs GFPGAN to restore/enhance face quality.
46
+ """
47
+
48
+ def __init__(self):
49
+ self._app = None # InsightFace FaceAnalysis
50
+ self._swapper = None # inswapper ONNX model
51
+ self._enhancer = None # GFPGAN (lazy)
52
+ self._ready = False
53
+
54
+ # ── Lazy initialisation ───────────────────────────────────────────────────
55
+
56
+ def _init(self):
57
+ if self._ready:
58
+ return
59
+
60
+ import insightface
61
+ from insightface.app import FaceAnalysis
62
+
63
+ # Face analysis (buffalo_l auto-downloads on first run)
64
+ self._app = FaceAnalysis(
65
+ name="buffalo_l",
66
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
67
+ )
68
+ self._app.prepare(ctx_id=0, det_size=(640, 640))
69
+
70
+ # inswapper model
71
+ _download_file(INSWAPPER_URL, INSWAPPER_PATH)
72
+ self._swapper = insightface.model_zoo.get_model(
73
+ str(INSWAPPER_PATH),
74
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
75
+ )
76
+
77
+ self._ready = True
78
+
79
+ def _get_enhancer(self):
80
+ """Lazy-load GFPGAN enhancer."""
81
+ if self._enhancer is None:
82
+ from gfpgan import GFPGANer
83
+
84
+ self._enhancer = GFPGANer(
85
+ model_path=(
86
+ "https://github.com/TencentARC/GFPGAN/releases/download/"
87
+ "v1.3.0/GFPGANv1.4.pth"
88
+ ),
89
+ upscale=1,
90
+ arch="clean",
91
+ channel_multiplier=2,
92
+ bg_upsampler=None,
93
+ )
94
+ return self._enhancer
95
+
96
+ # ── Public API ────────────────────────────────────────────────────────────
97
+
98
+ def swap(
99
+ self,
100
+ source_bgr: np.ndarray,
101
+ target_bgr: np.ndarray,
102
+ enhance: bool = False,
103
+ ) -> tuple[np.ndarray | None, str]:
104
+ """
105
+ Swap the first detected face in *source_bgr* onto every face in
106
+ *target_bgr*.
107
+
108
+ Returns:
109
+ (result_bgr, status_message)
110
+ """
111
+ self._init()
112
+
113
+ try:
114
+ source_faces = self._app.get(source_bgr)
115
+ target_faces = self._app.get(target_bgr)
116
+
117
+ if not source_faces:
118
+ return None, "No face detected in source image."
119
+ if not target_faces:
120
+ return None, "No face detected in target image."
121
+
122
+ source_face = source_faces[0]
123
+ result = target_bgr.copy()
124
+
125
+ for tgt_face in target_faces:
126
+ result = self._swapper.get(
127
+ result, tgt_face, source_face, paste_back=True
128
+ )
129
+
130
+ if enhance:
131
+ try:
132
+ _, _, result = self._get_enhancer().enhance(
133
+ result,
134
+ has_aligned=False,
135
+ only_center_face=False,
136
+ paste_back=True,
137
+ )
138
+ except Exception as e:
139
+ print(f"[FaceSwapper] Enhancement skipped: {e}")
140
+
141
+ return result, f"Swapped {len(target_faces)} face(s) successfully."
142
+
143
+ except Exception as exc:
144
+ return None, f"Face swap error: {exc}"
processors/video_processor.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Video processor — extracts frames from an input video, applies face or body
3
+ swap to each frame, then re-encodes the result with FFmpeg (audio preserved).
4
+
5
+ A hard cap of MAX_FRAMES is enforced to keep processing times reasonable on
6
+ free GPU tiers.
7
+ """
8
+
9
+ import cv2
10
+ import os
11
+ import tempfile
12
+ import numpy as np
13
+ from pathlib import Path
14
+
15
+ MAX_FRAMES = 300 # ~10 s at 30 fps — raise for paid tiers
16
+
17
+
18
+ class VideoProcessor:
19
+ def __init__(
20
+ self,
21
+ face_swapper=None,
22
+ body_swapper=None,
23
+ ):
24
+ self.face_swapper = face_swapper
25
+ self.body_swapper = body_swapper
26
+
27
+ # ── Public API ────────────────────────────────────────────────────────────
28
+
29
+ def process_video(
30
+ self,
31
+ source_bgr: np.ndarray,
32
+ video_path: str,
33
+ mode: str = "face", # "face" | "body"
34
+ enhance: bool = False,
35
+ blend_strength: float = 0.85,
36
+ progress=None,
37
+ ) -> tuple[str | None, str]:
38
+ """
39
+ Process every frame of *video_path*, applying the selected swap mode.
40
+
41
+ Returns:
42
+ (output_path, status_message)
43
+ """
44
+ cap = cv2.VideoCapture(video_path)
45
+ if not cap.isOpened():
46
+ return None, "Could not open video file."
47
+
48
+ fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
49
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
50
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
51
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
52
+
53
+ if total_frames > MAX_FRAMES:
54
+ cap.release()
55
+ return None, (
56
+ f"Video has {total_frames} frames — maximum allowed is "
57
+ f"{MAX_FRAMES} (~{MAX_FRAMES / fps:.0f} s at {fps:.0f} fps). "
58
+ "Please trim the video and try again."
59
+ )
60
+
61
+ # Temp file for raw processed frames (mp4v codec)
62
+ raw_out_path = tempfile.mktemp(suffix="_raw.mp4")
63
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
64
+ writer = cv2.VideoWriter(raw_out_path, fourcc, fps, (width, height))
65
+
66
+ frame_idx = 0
67
+ processed = 0
68
+ errors = 0
69
+
70
+ while True:
71
+ ret, frame = cap.read()
72
+ if not ret:
73
+ break
74
+
75
+ if progress and total_frames > 0:
76
+ progress(
77
+ frame_idx / total_frames,
78
+ f"Processing frame {frame_idx + 1} / {total_frames}",
79
+ )
80
+
81
+ result_frame = self._process_frame(
82
+ source_bgr, frame, mode, enhance, blend_strength
83
+ )
84
+
85
+ if result_frame is not None:
86
+ writer.write(result_frame)
87
+ processed += 1
88
+ else:
89
+ writer.write(frame) # keep original on failure
90
+ errors += 1
91
+
92
+ frame_idx += 1
93
+
94
+ cap.release()
95
+ writer.release()
96
+
97
+ # Re-encode with H.264 and merge original audio via FFmpeg
98
+ final_path = self._ffmpeg_encode(video_path, raw_out_path)
99
+
100
+ # Clean up raw file
101
+ try:
102
+ os.unlink(raw_out_path)
103
+ except OSError:
104
+ pass
105
+
106
+ status = (
107
+ f"Done — {processed}/{frame_idx} frames processed"
108
+ + (f" ({errors} skipped)" if errors else "")
109
+ + "."
110
+ )
111
+ return final_path, status
112
+
113
+ # ── Internal helpers ──────────────────────────────────────────────────────
114
+
115
+ def _process_frame(
116
+ self,
117
+ source_bgr: np.ndarray,
118
+ frame: np.ndarray,
119
+ mode: str,
120
+ enhance: bool,
121
+ blend_strength: float,
122
+ ) -> np.ndarray | None:
123
+ try:
124
+ if mode == "face" and self.face_swapper:
125
+ result, _ = self.face_swapper.swap(source_bgr, frame, enhance=enhance)
126
+ return result
127
+ elif mode == "body" and self.body_swapper:
128
+ result, _ = self.body_swapper.swap(
129
+ source_bgr, frame, blend_strength=blend_strength
130
+ )
131
+ return result
132
+ except Exception as e:
133
+ print(f"[VideoProcessor] Frame error: {e}")
134
+ return None
135
+
136
+ @staticmethod
137
+ def _ffmpeg_encode(original_video_path: str, processed_raw_path: str) -> str:
138
+ """
139
+ Re-encode processed frames as H.264 and copy the original audio track.
140
+ Falls back to the raw file if FFmpeg is unavailable.
141
+ """
142
+ final_path = tempfile.mktemp(suffix="_output.mp4")
143
+ try:
144
+ import ffmpeg
145
+
146
+ video_stream = ffmpeg.input(processed_raw_path).video
147
+ audio_stream = ffmpeg.input(original_video_path).audio
148
+
149
+ (
150
+ ffmpeg.output(
151
+ video_stream,
152
+ audio_stream,
153
+ final_path,
154
+ vcodec="libx264",
155
+ crf=23,
156
+ preset="fast",
157
+ acodec="aac",
158
+ audio_bitrate="192k",
159
+ )
160
+ .overwrite_output()
161
+ .run(quiet=True)
162
+ )
163
+ return final_path
164
+
165
+ except Exception as e:
166
+ print(f"[VideoProcessor] FFmpeg re-encode failed ({e}), returning raw file.")
167
+ return processed_raw_path
requirements.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Core UI & GPU
2
+ gradio>=4.44.0
3
+ spaces>=0.19.0
4
+
5
+ # Deep Learning
6
+ torch>=2.1.0
7
+ torchvision>=0.16.0
8
+
9
+ # Face Swap
10
+ insightface>=0.7.3
11
+ onnxruntime-gpu>=1.16.0
12
+
13
+ # Face Enhancement
14
+ gfpgan>=1.3.8
15
+ facexlib>=0.3.0
16
+ realesrgan>=0.3.0
17
+ basicsr>=1.4.2
18
+
19
+ # Body Segmentation
20
+ rembg>=2.0.50
21
+
22
+ # Pose Estimation
23
+ mediapipe>=0.10.0
24
+
25
+ # Image / Video Processing
26
+ opencv-python-headless>=4.8.0
27
+ Pillow>=10.0.0
28
+ numpy>=1.24.0
29
+ scikit-image>=0.21.0
30
+ ffmpeg-python>=0.2.0
31
+
32
+ # Utilities
33
+ requests>=2.31.0
34
+ huggingface-hub>=0.19.0
utils/__init__.py ADDED
File without changes
utils/image_utils.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+
6
+ def pil_to_bgr(pil_img: Image.Image) -> np.ndarray:
7
+ """Convert PIL RGB image to OpenCV BGR numpy array."""
8
+ return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
9
+
10
+
11
+ def bgr_to_pil(bgr_img: np.ndarray) -> Image.Image:
12
+ """Convert OpenCV BGR numpy array to PIL RGB image."""
13
+ return Image.fromarray(cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB))
14
+
15
+
16
+ def resize_to_max(image: np.ndarray, max_size: int = 1280) -> np.ndarray:
17
+ """Downscale image so its longest side does not exceed max_size."""
18
+ h, w = image.shape[:2]
19
+ if max(h, w) <= max_size:
20
+ return image
21
+ scale = max_size / max(h, w)
22
+ return cv2.resize(image, (int(w * scale), int(h * scale)), interpolation=cv2.INTER_AREA)
23
+
24
+
25
+ def apply_color_correction(source: np.ndarray, target: np.ndarray, mask: np.ndarray) -> np.ndarray:
26
+ """
27
+ Shift source pixel statistics (mean/std per LAB channel) inside the masked
28
+ region to match those of the target, improving blending realism.
29
+ """
30
+ source_lab = cv2.cvtColor(source, cv2.COLOR_BGR2LAB).astype(float)
31
+ target_lab = cv2.cvtColor(target, cv2.COLOR_BGR2LAB).astype(float)
32
+
33
+ mask_bool = mask > 128
34
+
35
+ for ch in range(3):
36
+ src_vals = source_lab[:, :, ch][mask_bool]
37
+ tgt_vals = target_lab[:, :, ch][mask_bool]
38
+
39
+ src_mean, src_std = src_vals.mean(), src_vals.std()
40
+ tgt_mean, tgt_std = tgt_vals.mean(), tgt_vals.std()
41
+
42
+ if src_std > 1e-6:
43
+ source_lab[:, :, ch][mask_bool] = (
44
+ (src_vals - src_mean) * (tgt_std / src_std) + tgt_mean
45
+ )
46
+
47
+ source_lab = np.clip(source_lab, 0, 255).astype(np.uint8)
48
+ return cv2.cvtColor(source_lab, cv2.COLOR_LAB2BGR)
49
+
50
+
51
+ def feather_mask(mask: np.ndarray, blur_radius: int = 15) -> np.ndarray:
52
+ """Apply Gaussian blur to soften mask edges."""
53
+ if blur_radius % 2 == 0:
54
+ blur_radius += 1
55
+ return cv2.GaussianBlur(mask, (blur_radius, blur_radius), 0)
56
+
57
+
58
+ def alpha_blend(foreground: np.ndarray, background: np.ndarray, mask: np.ndarray) -> np.ndarray:
59
+ """
60
+ Blend foreground onto background using a soft mask.
61
+
62
+ Args:
63
+ foreground: BGR image (same size as background).
64
+ background: BGR image.
65
+ mask: Single-channel uint8 mask (0-255).
66
+
67
+ Returns:
68
+ Blended BGR image.
69
+ """
70
+ alpha = mask.astype(float) / 255.0
71
+ alpha_3ch = np.stack([alpha] * 3, axis=-1)
72
+ blended = (foreground.astype(float) * alpha_3ch +
73
+ background.astype(float) * (1.0 - alpha_3ch))
74
+ return np.clip(blended, 0, 255).astype(np.uint8)