Upload 3 files
Browse files- app.py +29 -0
- requirements.txt +6 -0
- run.py +43 -0
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
from run import run_faceswap
|
5 |
+
|
6 |
+
def process_face_swap(source_image, target_media):
|
7 |
+
source_path = "face.png"
|
8 |
+
target_path = "target"
|
9 |
+
|
10 |
+
source_image.save(source_path)
|
11 |
+
ext = os.path.splitext(target_media.name)[-1]
|
12 |
+
target_path += ext
|
13 |
+
target_media.save(target_path)
|
14 |
+
|
15 |
+
output_path = run_faceswap(source_path, target_path)
|
16 |
+
return output_path
|
17 |
+
|
18 |
+
iface = gr.Interface(
|
19 |
+
fn=process_face_swap,
|
20 |
+
inputs=[
|
21 |
+
gr.Image(label="Source Face (face.png)"),
|
22 |
+
gr.File(label="Target Image or Video (.jpg/.mp4)")
|
23 |
+
],
|
24 |
+
outputs=gr.File(label="Swapped Output"),
|
25 |
+
title="Lightweight Roop Face Swap",
|
26 |
+
description="Upload face.png and target image/video. Output will be result.jpg or result.mp4"
|
27 |
+
)
|
28 |
+
|
29 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
gradio==3.50.2
|
3 |
+
moviepy
|
4 |
+
opencv-python-headless
|
5 |
+
Pillow
|
6 |
+
tqdm
|
run.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
from moviepy.editor import VideoFileClip
|
5 |
+
import cv2
|
6 |
+
from PIL import Image
|
7 |
+
from tqdm import tqdm
|
8 |
+
|
9 |
+
def run_faceswap(source_path, target_path):
|
10 |
+
if target_path.endswith(".jpg") or target_path.endswith(".png"):
|
11 |
+
# Image face swap (mockup, actual logic should call Roop)
|
12 |
+
output_path = "result.jpg"
|
13 |
+
shutil.copy(target_path, output_path)
|
14 |
+
return output_path
|
15 |
+
|
16 |
+
elif target_path.endswith(".mp4"):
|
17 |
+
os.makedirs("frames_input", exist_ok=True)
|
18 |
+
os.makedirs("frames_output", exist_ok=True)
|
19 |
+
|
20 |
+
clip = VideoFileClip(target_path)
|
21 |
+
fps = clip.fps
|
22 |
+
|
23 |
+
for i, frame in enumerate(clip.iter_frames()):
|
24 |
+
Image.fromarray(frame).save(f"frames_input/frame_{i:05d}.jpg")
|
25 |
+
|
26 |
+
# Mock face swap on each frame (copy as is)
|
27 |
+
for frame_file in sorted(os.listdir("frames_input")):
|
28 |
+
shutil.copy(f"frames_input/{frame_file}", f"frames_output/{frame_file}")
|
29 |
+
|
30 |
+
frame_files = sorted(os.listdir("frames_output"))
|
31 |
+
frame_images = [cv2.imread(f"frames_output/{f}") for f in frame_files]
|
32 |
+
h, w, _ = frame_images[0].shape
|
33 |
+
out_path = "result.mp4"
|
34 |
+
video_writer = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
35 |
+
|
36 |
+
for img in frame_images:
|
37 |
+
video_writer.write(img)
|
38 |
+
video_writer.release()
|
39 |
+
|
40 |
+
return out_path
|
41 |
+
|
42 |
+
else:
|
43 |
+
raise ValueError("Unsupported file format")
|