sandrocalzada commited on
Commit
b39090b
1 Parent(s): 438d719

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -1,7 +1,11 @@
 
1
  import gradio as gr
 
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:
@@ -11,10 +15,22 @@ def predict(image_in_video, image_in_img):
11
 
12
  app = FaceAnalysis(name='buffalo_l')
13
  app.prepare(ctx_id=0, det_size=(640, 640))
14
- swapper = insightface.model_zoo.get_model('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:
@@ -28,7 +44,8 @@ def swapi(imagen):
28
  for face in faces:
29
  res = swapper.get(res, face, source_face, paste_back=True)
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")
@@ -57,4 +74,4 @@ with gr.Blocks() as blocks:
57
  gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
58
 
59
  blocks.queue()
60
- blocks.launch()
 
1
+ import numpy as np
2
  import gradio as gr
3
+ import glob
4
  import cv2
5
+ import matplotlib.pyplot as plt
6
  import insightface
7
  from insightface.app import FaceAnalysis
8
+ from insightface.data import get_image as ins_get_image
9
 
10
  def predict(image_in_video, image_in_img):
11
  if image_in_video == None and image_in_img == None:
 
15
 
16
  app = FaceAnalysis(name='buffalo_l')
17
  app.prepare(ctx_id=0, det_size=(640, 640))
18
+ swapper = insightface.model_zoo.get_model('inswapper_128.onnx')
19
+
20
 
21
  def swapi(imagen):
22
+ # Check if the input is a file path (str) or an image array (numpy.ndarray)
23
+ if isinstance(imagen, str):
24
+ img = cv2.imread(imagen)
25
+ else:
26
+ img = np.array(imagen)
27
+
28
+ if img is None:
29
+ raise ValueError("Failed to read the image.")
30
+
31
+ if img.shape[-1] == 3: # Check if the image is RGB or grayscale
32
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
33
+
34
  faces = app.get(img)
35
 
36
  if not faces:
 
44
  for face in faces:
45
  res = swapper.get(res, face, source_face, paste_back=True)
46
 
47
+ return res
48
+
49
 
50
  with gr.Blocks() as blocks:
51
  gr.Markdown("### Capture Image Using WebCam or Upload")
 
74
  gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
75
 
76
  blocks.queue()
77
+ blocks.launch(debug=True)