sandrocalzada commited on
Commit
59057f1
1 Parent(s): fdc2c6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -19,32 +19,27 @@ 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:
37
- return img # If no faces are detected, return the original image
38
 
39
- source_face = faces[0]
40
- bbox = source_face['bbox']
41
- bbox = [int(b) for b in bbox]
42
 
43
- res = img.copy()
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:
 
19
 
20
 
21
  def swapi(imagen):
22
+ # Use the uploaded image to extract features
23
+ img_user = cv2.imread(imagen)
24
+ faces_user = app.get(img_user)
 
 
25
 
26
+ # Use another image "background1" for modifications
27
+ img_background = cv2.imread('background1.jpg')
28
+ faces_background = app.get(img_background)
29
 
30
+ # Assuming the user image has a face and we are using its features
31
+ source_face = faces_user[0]
32
 
33
+ # Apply modifications to the "background1" image
34
+ res = img_background.copy()
35
+ for face in faces_background:
36
+ res = swapper.get(res, face, source_face, paste_back=True)
37
 
38
+ # Convert from BGR to RGB
39
+ res_rgb = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
 
40
 
41
+ return res_rgb
 
 
42
 
 
43
 
44
 
45
  with gr.Blocks() as blocks: