Aiconvert commited on
Commit
346a999
·
verified ·
1 Parent(s): d20992e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -24
app.py CHANGED
@@ -10,28 +10,21 @@ def prepare_app():
10
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
11
  return app, swapper
12
 
13
- def sort_faces(faces):
14
- return sorted(faces, key=lambda x: x.bbox[0])
15
-
16
- def get_face(faces, face_id):
17
- try:
18
- if len(faces) < face_id or face_id < 1:
19
- raise gr.Error(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
20
- return faces[face_id-1]
21
- except Exception as e:
22
- raise gr.Error(f"An error occurred: {str(e)}")
23
-
24
- app, swapper = prepare_app()
25
-
26
- def swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex):
27
- """Swaps faces between the source and destination images based on the specified face indices."""
28
- faces = sort_faces(app.get(sourceImage))
29
- source_face = get_face(faces, sourceFaceIndex)
30
 
31
- res_faces = sort_faces(app.get(destinationImage))
32
- res_face = get_face(res_faces, destinationFaceIndex)
 
 
33
 
34
- result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
35
  return result
36
 
37
 
@@ -40,14 +33,12 @@ gr.Interface(
40
  swap_faces,
41
  [
42
  gr.Image(label="Source Image (the image with the face that you want to use)"),
43
- gr.Number(precision=0, value=1, label='Source Face Position', info='In case there are multiple faces on the image specify which should be used from the left, starting at 1', visible=False,),
44
  gr.Image(label="Destination Image (the image with the face that you want to replace)"),
45
- gr.Number(precision=0, value=1, label='Destination Face Position', info='In case there are multiple faces on the image specify which should be replaced from the left, starting at 1' ,visible=False,)
46
  ],
47
  gr.Image(),
48
  examples=[
49
- ['./examples/rihanna.jpg',1, './examples/margaret_thatcher.jpg',1],
50
- ['./examples/game_of_thrones.jpg',1,'./examples/game_of_thrones.jpg',1],
51
  ],
52
  theme="syddharth/gray-minimal",
53
  title="<h1 style='color: black; text-decoration: underline;'>AI Face Swapping - Free Face Swap</h1>",
 
10
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
11
  return app, swapper
12
 
13
+ def swap_faces(sourceImage, destinationImage):
14
+ """Swaps faces between the source and destination images."""
15
+ app, swapper = prepare_app()
16
+
17
+ source_faces = app.get(sourceImage)
18
+ if not source_faces:
19
+ raise gr.Error("No face found in the source image.")
20
+ source_face = source_faces[0]
 
 
 
 
 
 
 
 
 
21
 
22
+ destination_faces = app.get(destinationImage)
23
+ if not destination_faces:
24
+ raise gr.Error("No face found in the destination image.")
25
+ destination_face = destination_faces[0]
26
 
27
+ result = swapper.get(destinationImage, destination_face, source_face, paste_back=True)
28
  return result
29
 
30
 
 
33
  swap_faces,
34
  [
35
  gr.Image(label="Source Image (the image with the face that you want to use)"),
 
36
  gr.Image(label="Destination Image (the image with the face that you want to replace)"),
 
37
  ],
38
  gr.Image(),
39
  examples=[
40
+ ['./examples/rihanna.jpg', './examples/margaret_thatcher.jpg'],
41
+ ['./examples/game_of_thrones.jpg', './examples/game_of_thrones.jpg'],
42
  ],
43
  theme="syddharth/gray-minimal",
44
  title="<h1 style='color: black; text-decoration: underline;'>AI Face Swapping - Free Face Swap</h1>",