tonyassi commited on
Commit
6cbc18d
·
verified ·
1 Parent(s): 9b91de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
app.py CHANGED
@@ -4,45 +4,38 @@ from PIL import Image
4
  import insightface
5
  from insightface.app import FaceAnalysis
6
 
7
- # ---- Load models once ----
8
  app = FaceAnalysis(name="buffalo_l")
9
  app.prepare(ctx_id=0, det_size=(640, 640))
10
- #swapper = insightface.model_zoo.get_model("./inswapper_128.onnx",download=False, download_zip=False)
11
- swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
12
 
13
- # ---- Gradio <4.39 monkey-patch for bool schema bug ----
14
- import gradio_client.utils as gu
15
- if hasattr(gu, "get_type"):
16
- _old_get_type = gu.get_type
17
- def _safe_get_type(schema):
18
- if isinstance(schema, bool): # <-- avoid "bool is not iterable"
19
- return "bool"
20
- return _old_get_type(schema)
21
- gu.get_type = _safe_get_type
22
 
23
- # ---- Main function ----
24
- def generate_image(src_img, dest_img):
25
- # src_img, dest_img arrive as numpy arrays
26
  src_faces = app.get(src_img)
27
  dest_faces = app.get(dest_img)
28
 
29
  res = dest_img.copy()
30
- for face in dest_faces:
31
- res = swapper.get(res, face, src_faces[0], paste_back=True)
32
 
33
  return Image.fromarray(np.uint8(res)).convert("RGB")
34
 
35
- # ---- Interface ----
36
- iface = gr.Interface(
37
- fn=generate_image,
38
- inputs=[gr.Image(label="Source Image"), gr.Image(label="Destination Image")],
39
- outputs=gr.Image(label="Final Image"),
40
- examples=[["Images/kim.jpg", "Images/marilyn.jpg"]],
41
- description="""
42
- <h1>Face Swap</h1>
43
- by <a href="https://www.tonyassi.com/" target="_blank" style="color:#97d8be;">Tony Assi</a><br>
44
- Try out <a href="https://huggingface.co/spaces/tonyassi/video-face-swap">Video Face Swap</a> ❤️
45
- """
46
- )
47
-
48
- iface.launch()
 
4
  import insightface
5
  from insightface.app import FaceAnalysis
6
 
7
+ # ---------- Load models once ----------
8
  app = FaceAnalysis(name="buffalo_l")
9
  app.prepare(ctx_id=0, det_size=(640, 640))
10
+ swapper = insightface.model_zoo.get_model("inswapper_128.onnx",
11
+ download=False, download_zip=False)
12
 
13
+ # ---------- HTML description ----------
14
+ description_html = """
15
+ <h1>Face Swap</h1>
16
+ <p>by <a href="https://www.tonyassi.com/" target="_blank" style="color:#97d8be;">Tony Assi</a></p>
17
+ <p>Try out <a href="https://huggingface.co/spaces/tonyassi/video-face-swap" target="_blank">Video Face Swap</a> ❤️</p>
18
+ """
 
 
 
19
 
20
+ # ---------- Swap logic ----------
21
+ def swap_faces(src_img, dest_img):
 
22
  src_faces = app.get(src_img)
23
  dest_faces = app.get(dest_img)
24
 
25
  res = dest_img.copy()
26
+ if src_faces and dest_faces:
27
+ res = swapper.get(res, dest_faces[0], src_faces[0], paste_back=True)
28
 
29
  return Image.fromarray(np.uint8(res)).convert("RGB")
30
 
31
+ # ---------- Interface ----------
32
+ gr.Interface(
33
+ fn=swap_faces,
34
+ inputs=[gr.Image(), gr.Image()],
35
+ outputs=gr.Image(),
36
+ description=description_html,
37
+ examples=[
38
+ ['./Images/kim.jpg', './Images/marilyn.jpg'],
39
+ ['./Images/friends.jpg', './Images/friends.jpg']
40
+ ],
41
+ ).launch()