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

Delete run.py

Browse files
Files changed (1) hide show
  1. run.py +0 -42
run.py DELETED
@@ -1,42 +0,0 @@
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