sandhyasinha655 commited on
Commit
109a39e
·
verified ·
1 Parent(s): e2f55d6

Upload __init__.py

Browse files
Files changed (1) hide show
  1. roop/__init__.py +45 -1
roop/__init__.py CHANGED
@@ -1 +1,45 @@
1
- # This file contains the run_face_swap() function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import cv2
4
+ from moviepy.editor import VideoFileClip
5
+ from PIL import Image
6
+ from roop.core import run
7
+ from tqdm import tqdm
8
+
9
+ def run_face_swap(source_img, target_file):
10
+ filename = target_file.name
11
+
12
+ if filename.endswith(".jpg") or filename.endswith(".png"):
13
+ # Image swap (simplified)
14
+ os.makedirs("output", exist_ok=True)
15
+ output_path = "output/result.jpg"
16
+ os.system(f"python run.py --source {source_img} --target {filename} --output {output_path}")
17
+ return output_path
18
+
19
+ elif filename.endswith(".mp4"):
20
+ os.makedirs("frames_output", exist_ok=True)
21
+ os.makedirs("frames_input", exist_ok=True)
22
+ clip = VideoFileClip(filename)
23
+ fps = clip.fps
24
+ for i, frame in enumerate(clip.iter_frames()):
25
+ Image.fromarray(frame).save(f"frames_input/frame_{i:05d}.jpg")
26
+ import sys
27
+ sys.argv = ["run.py", "--source", source_img, "--target", "", "--output", ""]
28
+ for frame_file in sorted(os.listdir("frames_input")):
29
+ input_path = os.path.join("frames_input", frame_file)
30
+ output_path = os.path.join("frames_output", frame_file)
31
+ sys.argv[4] = input_path
32
+ sys.argv[6] = output_path
33
+ run()
34
+ frame_files = sorted(os.listdir("frames_output"))
35
+ frame_images = [cv2.imread(f"frames_output/{f}") for f in frame_files]
36
+ h, w, _ = frame_images[0].shape
37
+ out_path = "output/result.mp4"
38
+ writer = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
39
+ for img in frame_images:
40
+ writer.write(img)
41
+ writer.release()
42
+ return out_path
43
+
44
+ else:
45
+ raise ValueError("Unsupported file type")