arjunyonjan commited on
Commit
74943d8
·
verified ·
1 Parent(s): 3f9baed

hq gfpgan pinned py310

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +11 -25
  3. requirements.txt +6 -2
README.md CHANGED
@@ -8,5 +8,5 @@ sdk_version: 6.25.0
8
  app_file: app.py
9
  pinned: false
10
  hardware: zero-a10g
 
11
  ---
12
- Private FaceFusion ZeroGPU Space
 
8
  app_file: app.py
9
  pinned: false
10
  hardware: zero-a10g
11
+ python_version: "3.10"
12
  ---
 
app.py CHANGED
@@ -1,16 +1,8 @@
1
  import spaces
2
  import gradio as gr
3
  import cv2
4
- import numpy as np
5
  import traceback
6
 
7
- def enhance_sharp(img):
8
- # strong unsharp mask via opencv - works without gfpgan
9
- gauss = cv2.GaussianBlur(img, (0,0), 2)
10
- sharp = cv2.addWeighted(img, 1.7, gauss, -0.7, 0)
11
- # extra detail enhance on face region only is done by caller
12
- return sharp
13
-
14
  @spaces.GPU
15
  def swap_face(source_img, target_img):
16
  import insightface
@@ -40,35 +32,29 @@ def swap_face(source_img, target_img):
40
  cv2.imwrite(out,tgt)
41
  return out
42
  res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
43
- # enhance face region only for sharpness
44
  try:
45
- # detect face bbox and enhance that region
46
- x1,y1,x2,y2 = map(int, t_faces[0].bbox)
47
- pad = 20
48
- x1,y1 = max(0,x1-pad), max(0,y1-pad)
49
- x2,y2 = min(res.shape[1], x2+pad), min(res.shape[0], y2+pad)
50
- face_crop = res[y1:y2, x1:x2]
51
- enhanced = enhance_sharp(face_crop)
52
- res[y1:y2, x1:x2] = enhanced
53
- except:
54
- res = enhance_sharp(res)
55
- out="/tmp/swapped_enhanced.jpg"
56
  cv2.imwrite(out,res)
57
  return out
58
  except Exception as e:
59
- tb=traceback.format_exc()[:500]
60
  tgt2=cv2.imread(target_img)
61
- cv2.putText(tgt2, str(e)[:50], (20,40), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255),2)
62
  out="/tmp/err.jpg"
63
  cv2.imwrite(out,tgt2)
64
  return out
65
 
66
  with gr.Blocks() as demo:
67
- gr.Markdown("# FaceFusion Zero - Enhanced (opencv)")
68
  with gr.Row():
69
  src = gr.Image(type="filepath", label="Source")
70
  tgt = gr.Image(type="filepath", label="Target")
71
- out = gr.Image(type="filepath", label="Enhanced Result")
72
- btn = gr.Button("Swap + Enhance")
73
  btn.click(swap_face, inputs=[src, tgt], outputs=out)
74
  demo.launch()
 
1
  import spaces
2
  import gradio as gr
3
  import cv2
 
4
  import traceback
5
 
 
 
 
 
 
 
 
6
  @spaces.GPU
7
  def swap_face(source_img, target_img):
8
  import insightface
 
32
  cv2.imwrite(out,tgt)
33
  return out
34
  res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
35
+ # High quality GFPGAN enhancer
36
  try:
37
+ from gfpgan import GFPGANer
38
+ enhancer = GFPGANer(model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth', upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None)
39
+ _, _, res = enhancer.enhance(res, has_aligned=False, only_center_face=False, paste_back=True)
40
+ except Exception as e:
41
+ print("gfpgan fail",e, flush=True)
42
+ out="/tmp/swapped_gfpgan.jpg"
 
 
 
 
 
43
  cv2.imwrite(out,res)
44
  return out
45
  except Exception as e:
 
46
  tgt2=cv2.imread(target_img)
47
+ cv2.putText(tgt2, str(e)[:60], (20,40), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255),2)
48
  out="/tmp/err.jpg"
49
  cv2.imwrite(out,tgt2)
50
  return out
51
 
52
  with gr.Blocks() as demo:
53
+ gr.Markdown("# FaceFusion Zero + GFPGAN HQ")
54
  with gr.Row():
55
  src = gr.Image(type="filepath", label="Source")
56
  tgt = gr.Image(type="filepath", label="Target")
57
+ out = gr.Image(type="filepath", label="GFPGAN Enhanced")
58
+ btn = gr.Button("Swap + GFPGAN")
59
  btn.click(swap_face, inputs=[src, tgt], outputs=out)
60
  demo.launch()
requirements.txt CHANGED
@@ -1,7 +1,11 @@
1
  gradio
2
  spaces
3
- insightface
4
  onnxruntime-gpu
5
- opencv-python
 
 
 
 
6
  huggingface_hub
7
  hf_transfer
 
1
  gradio
2
  spaces
3
+ insightface==0.7.3
4
  onnxruntime-gpu
5
+ opencv-python==4.8.0.76
6
+ gfpgan==1.3.8
7
+ basicsr==1.4.2
8
+ facexlib==0.3.0
9
+ realesrgan==0.3.0
10
  huggingface_hub
11
  hf_transfer