Jonny001 commited on
Commit
344be82
·
verified ·
1 Parent(s): 4040418

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -42
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import numpy as np
2
  import gradio as gr
3
  import roop.globals
4
  from roop.core import (
@@ -9,33 +8,37 @@ from roop.core import (
9
  )
10
  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
@@ -44,34 +47,38 @@ def swap_face(source_file, target_file, doFaceEnhancer):
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
- app = gr.Blocks()
68
-
69
- with app:
70
- gr.Interface(
71
- fn=swap_face,
72
- inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="Enhance", info="Face Enhancer")],
73
- outputs="image",
74
- description="This model is running on CPU and might be slow."
75
- )
 
 
76
 
77
- app.launch()
 
 
1
  import gradio as gr
2
  import roop.globals
3
  from roop.core import (
 
8
  )
9
  from roop.processors.frame.core import get_frame_processors_modules
10
  from roop.utilities import normalize_output_path
 
11
  from PIL import Image
12
+ import io
13
 
14
+ def process_face_swap(source_image, target_image, apply_face_enhancer):
15
+ # Convert images to PIL Image objects and save them to in-memory file-like objects
16
+ src_image_bytes = io.BytesIO()
17
+ tgt_image_bytes = io.BytesIO()
18
+
19
+ source_image.save(src_image_bytes, format='JPEG')
20
+ target_image.save(tgt_image_bytes, format='JPEG')
21
 
22
+ src_image_bytes.seek(0)
23
+ tgt_image_bytes.seek(0)
24
 
25
+ src_image_path = "source_image.jpg"
26
+ tgt_image_path = "target_image.jpg"
27
+ output_path = "result_image.jpg"
28
 
29
+ with open(src_image_path, 'wb') as f:
30
+ f.write(src_image_bytes.getvalue())
31
+
32
+ with open(tgt_image_path, 'wb') as f:
33
+ f.write(tgt_image_bytes.getvalue())
34
 
35
+ # Set Roop configuration
36
+ roop.globals.source_path = src_image_path
37
+ roop.globals.target_path = tgt_image_path
 
 
 
38
  roop.globals.output_path = normalize_output_path(
39
  roop.globals.source_path, roop.globals.target_path, output_path
40
  )
41
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"] if apply_face_enhancer else ["face_swapper"]
 
 
 
42
  roop.globals.headless = True
43
  roop.globals.keep_fps = True
44
  roop.globals.keep_audio = True
 
47
  roop.globals.video_encoder = "libx264"
48
  roop.globals.video_quality = 18
49
  roop.globals.max_memory = suggest_max_memory()
50
+ roop.globals.execution_providers = decode_execution_providers(["cuda", "cpu"])
51
  roop.globals.execution_threads = suggest_execution_threads()
52
 
53
+ # Print paths for debugging
54
+ print(f"Starting the process with:")
55
+ print(f"Source: {roop.globals.source_path}")
56
+ print(f"Target: {roop.globals.target_path}")
57
+ print(f"Output: {roop.globals.output_path}")
 
58
 
59
+ # Execute face swap process
60
+ for processor in get_frame_processors_modules(roop.globals.frame_processors):
61
+ if not processor.pre_check():
 
62
  return
63
 
64
  start()
65
+
66
+ # Load and return the output image
67
+ with open(roop.globals.output_path, 'rb') as f:
68
+ output_image = f.read()
69
+
70
+ return output_image
71
 
72
+ # Create Gradio interface
73
+ iface = gr.Interface(
74
+ fn=process_face_swap,
75
+ inputs=[
76
+ gr.Image(type="pil", label="Source Image"),
77
+ gr.Image(type="pil", label="Target Image"),
78
+ gr.Checkbox(label="Apply Face Enhancer")
79
+ ],
80
+ outputs=gr.Image(type="pil"),
81
+ description="Swap faces between two images. Use the face enhancer option to improve the result. Processing may take longer if you use Face Enhancer. Becose this Modle is running using CPU."
82
+ )
83
 
84
+ iface.launch()