Update run.py
Browse files
run.py
CHANGED
@@ -2,34 +2,32 @@ import os
|
|
2 |
from moviepy.editor import VideoFileClip
|
3 |
import insightface
|
4 |
import cv2
|
|
|
5 |
|
6 |
-
# Load InsightFace model
|
7 |
model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
8 |
model.prepare(ctx_id=0)
|
9 |
|
10 |
def swap_face(source_path, target_path, output_path):
|
11 |
source_img = cv2.imread(source_path)
|
12 |
-
|
13 |
-
if len(
|
14 |
raise Exception("No face found in source.")
|
15 |
-
source_face =
|
|
|
|
|
|
|
|
|
16 |
|
17 |
if target_path.endswith(".jpg"):
|
18 |
target_img = cv2.imread(target_path)
|
19 |
-
|
20 |
-
if len(
|
21 |
-
raise Exception("No face found in target.")
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
tx1, ty1, tx2, ty2 = faces[0].bbox.astype(int)
|
28 |
-
target_w, target_h = tx2 - tx1, ty2 - ty1
|
29 |
-
source_resized = cv2.resize(source_crop, (target_w, target_h))
|
30 |
-
|
31 |
-
# Paste on target
|
32 |
-
target_img[ty1:ty2, tx1:tx2] = source_resized
|
33 |
cv2.imwrite(output_path, target_img)
|
34 |
|
35 |
else:
|
@@ -39,8 +37,16 @@ def swap_face(source_path, target_path, output_path):
|
|
39 |
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
40 |
faces = model.get(frame_bgr)
|
41 |
if len(faces) > 0:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from moviepy.editor import VideoFileClip
|
3 |
import insightface
|
4 |
import cv2
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
# Load the InsightFace model
|
8 |
model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
9 |
model.prepare(ctx_id=0)
|
10 |
|
11 |
def swap_face(source_path, target_path, output_path):
|
12 |
source_img = cv2.imread(source_path)
|
13 |
+
source_faces = model.get(source_img)
|
14 |
+
if len(source_faces) == 0:
|
15 |
raise Exception("No face found in source.")
|
16 |
+
source_face = source_faces[0]
|
17 |
+
source_crop = source_img[
|
18 |
+
int(source_face.bbox[1]):int(source_face.bbox[3]),
|
19 |
+
int(source_face.bbox[0]):int(source_face.bbox[2])
|
20 |
+
]
|
21 |
|
22 |
if target_path.endswith(".jpg"):
|
23 |
target_img = cv2.imread(target_path)
|
24 |
+
target_faces = model.get(target_img)
|
25 |
+
if len(target_faces) == 0:
|
26 |
+
raise Exception("No face found in target image.")
|
27 |
+
target_face = target_faces[0]
|
28 |
+
x1, y1, x2, y2 = map(int, target_face.bbox)
|
29 |
+
resized_crop = cv2.resize(source_crop, (x2 - x1, y2 - y1))
|
30 |
+
target_img[y1:y2, x1:x2] = resized_crop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
cv2.imwrite(output_path, target_img)
|
32 |
|
33 |
else:
|
|
|
37 |
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
38 |
faces = model.get(frame_bgr)
|
39 |
if len(faces) > 0:
|
40 |
+
face = faces[0]
|
41 |
+
x1, y1, x2, y2 = map(int, face.bbox)
|
42 |
+
resized_crop = cv2.resize(source_crop, (x2 - x1, y2 - y1))
|
43 |
+
frame_bgr[y1:y2, x1:x2] = resized_crop
|
44 |
+
return cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
45 |
+
|
46 |
+
new_clip = clip.fl_image(process_frame)
|
47 |
+
new_clip.write_videofile(output_path, audio=True)
|
48 |
+
|
49 |
+
def run_faceswap(source_path, target_path):
|
50 |
+
output_path = "result.jpg" if target_path.endswith(".jpg") else "result.mp4"
|
51 |
+
swap_face(source_path, target_path, output_path)
|
52 |
+
return output_path
|