tonyassi commited on
Commit
b114524
·
verified ·
1 Parent(s): d579dbd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import os
4
+ import cv2
5
+ from PIL import Image, ImageDraw
6
+ import insightface
7
+ from insightface.app import FaceAnalysis
8
+
9
+ # Insightface model
10
+ app = FaceAnalysis(name='buffalo_l')
11
+ app.prepare(ctx_id=0, det_size=(640, 640))
12
+
13
+
14
+ def generate_image(src_img, dest_img):
15
+ # Convert to RGB
16
+ src_img = src_img.convert(mode='RGB')
17
+ dest_img = dest_img.convert(mode='RGB')
18
+
19
+ # Convert to array
20
+ src_img_arr = np.asarray(src_img)
21
+ dest_img_arr = np.asarray(dest_img)
22
+
23
+ # Face detection
24
+ src_faces = app.get(src_img_arr)
25
+ dest_faces = app.get(dest_img_arr)
26
+
27
+ # Initialize swapper
28
+ swapper = insightface.model_zoo.get_model('./inswapper_128.onnx', download=False, download_zip=False)
29
+ #swapper = insightface.model_zoo.get_model('./inswapper_128.onnx')
30
+
31
+
32
+ # Swap face
33
+ res = dest_img_arr.copy()
34
+ for face in dest_faces:
35
+ res = swapper.get(res, face, src_faces[0], paste_back=True)
36
+
37
+ # Convert to PIL image
38
+ final_image = Image.fromarray(np.uint8(res)).convert('RGB')
39
+
40
+ return final_image
41
+
42
+
43
+ iface = gr.Interface(fn=generate_image, inputs=[
44
+ gr.Image(type="pil", label="Source Image"),
45
+ gr.Image(type="pil", label="Destination Image")],
46
+ outputs=gr.Image(type="pil", label="Final Image"),
47
+ theme=gr.themes.Base(primary_hue=gr.themes.colors.teal, font=["helvetica"]),
48
+ examples=[["Images/kim.jpg", "Images/marilyn.jpg"]],
49
+ description=
50
+ """ # Face Swap
51
+ by <a href="https://www.tonyassi.com/" style="color: #97d8be;">Tony Assi</a>
52
+
53
+ Try out [Video Face Swap](https://huggingface.co/spaces/tonyassi/video-face-swap). Please ❤️ this Space.
54
+
55
+ """
56
+ )
57
+ iface.launch()