kaizma commited on
Commit
b0288c7
·
verified ·
1 Parent(s): 55bb217

Update face_swap.py

Browse files
Files changed (1) hide show
  1. face_swap.py +44 -45
face_swap.py CHANGED
@@ -1,46 +1,45 @@
1
- import numpy as np
2
- from PIL import Image
3
- import insightface
4
- from insightface.app import FaceAnalysis
5
-
6
- import os
7
- import requests
8
-
9
- MODEL_URL = "https://huggingface.co/kaizma/face-swap-inswapper/resolve/da20be1c8ba9b074d52c6a0540f8935d3e3605e5/inswapper_128.onnx"
10
- MODEL_PATH = "inswapper_128.onnx"
11
-
12
- # Download model if not exists
13
- if not os.path.exists(MODEL_PATH):
14
- print("Downloading model...")
15
- r = requests.get(MODEL_URL, stream=True)
16
- with open(MODEL_PATH, "wb") as f:
17
- for chunk in r.iter_content(chunk_size=8192):
18
- f.write(chunk)
19
- print("Model downloaded.")
20
-
21
-
22
- def swap_faces(src_pil, tgt_pil):
23
- # Convert PIL to numpy (BGR)
24
- src = np.array(src_pil)[:, :, ::-1].copy()
25
- tgt = np.array(tgt_pil)[:, :, ::-1].copy()
26
-
27
- # Initialize face analysis and swapper
28
- app = FaceAnalysis(name='buffalo_l')
29
- app.prepare(ctx_id=0, det_size=(640, 640))
30
- model_path = r"C:\Users\Dell\Desktop\Face Swap\inswapper_128.onnx"
31
- swapper = insightface.model_zoo.get_model(model_path)
32
-
33
- # Detect faces
34
- src_faces = app.get(src)
35
- tgt_faces = app.get(tgt)
36
- if len(src_faces) == 0 or len(tgt_faces) == 0:
37
- raise ValueError("No face detected in one of the images.")
38
-
39
- source_face = src_faces[0]
40
- res = tgt.copy()
41
- for face in tgt_faces:
42
- res = swapper.get(res, face, source_face, paste_back=True)
43
-
44
- # Convert back to PIL (RGB)
45
- res_pil = Image.fromarray(res[:, :, ::-1])
46
  return res_pil
 
1
+ import numpy as np
2
+ from PIL import Image
3
+ import insightface
4
+ from insightface.app import FaceAnalysis
5
+
6
+ import os
7
+ import requests
8
+
9
+ MODEL_URL = "https://huggingface.co/kaizma/face-swap-inswapper/resolve/da20be1c8ba9b074d52c6a0540f8935d3e3605e5/inswapper_128.onnx"
10
+ MODEL_PATH = "inswapper_128.onnx"
11
+
12
+ # Download model if not exists
13
+ if not os.path.exists(MODEL_PATH):
14
+ print("Downloading model...")
15
+ r = requests.get(MODEL_URL, stream=True)
16
+ with open(MODEL_PATH, "wb") as f:
17
+ for chunk in r.iter_content(chunk_size=8192):
18
+ f.write(chunk)
19
+ print("Model downloaded.")
20
+
21
+
22
+ def swap_faces(src_pil, tgt_pil):
23
+ # Convert PIL to numpy (BGR)
24
+ src = np.array(src_pil)[:, :, ::-1].copy()
25
+ tgt = np.array(tgt_pil)[:, :, ::-1].copy()
26
+
27
+ # Initialize face analysis and swapper
28
+ app = FaceAnalysis(name='buffalo_l')
29
+ app.prepare(ctx_id=0, det_size=(640, 640))
30
+ swapper = insightface.model_zoo.get_model("inswapper_128.onnx")
31
+
32
+ # Detect faces
33
+ src_faces = app.get(src)
34
+ tgt_faces = app.get(tgt)
35
+ if len(src_faces) == 0 or len(tgt_faces) == 0:
36
+ raise ValueError("No face detected in one of the images.")
37
+
38
+ source_face = src_faces[0]
39
+ res = tgt.copy()
40
+ for face in tgt_faces:
41
+ res = swapper.get(res, face, source_face, paste_back=True)
42
+
43
+ # Convert back to PIL (RGB)
44
+ res_pil = Image.fromarray(res[:, :, ::-1])
 
45
  return res_pil