Apex-X commited on
Commit
03d2b14
·
verified ·
1 Parent(s): 0fed41d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -18
app.py CHANGED
@@ -1,5 +1,4 @@
1
-
2
- # -* coding:UTF-8 -*
3
  # !/usr/bin/env python
4
  import numpy as np
5
  import gradio as gr
@@ -13,11 +12,33 @@ from roop.core import (
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
 
@@ -35,10 +56,12 @@ def swap_face(source_file, target_file,doFaceEnhancer):
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,24 +73,28 @@ def swap_face(source_file, target_file,doFaceEnhancer):
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()
 
 
1
+ # -*- coding: UTF-8 -*-
 
2
  # !/usr/bin/env python
3
  import numpy as np
4
  import gradio as gr
 
12
  from roop.processors.frame.core import get_frame_processors_modules
13
  from roop.utilities import normalize_output_path
14
  import os
15
+ from PIL import Image, ImageDraw, ImageFont
16
+
17
+
18
+ def add_watermark(image_path, text="Made with ❤️ by Aadhi"):
19
+ image = Image.open(image_path).convert("RGBA")
20
+ watermark_layer = Image.new("RGBA", image.size, (255, 255, 255, 0))
21
+ draw = ImageDraw.Draw(watermark_layer)
22
+
23
+ font_size = int(min(image.size) / 20)
24
+ try:
25
+ font = ImageFont.truetype("arial.ttf", font_size)
26
+ except:
27
+ font = ImageFont.load_default()
28
+
29
+ text_width, text_height = draw.textsize(text, font=font)
30
+ x = image.width - text_width - 20
31
+ y = image.height - text_height - 20
32
+
33
+ draw.text((x, y), text, font=font, fill=(255, 255, 255, 180)) # semi-transparent white
34
 
35
+ watermarked_image = Image.alpha_composite(image, watermark_layer)
36
+ watermarked_path = "output_watermarked.jpg"
37
+ watermarked_image.convert("RGB").save(watermarked_path)
38
+ return watermarked_path
39
 
 
40
 
41
+ def swap_face(source_file, target_file, doFaceEnhancer):
42
  source_path = "input.jpg"
43
  target_path = "target.jpg"
44
 
 
56
  roop.globals.output_path = normalize_output_path(
57
  roop.globals.source_path, roop.globals.target_path, output_path
58
  )
59
+
60
+ if doFaceEnhancer:
61
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
62
  else:
63
  roop.globals.frame_processors = ["face_swapper"]
64
+
65
  roop.globals.headless = True
66
  roop.globals.keep_fps = True
67
  roop.globals.keep_audio = True
 
73
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
74
  roop.globals.execution_threads = suggest_execution_threads()
75
 
76
+ print("start process", roop.globals.source_path, roop.globals.target_path, roop.globals.output_path)
 
 
 
 
 
77
 
78
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
 
 
79
  if not frame_processor.pre_check():
80
  return
81
 
82
  start()
83
+
84
+ # Add watermark after face swap
85
+ watermarked_path = add_watermark(output_path, text="Made with ❤️ by Aadhi")
86
+ return watermarked_path
87
 
88
 
89
  app = gr.Interface(
90
+ fn=swap_face,
91
+ inputs=[
92
+ gr.Image(label="Source Image"),
93
+ gr.Image(label="Target Image"),
94
+ gr.Checkbox(label="Enable Face Enhancer?", info="Improves output quality")
95
+ ],
96
+ outputs=gr.Image(label="Output Image with Watermark"),
97
+ title="Face Swap with Watermark",
98
  )
99
+
100
+ app.launch()