Spaces:
Sleeping
Sleeping
Update run.py
Browse files
run.py
CHANGED
@@ -10,45 +10,30 @@ import shutil
|
|
10 |
os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
|
11 |
|
12 |
def swap_face(source_face, target_face, frame):
|
13 |
-
"""Remplace le visage cible par le visage source"""
|
14 |
src_emb = source_face.normed_embedding
|
15 |
tgt_bbox = target_face.bbox.astype(int)
|
16 |
-
|
17 |
-
# Obtenir le visage redimensionné
|
18 |
-
src_face_img = source_face.img
|
19 |
-
resized_face = cv2.resize(src_face_img, (tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
|
20 |
-
|
21 |
-
# Créer un masque doux
|
22 |
mask = np.zeros_like(resized_face)
|
23 |
center = (mask.shape[1]//2, mask.shape[0]//2)
|
24 |
radius = int(min(mask.shape) * 0.45)
|
25 |
cv2.circle(mask, center, radius, (255,255,255), -1)
|
26 |
mask = cv2.GaussianBlur(mask, (15,15), 5)
|
27 |
-
|
28 |
-
# Positionner le visage
|
29 |
center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
|
30 |
-
|
31 |
-
# Échange réaliste
|
32 |
result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
|
33 |
return result
|
34 |
|
35 |
-
|
36 |
def process_video(source_img, target_video):
|
37 |
try:
|
38 |
-
# Initialiser le modèle INSIDE la fonction
|
39 |
face_app = FaceAnalysis(name="buffalo_l", root="/tmp/.insightface")
|
40 |
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
41 |
|
42 |
-
# Charger le visage source
|
43 |
source_faces = face_app.get(source_img)
|
44 |
if not source_faces:
|
45 |
raise ValueError("Aucun visage trouvé dans l'image source.")
|
46 |
source_face = source_faces[0]
|
47 |
|
48 |
-
# Fichier temporaire pour sauvegarder la sortie
|
49 |
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
50 |
|
51 |
-
# Ouvrir la vidéo cible
|
52 |
cap = cv2.VideoCapture(target_video)
|
53 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
54 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
@@ -62,7 +47,6 @@ def process_video(source_img, target_video):
|
|
62 |
if not ret:
|
63 |
break
|
64 |
|
65 |
-
# Détecter les visages dans la frame
|
66 |
target_faces = face_app.get(frame)
|
67 |
for face in target_faces:
|
68 |
frame = swap_face(source_face, face, frame)
|
@@ -72,7 +56,6 @@ def process_video(source_img, target_video):
|
|
72 |
cap.release()
|
73 |
out.release()
|
74 |
|
75 |
-
# Copier vers un nouveau fichier stable
|
76 |
final_path = tempfile.mktemp(suffix=".mp4")
|
77 |
shutil.copy(temp_output.name, final_path)
|
78 |
|
@@ -82,11 +65,7 @@ def process_video(source_img, target_video):
|
|
82 |
print(f"Erreur lors du traitement : {str(e)}")
|
83 |
return None
|
84 |
|
85 |
-
|
86 |
# Interface Gradio
|
87 |
-
title = "🎬 FaceSwap Pro - Swap Video"
|
88 |
-
description = "Télécharge une image avec un visage et une vidéo. Le visage sera remplacé automatiquement."
|
89 |
-
|
90 |
demo = gr.Interface(
|
91 |
fn=process_video,
|
92 |
inputs=[
|
@@ -94,8 +73,8 @@ demo = gr.Interface(
|
|
94 |
gr.Video(label="Vidéo Cible"),
|
95 |
],
|
96 |
outputs=gr.Video(label="Vidéo Résultat"),
|
97 |
-
title=
|
98 |
-
description=
|
99 |
allow_flagging="never"
|
100 |
)
|
101 |
|
|
|
10 |
os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
|
11 |
|
12 |
def swap_face(source_face, target_face, frame):
|
|
|
13 |
src_emb = source_face.normed_embedding
|
14 |
tgt_bbox = target_face.bbox.astype(int)
|
15 |
+
resized_face = cv2.resize(source_face.img, (tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
|
|
|
|
|
|
|
|
|
|
|
16 |
mask = np.zeros_like(resized_face)
|
17 |
center = (mask.shape[1]//2, mask.shape[0]//2)
|
18 |
radius = int(min(mask.shape) * 0.45)
|
19 |
cv2.circle(mask, center, radius, (255,255,255), -1)
|
20 |
mask = cv2.GaussianBlur(mask, (15,15), 5)
|
|
|
|
|
21 |
center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
|
|
|
|
|
22 |
result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
|
23 |
return result
|
24 |
|
|
|
25 |
def process_video(source_img, target_video):
|
26 |
try:
|
|
|
27 |
face_app = FaceAnalysis(name="buffalo_l", root="/tmp/.insightface")
|
28 |
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
29 |
|
|
|
30 |
source_faces = face_app.get(source_img)
|
31 |
if not source_faces:
|
32 |
raise ValueError("Aucun visage trouvé dans l'image source.")
|
33 |
source_face = source_faces[0]
|
34 |
|
|
|
35 |
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
36 |
|
|
|
37 |
cap = cv2.VideoCapture(target_video)
|
38 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
39 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
47 |
if not ret:
|
48 |
break
|
49 |
|
|
|
50 |
target_faces = face_app.get(frame)
|
51 |
for face in target_faces:
|
52 |
frame = swap_face(source_face, face, frame)
|
|
|
56 |
cap.release()
|
57 |
out.release()
|
58 |
|
|
|
59 |
final_path = tempfile.mktemp(suffix=".mp4")
|
60 |
shutil.copy(temp_output.name, final_path)
|
61 |
|
|
|
65 |
print(f"Erreur lors du traitement : {str(e)}")
|
66 |
return None
|
67 |
|
|
|
68 |
# Interface Gradio
|
|
|
|
|
|
|
69 |
demo = gr.Interface(
|
70 |
fn=process_video,
|
71 |
inputs=[
|
|
|
73 |
gr.Video(label="Vidéo Cible"),
|
74 |
],
|
75 |
outputs=gr.Video(label="Vidéo Résultat"),
|
76 |
+
title="🎬 FaceSwap Pro",
|
77 |
+
description="Échangez des visages dans une vidéo.",
|
78 |
allow_flagging="never"
|
79 |
)
|
80 |
|