arjunyonjan commited on
Commit
6cd2f04
·
verified ·
1 Parent(s): d110311

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +48 -26
app.py CHANGED
@@ -1,46 +1,68 @@
1
  import spaces
2
  import gradio as gr
3
  import cv2
4
- import traceback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @spaces.GPU
6
  def swap_face(source_img, target_img):
 
7
  import insightface
8
  from insightface.app import FaceAnalysis
9
  try:
10
- app = FaceAnalysis(name='buffalo_l')
 
 
11
  try:
12
- app.prepare(ctx_id=0, det_size=(640,640))
13
- except:
14
- app.prepare(ctx_id=-1, det_size=(640,640))
15
  import insightface.model_zoo
16
- swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True)
17
- src = cv2.imread(source_img)
18
- tgt = cv2.imread(target_img)
19
  s_faces = app.get(src)
20
  t_faces = app.get(tgt)
21
- if len(s_faces)==0 or len(t_faces)==0:
22
- app2 = FaceAnalysis(name='buffalo_l')
23
- app2.prepare(ctx_id=-1, det_size=(1024,1024))
24
- if len(s_faces)==0: s_faces = app2.get(src)
25
- if len(t_faces)==0: t_faces = app2.get(tgt)
26
- if len(s_faces)==0 or len(t_faces)==0:
27
- cv2.putText(tgt, f"s:{len(s_faces)} t:{len(t_faces)}", (20,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
28
- out="/tmp/debug.jpg"
29
- cv2.imwrite(out,tgt)
30
- return out
31
  res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
32
- out="/tmp/swapped.jpg"
33
- cv2.imwrite(out,res)
34
  return out
35
- except Exception as e:
36
- import traceback
37
- raise gr.Error(traceback.format_exc())
 
 
 
38
  with gr.Blocks() as demo:
39
  gr.Markdown("# FaceFusion Zero")
40
  with gr.Row():
41
- src = gr.Image(type="filepath", label="Source")
42
- tgt = gr.Image(type="filepath", label="Target")
43
- out = gr.Image(type="filepath", label="Result")
44
  btn = gr.Button("Swap")
45
  btn.click(swap_face, inputs=[src, tgt], outputs=out)
 
46
  demo.launch()
 
1
  import spaces
2
  import gradio as gr
3
  import cv2
4
+ import numpy as np
5
+
6
+
7
+ def _to_bgr(img):
8
+ if img is None:
9
+ raise ValueError("no image received")
10
+ if isinstance(img, str):
11
+ im = cv2.imread(img)
12
+ if im is None:
13
+ raise ValueError("unreadable path: %s" % img)
14
+ return im
15
+ if hasattr(img, "convert"):
16
+ return cv2.cvtColor(np.array(img.convert("RGB")), cv2.COLOR_RGB2BGR)
17
+ a = np.array(img)
18
+ if a.ndim == 3 and a.shape[2] >= 3:
19
+ return cv2.cvtColor(a[:, :, :3], cv2.COLOR_RGB2BGR)
20
+ return a
21
+
22
+
23
  @spaces.GPU
24
  def swap_face(source_img, target_img):
25
+ import traceback
26
  import insightface
27
  from insightface.app import FaceAnalysis
28
  try:
29
+ src = _to_bgr(source_img)
30
+ tgt = _to_bgr(target_img)
31
+ app = FaceAnalysis(name="buffalo_l")
32
  try:
33
+ app.prepare(ctx_id=0, det_size=(640, 640))
34
+ except Exception:
35
+ app.prepare(ctx_id=-1, det_size=(640, 640))
36
  import insightface.model_zoo
37
+ swapper = insightface.model_zoo.get_model("inswapper_128.onnx", download=True)
 
 
38
  s_faces = app.get(src)
39
  t_faces = app.get(tgt)
40
+ if len(s_faces) == 0 or len(t_faces) == 0:
41
+ app2 = FaceAnalysis(name="buffalo_l")
42
+ app2.prepare(ctx_id=-1, det_size=(1024, 1024))
43
+ if len(s_faces) == 0:
44
+ s_faces = app2.get(src)
45
+ if len(t_faces) == 0:
46
+ t_faces = app2.get(tgt)
47
+ if len(s_faces) == 0 or len(t_faces) == 0:
48
+ raise gr.Error("no faces detected s:%d t:%d" % (len(s_faces), len(t_faces)))
 
49
  res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
50
+ out = "/tmp/swapped.jpg"
51
+ cv2.imwrite(out, res)
52
  return out
53
+ except gr.Error:
54
+ raise
55
+ except Exception:
56
+ raise gr.Error(traceback.format_exc()[-1500:])
57
+
58
+
59
  with gr.Blocks() as demo:
60
  gr.Markdown("# FaceFusion Zero")
61
  with gr.Row():
62
+ src = gr.Image(label="Source")
63
+ tgt = gr.Image(label="Target")
64
+ out = gr.Image(label="Result")
65
  btn = gr.Button("Swap")
66
  btn.click(swap_face, inputs=[src, tgt], outputs=out)
67
+
68
  demo.launch()