002CyberTiger commited on
Commit
fb7d24e
·
verified ·
1 Parent(s): 574d090

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -4
app.py CHANGED
@@ -1,15 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
3
  import insightface
4
  from insightface.app import FaceAnalysis
5
  from huggingface_hub import hf_hub_download
 
6
  import spaces
7
 
8
  app = None
9
  swapper = None
 
10
 
11
  def load_models():
12
- global app, swapper
13
  if app is None:
14
  app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
15
  app.prepare(ctx_id=-1, det_size=(640, 640))
@@ -19,9 +33,21 @@ def load_models():
19
  filename="inswapper_128.onnx"
20
  )
21
  swapper = insightface.model_zoo.get_model(swapper_path, download=False)
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  @spaces.GPU
24
- def swap_faces(source_img, target_img):
25
  if source_img is None or target_img is None:
26
  return None
27
 
@@ -39,19 +65,31 @@ def swap_faces(source_img, target_img):
39
  source_face = sorted(source_faces, key=lambda x: x.bbox[0])[0]
40
  target_face = sorted(target_faces, key=lambda x: x.bbox[0])[0]
41
 
 
42
  result_bgr = swapper.get(target_bgr, target_face, source_face, paste_back=True)
 
 
 
 
 
 
 
 
 
 
43
  return cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB)
44
 
45
  with gr.Blocks() as demo:
46
- gr.Markdown("# 🎭 Private Face Swapper")
47
  with gr.Row():
48
  with gr.Column():
49
  src = gr.Image(label="Source Image (Face to Copy)", type="numpy")
50
  tgt = gr.Image(label="Target Image (Body / Scene)", type="numpy")
 
51
  btn = gr.Button("Swap Face", variant="primary")
52
  with gr.Column():
53
  out = gr.Image(label="Swapped Result", type="numpy")
54
 
55
- btn.click(fn=swap_faces, inputs=[src, tgt], outputs=out)
56
 
57
  demo.queue().launch()
 
1
+ import sys
2
+ import types
3
+
4
+ # Patch for newer torchvision compatibility with GFPGAN
5
+ try:
6
+ import torchvision.transforms.functional as F
7
+ functional_tensor = types.ModuleType("torchvision.transforms.functional_tensor")
8
+ functional_tensor.rgb_to_grayscale = F.rgb_to_grayscale
9
+ sys.modules["torchvision.transforms.functional_tensor"] = functional_tensor
10
+ except Exception:
11
+ pass
12
+
13
  import gradio as gr
14
  import cv2
15
  import insightface
16
  from insightface.app import FaceAnalysis
17
  from huggingface_hub import hf_hub_download
18
+ from gfpgan import GFPGANer
19
  import spaces
20
 
21
  app = None
22
  swapper = None
23
+ restorer = None
24
 
25
  def load_models():
26
+ global app, swapper, restorer
27
  if app is None:
28
  app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
29
  app.prepare(ctx_id=-1, det_size=(640, 640))
 
33
  filename="inswapper_128.onnx"
34
  )
35
  swapper = insightface.model_zoo.get_model(swapper_path, download=False)
36
+ if restorer is None:
37
+ gfpgan_path = hf_hub_download(
38
+ repo_id="gmk123/GFPGAN",
39
+ filename="GFPGANv1.4.pth"
40
+ )
41
+ restorer = GFPGANer(
42
+ model_path=gfpgan_path,
43
+ upscale=1,
44
+ arch="clean",
45
+ channel_multiplier=2,
46
+ bg_upsampler=None
47
+ )
48
 
49
  @spaces.GPU
50
+ def swap_faces(source_img, target_img, enhance_face):
51
  if source_img is None or target_img is None:
52
  return None
53
 
 
65
  source_face = sorted(source_faces, key=lambda x: x.bbox[0])[0]
66
  target_face = sorted(target_faces, key=lambda x: x.bbox[0])[0]
67
 
68
+ # 1. Perform the face swap
69
  result_bgr = swapper.get(target_bgr, target_face, source_face, paste_back=True)
70
+
71
+ # 2. Sharpen and restore the face with GFPGAN if checked
72
+ if enhance_face:
73
+ _, _, result_bgr = restorer.enhance(
74
+ result_bgr,
75
+ has_aligned=False,
76
+ only_center_face=False,
77
+ paste_back=True
78
+ )
79
+
80
  return cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB)
81
 
82
  with gr.Blocks() as demo:
83
+ gr.Markdown("# 🎭 Private Face Swapper + GFPGAN Enhancer")
84
  with gr.Row():
85
  with gr.Column():
86
  src = gr.Image(label="Source Image (Face to Copy)", type="numpy")
87
  tgt = gr.Image(label="Target Image (Body / Scene)", type="numpy")
88
+ enhance = gr.Checkbox(label="Enhance & Sharpen Face (GFPGAN v1.4)", value=True)
89
  btn = gr.Button("Swap Face", variant="primary")
90
  with gr.Column():
91
  out = gr.Image(label="Swapped Result", type="numpy")
92
 
93
+ btn.click(fn=swap_faces, inputs=[src, tgt, enhance], outputs=out)
94
 
95
  demo.queue().launch()