sandhyasinha655 commited on
Commit
06cd1c0
·
verified ·
1 Parent(s): a04bcf7

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +32 -0
  2. run.py +42 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import gradio as gr
4
+ from run import run_faceswap
5
+
6
+ def process_face_swap(source_image, target_file):
7
+ source_path = "face.png"
8
+ target_path = "target" + os.path.splitext(target_file.name)[-1]
9
+
10
+ # Save uploaded numpy image using OpenCV
11
+ cv2.imwrite(source_path, cv2.cvtColor(source_image, cv2.COLOR_RGB2BGR))
12
+
13
+ # Save uploaded target file
14
+ with open(target_path, "wb") as f:
15
+ f.write(target_file.read())
16
+
17
+ # Call face swap
18
+ output_path = run_faceswap(source_path, target_path)
19
+ return output_path
20
+
21
+ iface = gr.Interface(
22
+ fn=process_face_swap,
23
+ inputs=[
24
+ gr.Image(label="Source Face (face.png)", type="numpy"),
25
+ gr.File(label="Target Image or Video (.jpg/.mp4)")
26
+ ],
27
+ outputs=gr.File(label="Swapped Output"),
28
+ title="Lightweight Face Swap (User Logic)",
29
+ description="Upload face.png and target image/video. Output will be result.jpg or result.mp4"
30
+ )
31
+
32
+ iface.launch()
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