Athagi commited on
Commit
3d9f45e
·
1 Parent(s): 2d2cd63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -36
app.py CHANGED
@@ -1,55 +1,70 @@
1
  import gradio as gr
 
2
  import cv2
 
3
  import numpy as np
4
- from insightface.app import FaceAnalysis
5
- from insightface.model_zoo.inswapper import INSwapper
6
  from gfpgan import GFPGANer
7
- import os
 
 
 
 
 
 
8
 
9
- # Setup paths
10
- swapper_model_path = "models/inswapper_128.onnx"
11
- gfpgan_model_path = "models/GFPGANv1.4.pth"
12
- gfpganer = GFPGANer(model_path=gfpgan_model_path, upscale=1, arch='clean', channel_multiplier=2)
 
 
 
 
13
 
14
- # Initialize FaceAnalysis and INSwapper
15
- face_app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
16
- face_app.prepare(ctx_id=0, det_size=(640, 640))
17
- swapper = INSwapper(model_file=swapper_model_path)
18
 
19
- def enhance_face(img_np):
20
- _, _, output = gfpganer.enhance(img_np, has_aligned=False, only_center_face=False, paste_back=True)
21
- return output
22
 
23
- def swap_faces(source_img, target_img):
24
- source = np.array(source_img)
25
- target = np.array(target_img)
 
26
 
27
- source_faces = face_app.get(source)
28
- target_faces = face_app.get(target)
29
 
30
- if len(source_faces) == 0 or len(target_faces) == 0:
31
- return target
 
 
32
 
33
- source_face = source_faces[0]
34
- for face in target_faces:
35
- target = swapper.get(target, face, source_face)
 
36
 
37
- target = enhance_face(target)
38
- return target
39
 
40
- def interface(source, target):
41
- result = swap_faces(source, target)
42
- return result
43
 
 
44
  iface = gr.Interface(
45
- fn=interface,
46
  inputs=[
47
- gr.Image(label="Source Face", type="numpy"),
48
- gr.Image(label="Target Image", type="numpy")
 
 
 
 
49
  ],
50
- outputs=gr.Image(label="Swapped Output"),
51
- title="Face Swap with Enhancement",
52
- description="Upload a source face and a target image. The app swaps the face and enhances it using GFPGAN."
53
  )
54
 
55
- iface.launch()
 
1
  import gradio as gr
2
+ import os
3
  import cv2
4
+ import torch
5
  import numpy as np
6
+ from PIL import Image
7
+
8
  from gfpgan import GFPGANer
9
+ from insightface.app import FaceAnalysis
10
+ from insightface.model_zoo import model_zoo
11
+
12
+ # Paths
13
+ MODEL_DIR = "model"
14
+ GFPGAN_MODEL_PATH = os.path.join(MODEL_DIR, "GFPGANv1.4.pth")
15
+ INSWAPPER_PATH = os.path.join(MODEL_DIR, "inswapper_128.onnx")
16
 
17
+ # Initialize GFPGAN
18
+ gfpganer = GFPGANer(
19
+ model_path=GFPGAN_MODEL_PATH,
20
+ upscale=2,
21
+ arch='clean',
22
+ channel_multiplier=2,
23
+ bg_upsampler=None
24
+ )
25
 
26
+ # Initialize InsightFace (for face detection + swapping)
27
+ faceapp = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
28
+ faceapp.prepare(ctx_id=0)
 
29
 
30
+ swapper = model_zoo.get_model(INSWAPPER_PATH, providers=['CPUExecutionProvider'])
 
 
31
 
32
+ # Function: Face Restoration + Swapping
33
+ def restore_and_swap(input_img: Image.Image, target_img: Image.Image):
34
+ input_np = np.array(input_img.convert("RGB"))
35
+ target_np = np.array(target_img.convert("RGB"))
36
 
37
+ # Step 1: Face restore
38
+ cropped_faces, restored_img = gfpganer.enhance(input_np, has_aligned=False, only_center_face=False, paste_back=True)
39
 
40
+ # Step 2: Detect face in target
41
+ target_faces = faceapp.get(target_np)
42
+ if len(target_faces) == 0:
43
+ return Image.fromarray(restored_img), "No face found in target image."
44
 
45
+ # Step 3: Detect face in restored image
46
+ source_faces = faceapp.get(restored_img)
47
+ if len(source_faces) == 0:
48
+ return Image.fromarray(restored_img), "No face found in restored image."
49
 
50
+ # Step 4: Face swap using first face
51
+ swapped = swapper.get(restored_img, source_faces[0], target_faces[0], paste_back=True)
52
 
53
+ return Image.fromarray(swapped), "Success! Face restored and swapped."
 
 
54
 
55
+ # Gradio Interface
56
  iface = gr.Interface(
57
+ fn=restore_and_swap,
58
  inputs=[
59
+ gr.Image(type="pil", label="Input Image (to restore + swap)"),
60
+ gr.Image(type="pil", label="Target Image (face to apply)")
61
+ ],
62
+ outputs=[
63
+ gr.Image(type="pil", label="Output Image"),
64
+ gr.Textbox(label="Status")
65
  ],
66
+ title="GFPGAN + InsightFace Face Swapper",
67
+ description="This app restores old faces using GFPGAN and swaps them with another face using InsightFace InSwapper."
 
68
  )
69
 
70
+ iface.launch()