assile commited on
Commit
1b5bc6a
·
verified ·
1 Parent(s): 3baaad2

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +50 -21
run.py CHANGED
@@ -3,41 +3,70 @@ import cv2
3
  import numpy as np
4
  from insightface.app import FaceAnalysis
5
 
6
- app = FaceAnalysis()
7
- app.prepare(ctx_id=0, det_size=(640, 640))
 
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 = app.get(source)
17
- target_faces = app.get(target)
18
 
19
- if len(source_faces) == 0 or len(target_faces) == 0:
20
  raise ValueError("Aucun visage détecté dans une des images")
21
 
22
- # Échange simplifié (exemple)
 
 
 
 
 
 
 
 
23
  result = target.copy()
24
- for i, face in enumerate(target_faces):
25
- if i < len(source_faces):
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"Erreur : {str(e)}")
32
- return None
 
33
 
34
- interface = gr.Interface(
35
- fn=face_swap,
36
- inputs=[gr.Image(), gr.Image()],
37
- outputs=gr.Image(),
38
- title="FaceSwap Pro",
39
- description="Échange de visages avec InsightFace"
40
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
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)