greathassan commited on
Commit
81325a5
·
verified ·
1 Parent(s): 7561b3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -11,36 +11,26 @@ from roop.processors.frame.core import get_frame_processors_modules
11
  from roop.utilities import normalize_output_path
12
  import os
13
  from PIL import Image
14
- import cv2
15
 
16
- def swap_face(source_file, target_file, doFaceEnhancer):
17
 
 
18
  source_path = "input.jpg"
19
  target_path = "target"
20
-
21
- if isinstance(source_file, np.ndarray):
22
- # Handle source image
23
- source_image = Image.fromarray(source_file)
24
- source_path += ".jpg"
25
- source_image.save(source_path)
26
- else:
27
- raise ValueError("Source file should be an image.")
28
-
29
- if isinstance(target_file, np.ndarray):
30
- # Handle target image
31
  target_image = Image.fromarray(target_file)
32
  target_path += ".jpg"
33
  target_image.save(target_path)
34
- elif isinstance(target_file, str):
35
- # Handle target video
36
- if target_file.endswith(".mp4"):
37
- target_path += ".mp4"
38
- os.rename(target_file, target_path)
39
- else:
40
- raise ValueError("Target file should be a video with .mp4 extension.")
41
  else:
42
- raise ValueError("Target file should be an image or video.")
43
-
44
  print("source_path: ", source_path)
45
  print("target_path: ", target_path)
46
 
@@ -51,16 +41,16 @@ def swap_face(source_file, target_file, doFaceEnhancer):
51
  output_path += ".mp4"
52
  else:
53
  output_path += ".jpg"
54
-
55
  roop.globals.output_path = normalize_output_path(
56
  roop.globals.source_path, roop.globals.target_path, output_path
57
  )
58
-
59
  if doFaceEnhancer:
60
  roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
61
  else:
62
  roop.globals.frame_processors = ["face_swapper"]
63
-
64
  roop.globals.headless = True
65
  roop.globals.keep_fps = True
66
  roop.globals.keep_audio = True
@@ -87,9 +77,29 @@ def swap_face(source_file, target_file, doFaceEnhancer):
87
  return output_path
88
 
89
 
 
 
 
 
 
 
 
 
 
90
  app = gr.Interface(
91
  fn=swap_face,
92
- inputs=[gr.Image(), gr.Video() | gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")],
93
- outputs="auto"
 
 
 
 
 
 
94
  )
 
 
 
 
 
95
  app.launch()
 
11
  from roop.utilities import normalize_output_path
12
  import os
13
  from PIL import Image
 
14
 
 
15
 
16
+ def swap_face(source_file, target_file, target_type, doFaceEnhancer):
17
  source_path = "input.jpg"
18
  target_path = "target"
19
+
20
+ # Handle source image
21
+ source_image = Image.fromarray(source_file)
22
+ source_image.save(source_path)
23
+
24
+ if target_type == "Image":
 
 
 
 
 
25
  target_image = Image.fromarray(target_file)
26
  target_path += ".jpg"
27
  target_image.save(target_path)
28
+ elif target_type == "Video":
29
+ target_path += ".mp4"
30
+ os.rename(target_file, target_path)
 
 
 
 
31
  else:
32
+ raise ValueError("Unsupported target type.")
33
+
34
  print("source_path: ", source_path)
35
  print("target_path: ", target_path)
36
 
 
41
  output_path += ".mp4"
42
  else:
43
  output_path += ".jpg"
44
+
45
  roop.globals.output_path = normalize_output_path(
46
  roop.globals.source_path, roop.globals.target_path, output_path
47
  )
48
+
49
  if doFaceEnhancer:
50
  roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
51
  else:
52
  roop.globals.frame_processors = ["face_swapper"]
53
+
54
  roop.globals.headless = True
55
  roop.globals.keep_fps = True
56
  roop.globals.keep_audio = True
 
77
  return output_path
78
 
79
 
80
+ def get_target_input(target_type):
81
+ if target_type == "Image":
82
+ return gr.Image()
83
+ elif target_type == "Video":
84
+ return gr.Video()
85
+ else:
86
+ return None
87
+
88
+
89
  app = gr.Interface(
90
  fn=swap_face,
91
+ inputs=[
92
+ gr.Image(),
93
+ gr.Radio(["Image", "Video"], label="Target Type"),
94
+ gr.State(),
95
+ gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
96
+ ],
97
+ outputs="auto",
98
+ live=True
99
  )
100
+
101
+
102
+ def update_target_input(target_type):
103
+ return gr.update(inputs=[gr.Image(), get_target_input(target_type), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")])
104
+
105
  app.launch()