Apex-X commited on
Commit
3d2113a
·
verified ·
1 Parent(s): 59d275a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -52
app.py CHANGED
@@ -1,9 +1,7 @@
1
- # -*- coding: UTF-8 -*-
2
- # !/usr/bin/env python
3
 
4
- import os
5
- import shutil
6
- from PIL import Image
7
  import gradio as gr
8
  import roop.globals
9
  from roop.core import (
@@ -14,29 +12,33 @@ from roop.core import (
14
  )
15
  from roop.processors.frame.core import get_frame_processors_modules
16
  from roop.utilities import normalize_output_path
 
 
 
 
 
17
 
18
- def swap_face(source_file, target_file, do_face_enhancer, mode):
19
- # Save source image
20
  source_path = "input.jpg"
21
- Image.open(source_file).save(source_path)
22
- roop.globals.source_path = source_path
23
 
24
- # Save target file based on mode
25
- if mode == "Image to Image":
26
- target_path = "target.jpg"
27
- Image.open(target_file).save(target_path)
28
- output_path = "output.jpg"
29
- else: # Image to Video
30
- target_path = "target.mp4"
31
- shutil.copyfile(target_file, target_path) # ✅ FIXED for file path
32
- output_path = "output.mp4"
33
 
34
- # Set Roop globals
 
 
 
35
  roop.globals.target_path = target_path
 
36
  roop.globals.output_path = normalize_output_path(
37
  roop.globals.source_path, roop.globals.target_path, output_path
38
  )
39
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"] if do_face_enhancer else ["face_swapper"]
 
 
 
40
  roop.globals.headless = True
41
  roop.globals.keep_fps = True
42
  roop.globals.keep_audio = True
@@ -48,40 +50,24 @@ def swap_face(source_file, target_file, do_face_enhancer, mode):
48
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
49
  roop.globals.execution_threads = suggest_execution_threads()
50
 
51
- # Validate pre-checks
52
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
 
 
 
 
 
 
 
 
53
  if not frame_processor.pre_check():
54
- return None
55
 
56
- # Start face swap
57
  start()
 
58
 
59
- return roop.globals.output_path
60
-
61
- # Gradio Interface
62
- with gr.Blocks() as app:
63
- gr.Markdown("# 👤 Face Swap App - Roop Based")
64
-
65
- with gr.Row():
66
- mode = gr.Radio(["Image to Image", "Image to Video"], label="Swap Mode", value="Image to Image")
67
- do_face_enhancer = gr.Checkbox(label="Apply Face Enhancer", value=True)
68
-
69
- with gr.Row():
70
- source_file = gr.Image(label="Source Face Image", type="filepath")
71
- target_file = gr.File(label="Target Image or Video")
72
-
73
- output = gr.File(label="Swapped Output")
74
 
75
- swap_button = gr.Button("🔁 Swap Face")
76
-
77
- def run_swap(source_file, target_file, do_face_enhancer, mode):
78
- result = swap_face(source_file, target_file, do_face_enhancer, mode)
79
- return result
80
-
81
- swap_button.click(
82
- fn=run_swap,
83
- inputs=[source_file, target_file, do_face_enhancer, mode],
84
- outputs=output
85
- )
86
-
87
- app.launch()
 
 
 
1
 
2
+ # -* coding:UTF-8 -*
3
+ # !/usr/bin/env python
4
+ import numpy as np
5
  import gradio as gr
6
  import roop.globals
7
  from roop.core import (
 
12
  )
13
  from roop.processors.frame.core import get_frame_processors_modules
14
  from roop.utilities import normalize_output_path
15
+ import os
16
+ from PIL import Image
17
+
18
+
19
+ def swap_face(source_file, target_file,doFaceEnhancer):
20
 
 
 
21
  source_path = "input.jpg"
22
+ target_path = "target.jpg"
 
23
 
24
+ source_image = Image.fromarray(source_file)
25
+ source_image.save(source_path)
26
+ target_image = Image.fromarray(target_file)
27
+ target_image.save(target_path)
 
 
 
 
 
28
 
29
+ print("source_path: ", source_path)
30
+ print("target_path: ", target_path)
31
+
32
+ roop.globals.source_path = source_path
33
  roop.globals.target_path = target_path
34
+ output_path = "output.jpg"
35
  roop.globals.output_path = normalize_output_path(
36
  roop.globals.source_path, roop.globals.target_path, output_path
37
  )
38
+ if doFaceEnhancer == True:
39
+ roop.globals.frame_processors = ["face_swapper","face_enhancer"]
40
+ else:
41
+ roop.globals.frame_processors = ["face_swapper"]
42
  roop.globals.headless = True
43
  roop.globals.keep_fps = True
44
  roop.globals.keep_audio = True
 
50
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
51
  roop.globals.execution_threads = suggest_execution_threads()
52
 
53
+ print(
54
+ "start process",
55
+ roop.globals.source_path,
56
+ roop.globals.target_path,
57
+ roop.globals.output_path,
58
+ )
59
+
60
+ for frame_processor in get_frame_processors_modules(
61
+ roop.globals.frame_processors
62
+ ):
63
  if not frame_processor.pre_check():
64
+ return
65
 
 
66
  start()
67
+ return output_path
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ app = gr.Interface(
71
+ fn=swap_face, inputs=[gr.Image(), gr.Image(),gr.Checkbox(label="face_enhancer?", info="do face enhancer?")], outputs="image"
72
+ )
73
+ app.launch()