Jonny001 commited on
Commit
e3f4dc3
·
verified ·
1 Parent(s): 597de6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -61
app.py CHANGED
@@ -11,83 +11,100 @@ 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
 
 
 
 
 
 
15
 
16
- def swap_face(source_file, target_file, doFaceEnhancer):
 
 
 
17
 
18
- source_path = "input.jpg"
19
- target_path = "target.jpg"
 
 
 
 
 
 
 
 
20
 
21
- source_image = Image.fromarray(source_file)
22
- source_image.save(source_path)
23
- target_image = Image.fromarray(target_file)
24
- target_image.save(target_path)
 
25
 
26
- print("source_path: ", source_path)
27
- print("target_path: ", target_path)
 
 
 
 
 
 
 
28
 
29
- roop.globals.source_path = source_path
30
- roop.globals.target_path = target_path
31
- output_path = "output.jpg"
32
- roop.globals.output_path = normalize_output_path(
33
- roop.globals.source_path, roop.globals.target_path, output_path
34
- )
35
- if doFaceEnhancer:
36
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
37
- else:
38
- roop.globals.frame_processors = ["face_swapper"]
39
- roop.globals.headless = True
40
- roop.globals.keep_fps = True
41
- roop.globals.keep_audio = True
42
- roop.globals.keep_frames = False
43
- roop.globals.many_faces = False
44
- roop.globals.video_encoder = "libx264"
45
- roop.globals.video_quality = 18
46
- roop.globals.max_memory = suggest_max_memory()
47
- roop.globals.execution_providers = decode_execution_providers(["cuda"])
48
- roop.globals.execution_threads = suggest_execution_threads()
49
 
50
- print(
51
- "start process",
52
- roop.globals.source_path,
53
- roop.globals.target_path,
54
- roop.globals.output_path,
55
- )
 
 
 
56
 
57
- for frame_processor in get_frame_processors_modules(
58
- roop.globals.frame_processors
59
- ):
60
- if not frame_processor.pre_check():
61
- return
62
 
63
- start()
64
- return output_path
65
 
 
 
66
 
67
- #html_section_1 = "<div><h1>Welcome to the Face Swap App</h1></div>"
68
- html_section_2 = "<div><p>This model is running on CPU an might be slow.</p></div>"
69
- html_section_3 = """<div>
70
- <a href="https://ziverr.xyz/monica" target="_blank" style="display: inline-block;">
71
- <img decoding="async" alt="banner" src="https://ziverr.xyz/wp-content/uploads/2024/06/PASSIVE-3.gif">
72
- </a>
73
- <a href="https://go.fiverr.com/visit/?bta=36184&brand=fiverrcpa&landingPage=https%253A%252F%252Fwww.fiverr.com%252Fcategories%252Fprogramming-tech%252Fai-coding%252Fai-applications%253Fsource%253Dcategory_tree" target="_blank" style="display: inline-block;">
74
- <img fetchpriority="high" decoding="async" width="468" height="120" src="https://ziverr.xyz/wp-content/uploads/2024/06/PASSIVE-1.gif" class="attachment-large size-large wp-image-1266" alt="">
75
- </a>
76
- <a href="https://beta.publishers.adsterra.com/referral/UNXJYTziBP" target="_blank" style="display: inline-block;">
77
- <img decoding="async" alt="banner" src="https://landings-cdn.adsterratech.com/referralBanners/gif/468x120_adsterra_reff.gif">
78
- </a>
79
- </div>"""
80
 
 
81
  app = gr.Blocks()
82
 
83
  with app:
84
- # gr.HTML(html_section_1)
85
- gr.HTML(html_section_2)
86
- gr.HTML(html_section_3)
 
 
 
 
 
 
 
 
 
 
 
 
87
  gr.Interface(
88
  fn=swap_face,
89
- inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="Enhance", info="Face Enhancer")],
90
- outputs="image"
91
- )
92
 
93
  app.launch()
 
11
  from roop.utilities import normalize_output_path
12
  import os
13
  from PIL import Image
14
+ from datetime import datetime
15
 
16
+ def swap_face(source_file, target_file, doFaceEnhancer, use_gpu):
17
+ try:
18
+ # Save input images temporarily
19
+ source_path = "input.jpg"
20
+ target_path = "target.jpg"
21
 
22
+ source_image = Image.fromarray(source_file)
23
+ source_image.save(source_path)
24
+ target_image = Image.fromarray(target_file)
25
+ target_image.save(target_path)
26
 
27
+ # Set global variables for Roop
28
+ roop.globals.source_path = source_path
29
+ roop.globals.target_path = target_path
30
+
31
+ # Create a dynamic output path
32
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
33
+ output_path = f"output_{timestamp}.jpg"
34
+ roop.globals.output_path = normalize_output_path(
35
+ roop.globals.source_path, roop.globals.target_path, output_path
36
+ )
37
 
38
+ # Configure frame processors
39
+ if doFaceEnhancer:
40
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
41
+ else:
42
+ roop.globals.frame_processors = ["face_swapper"]
43
 
44
+ # Global settings
45
+ roop.globals.headless = True
46
+ roop.globals.keep_fps = True
47
+ roop.globals.keep_audio = True
48
+ roop.globals.keep_frames = False
49
+ roop.globals.many_faces = False
50
+ roop.globals.video_encoder = "libx264"
51
+ roop.globals.video_quality = 18
52
+ roop.globals.max_memory = suggest_max_memory()
53
 
54
+ # Execution providers
55
+ if use_gpu:
56
+ roop.globals.execution_providers = decode_execution_providers(["cuda"])
57
+ else:
58
+ roop.globals.execution_providers = decode_execution_providers(["cpu"])
59
+ roop.globals.execution_threads = suggest_execution_threads()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ print(
62
+ "Starting process with the following parameters:",
63
+ f"Source: {roop.globals.source_path}",
64
+ f"Target: {roop.globals.target_path}",
65
+ f"Output: {roop.globals.output_path}",
66
+ f"Enhancer: {doFaceEnhancer}",
67
+ f"Using GPU: {use_gpu}",
68
+ sep="\n"
69
+ )
70
 
71
+ # Pre-check and start process
72
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
73
+ if not frame_processor.pre_check():
74
+ return "Pre-check failed for frame processors."
 
75
 
76
+ start()
77
+ return output_path
78
 
79
+ except Exception as e:
80
+ return f"An error occurred: {e}"
81
 
82
+ # HTML content for UI information
83
+ html_section_1 = "<div><p>This model is running on the selected hardware (CPU/GPU). Processing might take some time.</p></div>"
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ # Create the Gradio interface
86
  app = gr.Blocks()
87
 
88
  with app:
89
+ gr.HTML(html_section_1)
90
+ gr.Markdown("## Face Swap Application")
91
+ gr.Markdown("Upload a source and target image to swap faces. Optionally, enhance the swapped face.")
92
+
93
+ # Inputs and Interface
94
+ inputs = [
95
+ gr.Image(label="Source Image"),
96
+ gr.Image(label="Target Image"),
97
+ gr.Checkbox(label="Enhance", info="Use Face Enhancer"),
98
+ gr.Checkbox(label="Use GPU (if available)", value=True)
99
+ ]
100
+
101
+ outputs = gr.Image(label="Swapped Image")
102
+
103
+ # Interface function call
104
  gr.Interface(
105
  fn=swap_face,
106
+ inputs=inputs,
107
+ outputs=outputs
108
+ ).launch()
109
 
110
  app.launch()