assile commited on
Commit
0a14ee3
·
verified ·
1 Parent(s): 6289fd9

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +21 -10
run.py CHANGED
@@ -6,14 +6,21 @@ from insightface.app import FaceAnalysis
6
  import tempfile
7
  from moviepy.editor import VideoFileClip
8
 
9
- # Rediriger Matplotlib et Fontconfig vers des répertoires temporaires
 
 
 
 
 
10
  os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
11
  os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
12
  os.environ['FONTCONFIG_FILE'] = '/tmp/fontconfig/fonts.conf'
13
 
 
14
  os.makedirs(os.environ['MPLCONFIGDIR'], exist_ok=True)
15
  os.makedirs(os.environ['FONTCONFIG_PATH'], exist_ok=True)
16
 
 
17
  with open(os.environ['FONTCONFIG_FILE'], 'w') as f:
18
  f.write('''<?xml version="1.0"?>
19
  <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
@@ -24,18 +31,20 @@ with open(os.environ['FONTCONFIG_FILE'], 'w') as f:
24
  os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
25
  os.environ["ORT_DISABLE_CUDA"] = "1" # Désactiver CUDA si GPU indisponible
26
 
 
 
27
  def swap_face(source_face, target_face, frame):
28
  src_emb = source_face.normed_embedding
29
  tgt_bbox = target_face.bbox.astype(int)
30
- resized_face = cv2.resize(source_face.img, (tgt_bbox[2]-tgt_bbox[0], tgt_bbox[3]-tgt_bbox[1]))
31
-
32
  mask = np.zeros_like(resized_face)
33
- center = (mask.shape[1]//2, mask.shape[0]//2)
34
  radius = int(min(mask.shape) * 0.45)
35
- cv2.circle(mask, center, radius, (255,255,255), -1)
36
- mask = cv2.GaussianBlur(mask, (15,15), 5)
37
-
38
- center = ((tgt_bbox[0]+tgt_bbox[2])//2, (tgt_bbox[1]+tgt_bbox[3])//2)
39
  result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
40
  return result
41
 
@@ -57,7 +66,7 @@ def process_video(source_img, target_video):
57
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
58
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
 
60
- fourcc = cv2.VideoWriter_fourcc(*'avc1') # Codec compatible
61
  out = cv2.VideoWriter(temp_output.name, fourcc, fps, (frame_width, frame_height))
62
 
63
  while True:
@@ -88,7 +97,8 @@ def process_video(source_img, target_video):
88
  return None
89
 
90
 
91
- # Interface Gradio
 
92
  demo = gr.Interface(
93
  fn=process_video,
94
  inputs=[
@@ -101,5 +111,6 @@ demo = gr.Interface(
101
  allow_flagging="never"
102
  )
103
 
 
104
  if __name__ == "__main__":
105
  demo.launch()
 
6
  import tempfile
7
  from moviepy.editor import VideoFileClip
8
 
9
+ # ===== Configuration précoce pour éviter les erreurs =====
10
+
11
+ # Désactiver les warnings d'Albumentations (optionnel)
12
+ os.environ["NO_ALBUMENTATIONS_UPDATE"] = "1"
13
+
14
+ # Rediriger les chemins problématiques
15
  os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
16
  os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
17
  os.environ['FONTCONFIG_FILE'] = '/tmp/fontconfig/fonts.conf'
18
 
19
+ # Créer les répertoires temporaires nécessaires
20
  os.makedirs(os.environ['MPLCONFIGDIR'], exist_ok=True)
21
  os.makedirs(os.environ['FONTCONFIG_PATH'], exist_ok=True)
22
 
23
+ # Créer un fichier minimal de configuration Fontconfig si nécessaire
24
  with open(os.environ['FONTCONFIG_FILE'], 'w') as f:
25
  f.write('''<?xml version="1.0"?>
26
  <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
 
31
  os.environ['INSIGHTFACE_ROOT'] = '/tmp/.insightface'
32
  os.environ["ORT_DISABLE_CUDA"] = "1" # Désactiver CUDA si GPU indisponible
33
 
34
+ # ===== Fonction de swap =====
35
+
36
  def swap_face(source_face, target_face, frame):
37
  src_emb = source_face.normed_embedding
38
  tgt_bbox = target_face.bbox.astype(int)
39
+ resized_face = cv2.resize(source_face.img, (tgt_bbox[2] - tgt_bbox[0], tgt_bbox[3] - tgt_bbox[1]))
40
+
41
  mask = np.zeros_like(resized_face)
42
+ center = (mask.shape[1] // 2, mask.shape[0] // 2)
43
  radius = int(min(mask.shape) * 0.45)
44
+ cv2.circle(mask, center, radius, (255, 255, 255), -1)
45
+ mask = cv2.GaussianBlur(mask, (15, 15), 5)
46
+
47
+ center = ((tgt_bbox[0] + tgt_bbox[2]) // 2, (tgt_bbox[1] + tgt_bbox[3]) // 2)
48
  result = cv2.seamlessClone(resized_face, frame, mask, center, cv2.NORMAL_CLONE)
49
  return result
50
 
 
66
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
67
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
68
 
69
+ fourcc = cv2.VideoWriter_fourcc(*'avc1') # Codec compatible H.264
70
  out = cv2.VideoWriter(temp_output.name, fourcc, fps, (frame_width, frame_height))
71
 
72
  while True:
 
97
  return None
98
 
99
 
100
+ # ===== Interface Gradio =====
101
+
102
  demo = gr.Interface(
103
  fn=process_video,
104
  inputs=[
 
111
  allow_flagging="never"
112
  )
113
 
114
+
115
  if __name__ == "__main__":
116
  demo.launch()