assile commited on
Commit
b45ec36
·
verified ·
1 Parent(s): d6b02d3

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +22 -20
run.py CHANGED
@@ -6,35 +6,36 @@ from insightface.app import FaceAnalysis
6
  import tempfile
7
  from moviepy.editor import VideoFileClip
8
 
9
- # Désactiver les avertissements d'Albumentations
10
- os.environ["NO_ALBUMENTATIONS_UPDATE"] = "1"
11
 
12
- # Rediriger les chemins problématiques
13
  os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
14
  os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
15
 
16
  os.makedirs('/tmp/matplotlib', exist_ok=True)
17
  os.makedirs('/tmp/fontconfig', exist_ok=True)
18
 
19
- # Forcer InsightFace à utiliser un répertoire accessible
20
  os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
21
- os.environ["ORT_DISABLE_CUDA"] = "1" # Désactiver CUDA si GPU indisponible
 
 
22
 
23
  def swap_face(source_face, target_face, frame):
24
  src_emb = source_face.normed_embedding
25
  tgt_bbox = target_face.bbox.astype(int)
26
- resized_face = cv2.resize(source_face.img, (tgt_bbox[2] - tgt_bbox[0], tgt_bbox[3] - tgt_bbox[1]))
27
-
28
  mask = np.zeros_like(resized_face)
29
- center = (mask.shape[1] // 2, mask.shape[0] // 2)
30
  radius = int(min(mask.shape) * 0.45)
31
- cv2.circle(mask, center, radius, (255, 255, 255), -1)
32
- mask = cv2.GaussianBlur(mask, (15, 15), 5)
33
-
34
- center = ((tgt_bbox[0] + tgt_bbox[2]) // 2, (tgt_bbox[1] + tgt_bbox[3]) // 2)
35
  result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
36
  return result
37
 
 
38
  def process_video(source_img, target_video):
39
  try:
40
  face_app = FaceAnalysis(name="buffalo_l", root="/tmp/.insightface")
@@ -52,7 +53,7 @@ def process_video(source_img, target_video):
52
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
53
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
54
 
55
- fourcc = cv2.VideoWriter_fourcc(*'avc1') # Codec compatible H.264
56
  out = cv2.VideoWriter(temp_output.name, fourcc, fps, (frame_width, frame_height))
57
 
58
  while True:
@@ -69,8 +70,6 @@ def process_video(source_img, target_video):
69
  cap.release()
70
  out.release()
71
 
72
- print(f"Taille de la vidéo temporaire : {os.path.getsize(temp_output.name)} octets")
73
-
74
  # Réencodage final pour compatibilité Gradio
75
  clip = VideoFileClip(temp_output.name)
76
  final_path = tempfile.mktemp(suffix=".mp4")
@@ -79,21 +78,24 @@ def process_video(source_img, target_video):
79
  return final_path
80
 
81
  except Exception as e:
82
- print(f"Erreur lors du traitement : {str(e)}")
83
  return None
84
 
85
- # Interface Gradio
 
 
86
  demo = gr.Interface(
87
  fn=process_video,
88
  inputs=[
89
- gr.Image(label="Visage Source", type="numpy"),
90
- gr.Video(label="Vidéo Cible"),
91
  ],
92
- outputs=gr.Video(label="Vidéo Résultat"),
93
  title="🎬 FaceSwap Pro",
94
  description="Échangez des visages dans une vidéo.",
95
  allow_flagging="never"
96
  )
97
 
 
98
  if __name__ == "__main__":
99
  demo.launch(share=True)
 
6
  import tempfile
7
  from moviepy.editor import VideoFileClip
8
 
9
+ # ===== Configuration précoce pour éviter les erreurs =====
 
10
 
11
+ os.environ["NO_ALBUMENTATIONS_UPDATE"] = "1"
12
  os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
13
  os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
14
 
15
  os.makedirs('/tmp/matplotlib', exist_ok=True)
16
  os.makedirs('/tmp/fontconfig', exist_ok=True)
17
 
 
18
  os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
19
+ os.environ["ORT_DISABLE_CUDA"] = "1"
20
+
21
+ # ===== Fonction de swap =====
22
 
23
  def swap_face(source_face, target_face, frame):
24
  src_emb = source_face.normed_embedding
25
  tgt_bbox = target_face.bbox.astype(int)
26
+ resized_face = cv2.resize(source_face.img, (tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
27
+
28
  mask = np.zeros_like(resized_face)
29
+ center = (mask.shape[1]//2, mask.shape[0]//2)
30
  radius = int(min(mask.shape) * 0.45)
31
+ cv2.circle(mask, center, radius, (255,255,255), -1)
32
+ mask = cv2.GaussianBlur(mask, (15,15), 5)
33
+
34
+ center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
35
  result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
36
  return result
37
 
38
+
39
  def process_video(source_img, target_video):
40
  try:
41
  face_app = FaceAnalysis(name="buffalo_l", root="/tmp/.insightface")
 
53
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
54
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
55
 
56
+ fourcc = cv2.VideoWriter_fourcc(*'avc1')
57
  out = cv2.VideoWriter(temp_output.name, fourcc, fps, (frame_width, frame_height))
58
 
59
  while True:
 
70
  cap.release()
71
  out.release()
72
 
 
 
73
  # Réencodage final pour compatibilité Gradio
74
  clip = VideoFileClip(temp_output.name)
75
  final_path = tempfile.mktemp(suffix=".mp4")
 
78
  return final_path
79
 
80
  except Exception as e:
81
+ print(f"Erreur : {str(e)}")
82
  return None
83
 
84
+
85
+ # ===== Interface Gradio =====
86
+
87
  demo = gr.Interface(
88
  fn=process_video,
89
  inputs=[
90
+ gr.Image(label="Visage Source", type="numpy"), # ✅ Correct
91
+ gr.Video(label="Vidéo Cible"), # ✅ Correct
92
  ],
93
+ outputs=gr.Video(label="Vidéo Résultat"), # ✅ Correct
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)