LogicGoInfotechSpaces commited on
Commit
1957892
·
verified ·
1 Parent(s): 07ac4f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -1
app.py CHANGED
@@ -11,7 +11,7 @@ import logging
11
  import tempfile
12
  import sys
13
  from datetime import datetime,timedelta
14
-
15
  import insightface
16
  from insightface.app import FaceAnalysis
17
  from huggingface_hub import hf_hub_download
@@ -151,6 +151,46 @@ async def log_faceswap_hit(token: str, status: str = "success"):
151
  # --------------------- Face Swap Pipeline ---------------------
152
  swap_lock = threading.Lock()
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  def enhance_image_with_codeformer(rgb_img, temp_dir=None):
155
  if temp_dir is None:
156
  temp_dir = os.path.join(tempfile.gettempdir(), f"enhance_{uuid.uuid4().hex[:8]}")
@@ -370,6 +410,31 @@ def build_multi_faceswap_gradio():
370
  btn.click(process, [src, tgt], [out, error])
371
 
372
  return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
 
374
  def mandatory_enhancement(rgb_img):
375
  """
 
11
  import tempfile
12
  import sys
13
  from datetime import datetime,timedelta
14
+ import tempfile
15
  import insightface
16
  from insightface.app import FaceAnalysis
17
  from huggingface_hub import hf_hub_download
 
151
  # --------------------- Face Swap Pipeline ---------------------
152
  swap_lock = threading.Lock()
153
 
154
+ def swap_faces_on_video(source_img, video_file):
155
+ """
156
+ source_img: numpy RGB image
157
+ video_file: path to uploaded video
158
+ Returns path to swapped video
159
+ """
160
+ temp_dir = tempfile.mkdtemp(prefix="faceswap_video_")
161
+ input_path = video_file.name # Gradio gives a NamedTemporaryFile
162
+ output_path = os.path.join(temp_dir, f"swapped_{uuid.uuid4().hex}.mp4")
163
+
164
+ cap = cv2.VideoCapture(input_path)
165
+ fps = cap.get(cv2.CAP_PROP_FPS)
166
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
167
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
168
+
169
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
170
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
171
+
172
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
173
+ print(f"Processing {frame_count} frames...")
174
+
175
+ while True:
176
+ ret, frame = cap.read()
177
+ if not ret:
178
+ break
179
+
180
+ # Convert frame to RGB
181
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
182
+
183
+ # Apply existing functions
184
+ swapped_rgb = multi_face_swap(source_img, frame_rgb)
185
+ final_rgb = mandatory_enhancement(swapped_rgb)
186
+
187
+ # Convert back to BGR and write frame
188
+ out.write(cv2.cvtColor(final_rgb, cv2.COLOR_RGB2BGR))
189
+
190
+ cap.release()
191
+ out.release()
192
+ return
193
+
194
  def enhance_image_with_codeformer(rgb_img, temp_dir=None):
195
  if temp_dir is None:
196
  temp_dir = os.path.join(tempfile.gettempdir(), f"enhance_{uuid.uuid4().hex[:8]}")
 
410
  btn.click(process, [src, tgt], [out, error])
411
 
412
  return demo
413
+
414
+ # ------------------- Gradio Interface -------------------
415
+ def build_video_faceswap_gradio():
416
+ with gr.Blocks() as demo:
417
+ gr.Markdown("## 🎬 Video Face Swap")
418
+
419
+ with gr.Row():
420
+ src = gr.Image(type="numpy", label="Source Face Image")
421
+ vid = gr.File(label="Target Video (.mp4)")
422
+
423
+ out = gr.Video(label="Swapped Video")
424
+ error = gr.Textbox(label="Logs", interactive=False)
425
+
426
+ def process(src_img, video_file):
427
+ try:
428
+ swapped_video = swap_faces_on_video(src_img, video_file)
429
+ return swapped_video, ""
430
+ except Exception as e:
431
+ return None, str(e)
432
+
433
+ btn = gr.Button("Swap Faces in Video")
434
+ btn.click(process, [src, vid], [out, error])
435
+
436
+ return demo
437
+
438
 
439
  def mandatory_enhancement(rgb_img):
440
  """