Spaces:
Running
Running
Update run.py
Browse files
run.py
CHANGED
|
@@ -2,87 +2,89 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from insightface.app import FaceAnalysis
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Initialisation
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 10 |
-
except Exception as e:
|
| 11 |
-
print(f"ERREUR initialisation : {str(e)}")
|
| 12 |
-
face_app = None
|
| 13 |
|
| 14 |
-
def
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
target = np.array(target_img, dtype=np.uint8)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
result[tgt_face[1]:tgt_face[3], tgt_face[0]:tgt_face[2]] = \
|
| 42 |
-
cv2.resize(source[src_face[1]:src_face[3], src_face[0]:src_face[2]],
|
| 43 |
-
(tgt_face[2]-tgt_face[0], tgt_face[3]-tgt_face[1]))
|
| 44 |
|
| 45 |
-
return result
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"ERREUR
|
| 48 |
-
# Retourne l'image cible avec message d'erreur
|
| 49 |
-
if target_img is not None:
|
| 50 |
-
return target_img
|
| 51 |
return None
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
*Échange de visages stable et sécurisé*
|
| 58 |
-
""")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
btn.click(
|
| 77 |
-
fn=
|
| 78 |
-
inputs=[
|
| 79 |
-
outputs=
|
| 80 |
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
app.launch(
|
| 84 |
-
server_name="0.0.0.0",
|
| 85 |
-
server_port=7860,
|
| 86 |
-
show_error=True,
|
| 87 |
-
debug=True
|
| 88 |
-
)
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from insightface.app import FaceAnalysis
|
| 5 |
+
import tempfile
|
| 6 |
+
from moviepy.editor import VideoFileClip
|
| 7 |
|
| 8 |
+
# Initialisation du modèle
|
| 9 |
+
face_swapper = FaceAnalysis(name="buffalo_l")
|
| 10 |
+
face_swapper.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def process_video(source_img, target_video):
|
| 13 |
try:
|
| 14 |
+
# Fichiers temporaires
|
| 15 |
+
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 16 |
|
| 17 |
+
# Traitement de la vidéo
|
| 18 |
+
source_face = face_swapper.get(np.array(source_img))[0]
|
|
|
|
| 19 |
|
| 20 |
+
cap = cv2.VideoCapture(target_video)
|
| 21 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 22 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 23 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 24 |
+
|
| 25 |
+
out = cv2.VideoWriter(temp_output.name,
|
| 26 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
| 27 |
+
fps,
|
| 28 |
+
(frame_width, frame_height))
|
| 29 |
|
| 30 |
+
while cap.isOpened():
|
| 31 |
+
ret, frame = cap.read()
|
| 32 |
+
if not ret:
|
| 33 |
+
break
|
| 34 |
+
|
| 35 |
+
target_faces = face_swapper.get(frame)
|
| 36 |
+
if target_faces:
|
| 37 |
+
frame = swap_face(source_face, target_faces[0], frame)
|
| 38 |
|
| 39 |
+
out.write(frame)
|
| 40 |
+
|
| 41 |
+
cap.release()
|
| 42 |
+
out.release()
|
| 43 |
|
| 44 |
+
return temp_output.name
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"ERREUR: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 48 |
return None
|
| 49 |
|
| 50 |
+
def swap_face(source_face, target_face, frame):
|
| 51 |
+
# Algorithme professionnel d'échange
|
| 52 |
+
src_bbox = source_face.bbox.astype(int)
|
| 53 |
+
tgt_bbox = target_face.bbox.astype(int)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Extraction et ajustement du visage source
|
| 56 |
+
src_face = cv2.resize(source_face.img[tgt_bbox[1]:tgt_bbox[3], tgt_bbox[0]:tgt_bbox[2]],
|
| 57 |
+
(tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
|
| 58 |
+
|
| 59 |
+
# Fusion réaliste
|
| 60 |
+
mask = np.zeros_like(src_face)
|
| 61 |
+
cv2.circle(mask,
|
| 62 |
+
(mask.shape[1]//2, mask.shape[0]//2),
|
| 63 |
+
min(mask.shape)//2,
|
| 64 |
+
(255,255,255), -1)
|
| 65 |
|
| 66 |
+
center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
|
| 67 |
+
output = cv2.seamlessClone(
|
| 68 |
+
src_face, frame, mask, center, cv2.NORMAL_CLONE)
|
| 69 |
|
| 70 |
+
return output
|
| 71 |
+
|
| 72 |
+
# Interface optimisée pour vidéo
|
| 73 |
+
with gr.Blocks() as app:
|
| 74 |
+
gr.Markdown("## 🎬 VideoFaceSwap Pro")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
src_img = gr.Image(label="Visage source", type="numpy")
|
| 78 |
+
tgt_video = gr.Video(label="Vidéo cible")
|
| 79 |
+
|
| 80 |
+
btn = gr.Button("Traiter la vidéo", variant="primary")
|
| 81 |
+
output_video = gr.Video(label="Résultat")
|
| 82 |
|
| 83 |
btn.click(
|
| 84 |
+
fn=process_video,
|
| 85 |
+
inputs=[src_img, tgt_video],
|
| 86 |
+
outputs=output_video
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|