sandrocalzada commited on
Commit
061fa24
1 Parent(s): e70a1d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -2,16 +2,24 @@ import gradio as gr
2
  import cv2
3
  import insightface
4
  from insightface.app import FaceAnalysis
5
- from insightface.data import get_image as ins_get_image
6
 
7
  def predict(image_in_video, image_in_img):
8
  if image_in_video == None and image_in_img == None:
9
  raise gr.Error("Please capture an image using the webcam or upload an image.")
10
-
11
  image = image_in_video or image_in_img
12
- img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
-
 
 
 
 
 
 
14
  faces = app.get(img)
 
 
 
 
15
  source_face = faces[0]
16
  bbox = source_face['bbox']
17
  bbox = [int(b) for b in bbox]
@@ -22,10 +30,6 @@ def predict(image_in_video, image_in_img):
22
 
23
  return res[:, :, [2, 1, 0]] # Convert BGR to RGB for Gradio display
24
 
25
- app = FaceAnalysis(name='buffalo_l')
26
- app.prepare(ctx_id=0, det_size=(640, 640))
27
- swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download='FALSE', download_zip= 'FALSE')
28
-
29
  with gr.Blocks() as blocks:
30
  gr.Markdown("### Capture Image Using WebCam or Upload")
31
 
@@ -53,5 +57,4 @@ with gr.Blocks() as blocks:
53
  gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
54
 
55
  blocks.queue()
56
- blocks.launch()
57
-
 
2
  import cv2
3
  import insightface
4
  from insightface.app import FaceAnalysis
 
5
 
6
  def predict(image_in_video, image_in_img):
7
  if image_in_video == None and image_in_img == None:
8
  raise gr.Error("Please capture an image using the webcam or upload an image.")
 
9
  image = image_in_video or image_in_img
10
+ return swapi(image)
11
+
12
+ app = FaceAnalysis(name='buffalo_l')
13
+ app.prepare(ctx_id=0, det_size=(640, 640))
14
+ swapper = insightface.model_zoo.get_model('/content/drive/MyDrive/inswapper_128.onnx', download='FALSE', download_zip= 'FALSE')
15
+
16
+ def swapi(imagen):
17
+ img = cv2.cvtColor(np.array(imagen), cv2.COLOR_RGB2BGR) # Convert image from RGB to BGR format
18
  faces = app.get(img)
19
+
20
+ if not faces:
21
+ return img # If no faces are detected, return the original image
22
+
23
  source_face = faces[0]
24
  bbox = source_face['bbox']
25
  bbox = [int(b) for b in bbox]
 
30
 
31
  return res[:, :, [2, 1, 0]] # Convert BGR to RGB for Gradio display
32
 
 
 
 
 
33
  with gr.Blocks() as blocks:
34
  gr.Markdown("### Capture Image Using WebCam or Upload")
35
 
 
57
  gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
58
 
59
  blocks.queue()
60
+ blocks.launch()