Spaces:
Running
Running
Update run.py
Browse files
run.py
CHANGED
|
@@ -1,101 +1,18 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
-
from insightface.app import FaceAnalysis
|
| 6 |
-
import tempfile
|
| 7 |
-
from moviepy.editor import VideoFileClip
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Rediriger les chemins problématiques
|
| 14 |
-
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
|
| 15 |
-
os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
|
| 16 |
-
|
| 17 |
-
os.makedirs('/tmp/matplotlib', exist_ok=True)
|
| 18 |
-
os.makedirs('/tmp/fontconfig', exist_ok=True)
|
| 19 |
-
|
| 20 |
-
# Forcer InsightFace à utiliser un répertoire accessible
|
| 21 |
-
os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
|
| 22 |
-
os.environ["ORT_DISABLE_CUDA"] = "1" # Désactiver CUDA si GPU indisponible
|
| 23 |
-
|
| 24 |
-
def swap_face(source_face, target_face, frame):
|
| 25 |
-
src_emb = source_face.normed_embedding
|
| 26 |
-
tgt_bbox = target_face.bbox.astype(int)
|
| 27 |
-
resized_face = cv2.resize(source_face.img, (tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
|
| 28 |
-
|
| 29 |
-
mask = np.zeros_like(resized_face)
|
| 30 |
-
center = (mask.shape[1]//2, mask.shape[0]//2)
|
| 31 |
-
radius = int(min(mask.shape) * 0.45)
|
| 32 |
-
cv2.circle(mask, center, radius, (255,255,255), -1)
|
| 33 |
-
mask = cv2.GaussianBlur(mask, (15,15), 5)
|
| 34 |
-
|
| 35 |
-
center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
|
| 36 |
-
result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
|
| 37 |
-
return result
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def process_video(source_img, target_video):
|
| 41 |
-
try:
|
| 42 |
-
face_app = FaceAnalysis(name="buffalo_l", root="/tmp/.insightface")
|
| 43 |
-
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 44 |
-
|
| 45 |
-
source_faces = face_app.get(source_img)
|
| 46 |
-
if not source_faces:
|
| 47 |
-
raise ValueError("Aucun visage trouvé dans l'image source.")
|
| 48 |
-
source_face = source_faces[0]
|
| 49 |
-
|
| 50 |
-
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 51 |
-
|
| 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))
|
| 55 |
-
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 56 |
-
|
| 57 |
-
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 58 |
-
out = cv2.VideoWriter(temp_output.name, fourcc, fps, (frame_width, frame_height))
|
| 59 |
-
|
| 60 |
-
while True:
|
| 61 |
-
ret, frame = cap.read()
|
| 62 |
-
if not ret:
|
| 63 |
-
break
|
| 64 |
-
|
| 65 |
-
target_faces = face_app.get(frame)
|
| 66 |
-
for face in target_faces:
|
| 67 |
-
frame = swap_face(source_face, face, frame)
|
| 68 |
-
|
| 69 |
-
out.write(frame)
|
| 70 |
-
|
| 71 |
-
cap.release()
|
| 72 |
-
out.release()
|
| 73 |
-
|
| 74 |
-
# Réencodage final pour compatibilité Gradio
|
| 75 |
-
clip = VideoFileClip(temp_output.name)
|
| 76 |
-
final_path = tempfile.mktemp(suffix=".mp4")
|
| 77 |
-
clip.write_videofile(final_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
| 78 |
-
|
| 79 |
-
return final_path
|
| 80 |
-
|
| 81 |
-
except Exception as e:
|
| 82 |
-
print(f"Erreur : {str(e)}")
|
| 83 |
-
return None
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# Interface Gradio
|
| 87 |
demo = gr.Interface(
|
| 88 |
-
fn=
|
| 89 |
-
inputs=
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
outputs=gr.Video(label="Vidéo Résultat"),
|
| 94 |
-
title="🎬 FaceSwap Pro",
|
| 95 |
-
description="Échangez des visages dans une vidéo.",
|
| 96 |
allow_flagging="never"
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
| 100 |
if __name__ == "__main__":
|
| 101 |
demo.launch(share=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Fonction de test (remplacez-la par votre logique plus tard)
|
| 4 |
+
def greet(name):
|
| 5 |
+
return f"Bonjour {name} !"
|
| 6 |
|
| 7 |
+
# Interface Gradio minimale
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
demo = gr.Interface(
|
| 9 |
+
fn=greet,
|
| 10 |
+
inputs=gr.Textbox(label="Votre nom"),
|
| 11 |
+
outputs=gr.Textbox(label="Résultat"),
|
| 12 |
+
title="🧪 Test API Gradio",
|
| 13 |
+
description="Testez cette interface pour vérifier que l'API est bien trouvée.",
|
|
|
|
|
|
|
|
|
|
| 14 |
allow_flagging="never"
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
if __name__ == "__main__":
|
| 18 |
demo.launch(share=True)
|