sandhyasinha655 commited on
Commit
4b35c08
·
verified ·
1 Parent(s): d6f7d31

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +28 -0
  2. requirements.txt +6 -0
  3. run.py +42 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from run import run_faceswap
4
+
5
+ def process_face_swap(source_image, target_media):
6
+ source_path = "face.png"
7
+ target_path = "target"
8
+
9
+ source_image.save(source_path)
10
+ ext = os.path.splitext(target_media.name)[-1]
11
+ target_path += ext
12
+ target_media.save(target_path)
13
+
14
+ output_path = run_faceswap(source_path, target_path)
15
+ return output_path
16
+
17
+ iface = gr.Interface(
18
+ fn=process_face_swap,
19
+ inputs=[
20
+ gr.Image(label="Source Face (face.png)"),
21
+ gr.File(label="Target Image or Video (.jpg/.mp4)")
22
+ ],
23
+ outputs=gr.File(label="Swapped Output"),
24
+ title="Lightweight Roop Face Swap",
25
+ description="Upload face.png and target image/video. Output will be result.jpg or result.mp4"
26
+ )
27
+
28
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.25.0
2
+ insightface==0.7.3
3
+ onnxruntime==1.16.3
4
+ moviepy==1.0.3
5
+ numpy==1.23.5
6
+ opencv-python==4.10.0.82
run.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from moviepy.editor import VideoFileClip
3
+ import cv2
4
+ import numpy as np
5
+ import insightface
6
+
7
+ model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
8
+ model.prepare(ctx_id=0)
9
+
10
+ def swap_face(source_path, target_path, output_path):
11
+ source_img = cv2.imread(source_path)
12
+ faces = model.get(source_img)
13
+ if len(faces) == 0:
14
+ raise Exception("No face found in source.")
15
+ source_face = faces[0]
16
+
17
+ if target_path.endswith(".jpg"):
18
+ target_img = cv2.imread(target_path)
19
+ faces = model.get(target_img)
20
+ if len(faces) == 0:
21
+ raise Exception("No face found in target.")
22
+ target_img[faces[0].bbox.astype(int)[1]:faces[0].bbox.astype(int)[3],
23
+ faces[0].bbox.astype(int)[0]:faces[0].bbox.astype(int)[2]] = source_img[source_face.bbox.astype(int)[1]:source_face.bbox.astype(int)[3],
24
+ source_face.bbox.astype(int)[0]:source_face.bbox.astype(int)[2]]
25
+ cv2.imwrite(output_path, target_img)
26
+ else:
27
+ clip = VideoFileClip(target_path)
28
+ def process_frame(frame):
29
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
30
+ faces = model.get(frame_bgr)
31
+ if len(faces) > 0:
32
+ frame_bgr[faces[0].bbox.astype(int)[1]:faces[0].bbox.astype(int)[3],
33
+ faces[0].bbox.astype(int)[0]:faces[0].bbox.astype(int)[2]] = source_img[source_face.bbox.astype(int)[1]:source_face.bbox.astype(int)[3],
34
+ source_face.bbox.astype(int)[0]:source_face.bbox.astype(int)[2]]
35
+ return cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
36
+ new_clip = clip.fl_image(process_frame)
37
+ new_clip.write_videofile(output_path, audio=True)
38
+
39
+ def run_faceswap(source_path, target_path):
40
+ output_path = "result.jpg" if target_path.endswith(".jpg") else "result.mp4"
41
+ swap_face(source_path, target_path, output_path)
42
+ return output_path