arjunyonjan commited on
Commit
3f9baed
·
verified ·
1 Parent(s): 6d7c2b1

light opencv enhancer strict facefusion

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -4,6 +4,13 @@ import cv2
4
  import numpy as np
5
  import traceback
6
 
 
 
 
 
 
 
 
7
  @spaces.GPU
8
  def swap_face(source_img, target_img):
9
  import insightface
@@ -33,7 +40,19 @@ def swap_face(source_img, target_img):
33
  cv2.imwrite(out,tgt)
34
  return out
35
  res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
36
- out="/tmp/swapped.jpg"
 
 
 
 
 
 
 
 
 
 
 
 
37
  cv2.imwrite(out,res)
38
  return out
39
  except Exception as e:
@@ -45,11 +64,11 @@ def swap_face(source_img, target_img):
45
  return out
46
 
47
  with gr.Blocks() as demo:
48
- gr.Markdown("# FaceFusion Zero")
49
  with gr.Row():
50
  src = gr.Image(type="filepath", label="Source")
51
  tgt = gr.Image(type="filepath", label="Target")
52
- out = gr.Image(type="filepath", label="Result")
53
- btn = gr.Button("Swap")
54
  btn.click(swap_face, inputs=[src, tgt], outputs=out)
55
  demo.launch()
 
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
  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:
 
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()