sandhyasinha655 commited on
Commit
ba242bc
·
verified ·
1 Parent(s): 533cd1e

Delete run.py

Browse files
Files changed (1) hide show
  1. run.py +0 -52
run.py DELETED
@@ -1,52 +0,0 @@
1
- import os
2
- from moviepy.editor import VideoFileClip
3
- import insightface
4
- import cv2
5
- import numpy as np
6
-
7
- # Load the InsightFace model
8
- model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
9
- model.prepare(ctx_id=0)
10
-
11
- def swap_face(source_path, target_path, output_path):
12
- source_img = cv2.imread(source_path)
13
- source_faces = model.get(source_img)
14
- if len(source_faces) == 0:
15
- raise Exception("No face found in source.")
16
- source_face = source_faces[0]
17
- source_crop = source_img[
18
- int(source_face.bbox[1]):int(source_face.bbox[3]),
19
- int(source_face.bbox[0]):int(source_face.bbox[2])
20
- ]
21
-
22
- if target_path.endswith(".jpg"):
23
- target_img = cv2.imread(target_path)
24
- target_faces = model.get(target_img)
25
- if len(target_faces) == 0:
26
- raise Exception("No face found in target image.")
27
- target_face = target_faces[0]
28
- x1, y1, x2, y2 = map(int, target_face.bbox)
29
- resized_crop = cv2.resize(source_crop, (x2 - x1, y2 - y1))
30
- target_img[y1:y2, x1:x2] = resized_crop
31
- cv2.imwrite(output_path, target_img)
32
-
33
- else:
34
- clip = VideoFileClip(target_path)
35
-
36
- def process_frame(frame):
37
- frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
38
- faces = model.get(frame_bgr)
39
- if len(faces) > 0:
40
- face = faces[0]
41
- x1, y1, x2, y2 = map(int, face.bbox)
42
- resized_crop = cv2.resize(source_crop, (x2 - x1, y2 - y1))
43
- frame_bgr[y1:y2, x1:x2] = resized_crop
44
- return cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
45
-
46
- new_clip = clip.fl_image(process_frame)
47
- new_clip.write_videofile(output_path, audio=True)
48
-
49
- def run_faceswap(source_path, target_path):
50
- output_path = "result.jpg" if target_path.endswith(".jpg") else "result.mp4"
51
- swap_face(source_path, target_path, output_path)
52
- return output_path