Spaces:
Running
Running
Update run.py
Browse files
run.py
CHANGED
|
@@ -3,41 +3,70 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from insightface.app import FaceAnalysis
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
def face_swap(source_img, target_img):
|
| 10 |
try:
|
| 11 |
-
# Conversion des images
|
| 12 |
source = np.array(source_img)
|
| 13 |
target = np.array(target_img)
|
| 14 |
|
| 15 |
# Détection des visages
|
| 16 |
-
source_faces =
|
| 17 |
-
target_faces =
|
| 18 |
|
| 19 |
-
if
|
| 20 |
raise ValueError("Aucun visage détecté dans une des images")
|
| 21 |
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
result = target.copy()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
x,y,w,h = face.bbox.astype(int)
|
| 27 |
-
result[y:y+h, x:x+w] = source_faces[i].bbox
|
| 28 |
|
|
|
|
|
|
|
| 29 |
return result
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
print(f"
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from insightface.app import FaceAnalysis
|
| 5 |
|
| 6 |
+
# Initialisation du détecteur de visages
|
| 7 |
+
face_app = FaceAnalysis(name="buffalo_l")
|
| 8 |
+
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 9 |
|
| 10 |
def face_swap(source_img, target_img):
|
| 11 |
try:
|
| 12 |
+
# Conversion des images Gradio en numpy arrays
|
| 13 |
source = np.array(source_img)
|
| 14 |
target = np.array(target_img)
|
| 15 |
|
| 16 |
# Détection des visages
|
| 17 |
+
source_faces = face_app.get(source)
|
| 18 |
+
target_faces = face_app.get(target)
|
| 19 |
|
| 20 |
+
if not source_faces or not target_faces:
|
| 21 |
raise ValueError("Aucun visage détecté dans une des images")
|
| 22 |
|
| 23 |
+
# Récupération du premier visage détecté
|
| 24 |
+
src_face = source_faces[0]
|
| 25 |
+
tgt_face = target_faces[0]
|
| 26 |
+
|
| 27 |
+
# Extraction des zones de visage (simplifié)
|
| 28 |
+
src_box = src_face.bbox.astype(int)
|
| 29 |
+
tgt_box = tgt_face.bbox.astype(int)
|
| 30 |
+
|
| 31 |
+
# Échange des visages
|
| 32 |
result = target.copy()
|
| 33 |
+
result[tgt_box[1]:tgt_box[3], tgt_box[0]:tgt_box[2]] = \
|
| 34 |
+
source[src_box[1]:src_box[3], src_box[0]:src_box[2]]
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Conversion BGR vers RGB pour Gradio
|
| 37 |
+
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 38 |
return result
|
| 39 |
+
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"ERREUR: {str(e)}")
|
| 42 |
+
# Retourne l'image originale en cas d'erreur
|
| 43 |
+
return cv2.cvtColor(target, cv2.COLOR_BGR2RGB)
|
| 44 |
|
| 45 |
+
# Interface Gradio améliorée
|
| 46 |
+
with gr.Blocks() as app:
|
| 47 |
+
gr.Markdown("## 🔄 FaceSwap Pro - Powered by InsightFace")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
src_img = gr.Image(label="Image Source", type="numpy")
|
| 52 |
+
tgt_img = gr.Image(label="Image Cible", type="numpy")
|
| 53 |
+
result_img = gr.Image(label="Résultat", interactive=False)
|
| 54 |
+
|
| 55 |
+
btn = gr.Button("Échanger les visages", variant="primary")
|
| 56 |
+
|
| 57 |
+
# Exemples intégrés
|
| 58 |
+
gr.Examples(
|
| 59 |
+
examples=[
|
| 60 |
+
["exemples/source1.jpg", "exemples/target1.jpg"],
|
| 61 |
+
["exemples/source2.jpg", "exemples/target2.jpg"]
|
| 62 |
+
],
|
| 63 |
+
inputs=[src_img, tgt_img],
|
| 64 |
+
outputs=result_img,
|
| 65 |
+
fn=face_swap,
|
| 66 |
+
cache_examples=True
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
btn.click(face_swap, inputs=[src_img, tgt_img], outputs=result_img)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|