aheedsajid commited on
Commit
263cf40
·
verified ·
1 Parent(s): a49d20b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -18
app.py CHANGED
@@ -11,25 +11,54 @@ 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
- def swap_face(source_file, target_file, doFaceEnhancer):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  source_path = "input.jpg"
16
  target_path = "target.jpg"
17
  source_image = Image.fromarray(source_file)
18
  source_image.save(source_path)
19
  target_image = Image.fromarray(target_file)
20
  target_image.save(target_path)
21
- print("source_path: ", source_path)
22
- print("target_path: ", target_path)
23
  roop.globals.source_path = source_path
24
  roop.globals.target_path = target_path
25
  output_path = "output.jpg"
26
  roop.globals.output_path = normalize_output_path(
27
  roop.globals.source_path, roop.globals.target_path, output_path
28
  )
 
29
  if doFaceEnhancer:
30
  roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
31
  else:
32
  roop.globals.frame_processors = ["face_swapper"]
 
33
  roop.globals.headless = True
34
  roop.globals.keep_fps = True
35
  roop.globals.keep_audio = True
@@ -40,28 +69,41 @@ def swap_face(source_file, target_file, doFaceEnhancer):
40
  roop.globals.max_memory = suggest_max_memory()
41
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
42
  roop.globals.execution_threads = suggest_execution_threads()
43
- print(
44
- "start process",
45
- roop.globals.source_path,
46
- roop.globals.target_path,
47
- roop.globals.output_path,
48
- )
49
- for frame_processor in get_frame_processors_modules(
50
- roop.globals.frame_processors
51
- ):
52
  if not frame_processor.pre_check():
53
- return
 
54
  start()
55
  return output_path
 
56
  html_section_1 = "<div><h1>Welcome to the NSFW Face Swap & API</h1></div>"
57
  html_section_2 = '<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature for HD Results.</p><h2><br /><strong>For fast bulk swap and API visit:</strong>&nbsp;<a href="https://picfy.xyz/" target="_blank" rel="noopener">https://picfy.xyz/</a><br /> <strong>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2></div><br>Start Face Swap SaaS on WordPress:</strong>&nbsp;<a href="https://www.codester.com/aheed/" target="_blank" rel="noopener">https://www.codester.com/aheed/</a>'
 
58
  app = gr.Blocks()
59
  with app:
60
  gr.HTML(html_section_1)
61
  gr.HTML(html_section_2)
62
- gr.Interface(
63
- fn=swap_face,
64
- inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")],
65
- outputs="image"
66
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  app.launch()
 
11
  from roop.utilities import normalize_output_path
12
  import os
13
  from PIL import Image
14
+ import requests
15
+ from io import BytesIO
16
+
17
+ def load_image_from_url(url):
18
+ try:
19
+ response = requests.get(url)
20
+ img = Image.open(BytesIO(response.content))
21
+ return np.array(img)
22
+ except:
23
+ return None
24
+
25
+ def swap_face(source_input, target_input, source_url, target_url, doFaceEnhancer):
26
+ # Handle source image
27
+ source_file = None
28
+ if source_input is not None:
29
+ source_file = source_input
30
+ elif source_url:
31
+ source_file = load_image_from_url(source_url)
32
+
33
+ # Handle target image
34
+ target_file = None
35
+ if target_input is not None:
36
+ target_file = target_input
37
+ elif target_url:
38
+ target_file = load_image_from_url(target_url)
39
+
40
+ if source_file is None or target_file is None:
41
+ return None
42
+
43
  source_path = "input.jpg"
44
  target_path = "target.jpg"
45
  source_image = Image.fromarray(source_file)
46
  source_image.save(source_path)
47
  target_image = Image.fromarray(target_file)
48
  target_image.save(target_path)
49
+
 
50
  roop.globals.source_path = source_path
51
  roop.globals.target_path = target_path
52
  output_path = "output.jpg"
53
  roop.globals.output_path = normalize_output_path(
54
  roop.globals.source_path, roop.globals.target_path, output_path
55
  )
56
+
57
  if doFaceEnhancer:
58
  roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
59
  else:
60
  roop.globals.frame_processors = ["face_swapper"]
61
+
62
  roop.globals.headless = True
63
  roop.globals.keep_fps = True
64
  roop.globals.keep_audio = True
 
69
  roop.globals.max_memory = suggest_max_memory()
70
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
71
  roop.globals.execution_threads = suggest_execution_threads()
72
+
73
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
 
 
 
 
 
 
 
74
  if not frame_processor.pre_check():
75
+ return None
76
+
77
  start()
78
  return output_path
79
+
80
  html_section_1 = "<div><h1>Welcome to the NSFW Face Swap & API</h1></div>"
81
  html_section_2 = '<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature for HD Results.</p><h2><br /><strong>For fast bulk swap and API visit:</strong>&nbsp;<a href="https://picfy.xyz/" target="_blank" rel="noopener">https://picfy.xyz/</a><br /> <strong>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2></div><br>Start Face Swap SaaS on WordPress:</strong>&nbsp;<a href="https://www.codester.com/aheed/" target="_blank" rel="noopener">https://www.codester.com/aheed/</a>'
82
+
83
  app = gr.Blocks()
84
  with app:
85
  gr.HTML(html_section_1)
86
  gr.HTML(html_section_2)
87
+ with gr.Tabs():
88
+ with gr.Tab("Upload Images"):
89
+ gr.Interface(
90
+ fn=lambda s, t, d: swap_face(s, t, None, None, d),
91
+ inputs=[
92
+ gr.Image(label="Source Image"),
93
+ gr.Image(label="Target Image"),
94
+ gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
95
+ ],
96
+ outputs="image"
97
+ )
98
+ with gr.Tab("Image URLs"):
99
+ gr.Interface(
100
+ fn=lambda s, t, d: swap_face(None, None, s, t, d),
101
+ inputs=[
102
+ gr.Textbox(label="Source Image URL"),
103
+ gr.Textbox(label="Target Image URL"),
104
+ gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
105
+ ],
106
+ outputs="image"
107
+ )
108
+
109
  app.launch()