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import webbrowser
|
3 |
+
import customtkinter as ctk
|
4 |
+
from typing import Callable, Tuple
|
5 |
+
import cv2
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
|
8 |
+
import roop.globals
|
9 |
+
import roop.metadata
|
10 |
+
from roop.face_analyser import get_one_face
|
11 |
+
from roop.capturer import get_video_frame, get_video_frame_total
|
12 |
+
from roop.predicter import predict_frame
|
13 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
14 |
+
from roop.utilities import is_image, is_video, resolve_relative_path
|
15 |
+
|
16 |
+
ROOT = None
|
17 |
+
ROOT_HEIGHT = 700
|
18 |
+
ROOT_WIDTH = 600
|
19 |
+
|
20 |
+
PREVIEW = None
|
21 |
+
PREVIEW_MAX_HEIGHT = 700
|
22 |
+
PREVIEW_MAX_WIDTH = 1200
|
23 |
+
|
24 |
+
RECENT_DIRECTORY_SOURCE = None
|
25 |
+
RECENT_DIRECTORY_TARGET = None
|
26 |
+
RECENT_DIRECTORY_OUTPUT = None
|
27 |
+
|
28 |
+
preview_label = None
|
29 |
+
preview_slider = None
|
30 |
+
source_label = None
|
31 |
+
target_label = None
|
32 |
+
status_label = None
|
33 |
+
|
34 |
+
|
35 |
+
def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
36 |
+
global ROOT, PREVIEW
|
37 |
+
|
38 |
+
ROOT = create_root(start, destroy)
|
39 |
+
PREVIEW = create_preview(ROOT)
|
40 |
+
|
41 |
+
return ROOT
|
42 |
+
|
43 |
+
|
44 |
+
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
45 |
+
global source_label, target_label, status_label
|
46 |
+
|
47 |
+
ctk.deactivate_automatic_dpi_awareness()
|
48 |
+
ctk.set_appearance_mode('system')
|
49 |
+
ctk.set_default_color_theme(resolve_relative_path('ui.json'))
|
50 |
+
|
51 |
+
root = ctk.CTk()
|
52 |
+
root.minsize(ROOT_WIDTH, ROOT_HEIGHT)
|
53 |
+
root.title(f'{roop.metadata.name} {roop.metadata.version}')
|
54 |
+
root.configure()
|
55 |
+
root.protocol('WM_DELETE_WINDOW', lambda: destroy())
|
56 |
+
|
57 |
+
source_label = ctk.CTkLabel(root, text=None)
|
58 |
+
source_label.place(relx=0.1, rely=0.1, relwidth=0.3, relheight=0.25)
|
59 |
+
|
60 |
+
target_label = ctk.CTkLabel(root, text=None)
|
61 |
+
target_label.place(relx=0.6, rely=0.1, relwidth=0.3, relheight=0.25)
|
62 |
+
|
63 |
+
source_button = ctk.CTkButton(root, text='Select a face', cursor='hand2', command=lambda: select_source_path())
|
64 |
+
source_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
|
65 |
+
|
66 |
+
target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path())
|
67 |
+
target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
|
68 |
+
|
69 |
+
keep_fps_value = ctk.BooleanVar(value=roop.globals.keep_fps)
|
70 |
+
keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(roop.globals, 'keep_fps', not roop.globals.keep_fps))
|
71 |
+
keep_fps_checkbox.place(relx=0.1, rely=0.6)
|
72 |
+
|
73 |
+
keep_frames_value = ctk.BooleanVar(value=roop.globals.keep_frames)
|
74 |
+
keep_frames_switch = ctk.CTkSwitch(root, text='Keep frames', variable=keep_frames_value, cursor='hand2', command=lambda: setattr(roop.globals, 'keep_frames', keep_frames_value.get()))
|
75 |
+
keep_frames_switch.place(relx=0.1, rely=0.65)
|
76 |
+
|
77 |
+
keep_audio_value = ctk.BooleanVar(value=roop.globals.keep_audio)
|
78 |
+
keep_audio_switch = ctk.CTkSwitch(root, text='Keep audio', variable=keep_audio_value, cursor='hand2', command=lambda: setattr(roop.globals, 'keep_audio', keep_audio_value.get()))
|
79 |
+
keep_audio_switch.place(relx=0.6, rely=0.6)
|
80 |
+
|
81 |
+
many_faces_value = ctk.BooleanVar(value=roop.globals.many_faces)
|
82 |
+
many_faces_switch = ctk.CTkSwitch(root, text='Many faces', variable=many_faces_value, cursor='hand2', command=lambda: setattr(roop.globals, 'many_faces', many_faces_value.get()))
|
83 |
+
many_faces_switch.place(relx=0.6, rely=0.65)
|
84 |
+
|
85 |
+
start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start))
|
86 |
+
start_button.place(relx=0.15, rely=0.75, relwidth=0.2, relheight=0.05)
|
87 |
+
|
88 |
+
stop_button = ctk.CTkButton(root, text='Destroy', cursor='hand2', command=lambda: destroy())
|
89 |
+
stop_button.place(relx=0.4, rely=0.75, relwidth=0.2, relheight=0.05)
|
90 |
+
|
91 |
+
preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview())
|
92 |
+
preview_button.place(relx=0.65, rely=0.75, relwidth=0.2, relheight=0.05)
|
93 |
+
|
94 |
+
status_label = ctk.CTkLabel(root, text=None, justify='center')
|
95 |
+
status_label.place(relx=0.1, rely=0.9, relwidth=0.8)
|
96 |
+
|
97 |
+
donate_label = ctk.CTkLabel(root, text='^_^ Donate to project ^_^', justify='center', cursor='hand2')
|
98 |
+
donate_label.place(relx=0.1, rely=0.95, relwidth=0.8)
|
99 |
+
donate_label.configure(text_color=ctk.ThemeManager.theme.get('RoopDonate').get('text_color'))
|
100 |
+
donate_label.bind('<Button>', lambda event: webbrowser.open('https://github.com/sponsors/s0md3v'))
|
101 |
+
|
102 |
+
return root
|
103 |
+
|
104 |
+
|
105 |
+
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
106 |
+
global preview_label, preview_slider
|
107 |
+
|
108 |
+
preview = ctk.CTkToplevel(parent)
|
109 |
+
preview.withdraw()
|
110 |
+
preview.title('Preview')
|
111 |
+
preview.configure()
|
112 |
+
preview.protocol('WM_DELETE_WINDOW', lambda: toggle_preview())
|
113 |
+
preview.resizable(width=False, height=False)
|
114 |
+
|
115 |
+
preview_label = ctk.CTkLabel(preview, text=None)
|
116 |
+
preview_label.pack(fill='both', expand=True)
|
117 |
+
|
118 |
+
preview_slider = ctk.CTkSlider(preview, from_=0, to=0, command=lambda frame_value: update_preview(frame_value))
|
119 |
+
|
120 |
+
return preview
|
121 |
+
|
122 |
+
|
123 |
+
def update_status(text: str) -> None:
|
124 |
+
status_label.configure(text=text)
|
125 |
+
ROOT.update()
|
126 |
+
|
127 |
+
|
128 |
+
def select_source_path() -> None:
|
129 |
+
global RECENT_DIRECTORY_SOURCE
|
130 |
+
|
131 |
+
PREVIEW.withdraw()
|
132 |
+
source_path = ctk.filedialog.askopenfilename(title='select an source image', initialdir=RECENT_DIRECTORY_SOURCE)
|
133 |
+
if is_image(source_path):
|
134 |
+
roop.globals.source_path = source_path
|
135 |
+
RECENT_DIRECTORY_SOURCE = os.path.dirname(roop.globals.source_path)
|
136 |
+
image = render_image_preview(roop.globals.source_path, (200, 200))
|
137 |
+
source_label.configure(image=image)
|
138 |
+
else:
|
139 |
+
roop.globals.source_path = None
|
140 |
+
source_label.configure(image=None)
|
141 |
+
|
142 |
+
|
143 |
+
def select_target_path() -> None:
|
144 |
+
global RECENT_DIRECTORY_TARGET
|
145 |
+
|
146 |
+
PREVIEW.withdraw()
|
147 |
+
target_path = ctk.filedialog.askopenfilename(title='select an target image or video', initialdir=RECENT_DIRECTORY_TARGET)
|
148 |
+
if is_image(target_path):
|
149 |
+
roop.globals.target_path = target_path
|
150 |
+
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
151 |
+
image = render_image_preview(roop.globals.target_path, (200, 200))
|
152 |
+
target_label.configure(image=image)
|
153 |
+
elif is_video(target_path):
|
154 |
+
roop.globals.target_path = target_path
|
155 |
+
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
156 |
+
video_frame = render_video_preview(target_path, (200, 200))
|
157 |
+
target_label.configure(image=video_frame)
|
158 |
+
else:
|
159 |
+
roop.globals.target_path = None
|
160 |
+
target_label.configure(image=None)
|
161 |
+
|
162 |
+
|
163 |
+
def select_output_path(start: Callable[[], None]) -> None:
|
164 |
+
global RECENT_DIRECTORY_OUTPUT
|
165 |
+
|
166 |
+
if is_image(roop.globals.target_path):
|
167 |
+
output_path = ctk.filedialog.asksaveasfilename(title='save image output file', defaultextension='.png', initialfile='output.png', initialdir=RECENT_DIRECTORY_OUTPUT)
|
168 |
+
elif is_video(roop.globals.target_path):
|
169 |
+
output_path = ctk.filedialog.asksaveasfilename(title='save video output file', defaultextension='.mp4', initialfile='output.mp4', initialdir=RECENT_DIRECTORY_OUTPUT)
|
170 |
+
else:
|
171 |
+
output_path = None
|
172 |
+
if output_path:
|
173 |
+
roop.globals.output_path = output_path
|
174 |
+
RECENT_DIRECTORY_OUTPUT = os.path.dirname(roop.globals.output_path)
|
175 |
+
start()
|
176 |
+
|
177 |
+
|
178 |
+
def render_image_preview(image_path: str, size: Tuple[int, int]) -> ctk.CTkImage:
|
179 |
+
image = Image.open(image_path)
|
180 |
+
if size:
|
181 |
+
image = ImageOps.fit(image, size, Image.LANCZOS)
|
182 |
+
return ctk.CTkImage(image, size=image.size)
|
183 |
+
|
184 |
+
|
185 |
+
def render_video_preview(video_path: str, size: Tuple[int, int], frame_number: int = 0) -> ctk.CTkImage:
|
186 |
+
capture = cv2.VideoCapture(video_path)
|
187 |
+
if frame_number:
|
188 |
+
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
189 |
+
has_frame, frame = capture.read()
|
190 |
+
if has_frame:
|
191 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
192 |
+
if size:
|
193 |
+
image = ImageOps.fit(image, size, Image.LANCZOS)
|
194 |
+
return ctk.CTkImage(image, size=image.size)
|
195 |
+
capture.release()
|
196 |
+
cv2.destroyAllWindows()
|
197 |
+
|
198 |
+
|
199 |
+
def toggle_preview() -> None:
|
200 |
+
if PREVIEW.state() == 'normal':
|
201 |
+
PREVIEW.withdraw()
|
202 |
+
elif roop.globals.source_path and roop.globals.target_path:
|
203 |
+
init_preview()
|
204 |
+
update_preview()
|
205 |
+
PREVIEW.deiconify()
|
206 |
+
|
207 |
+
|
208 |
+
def init_preview() -> None:
|
209 |
+
if is_image(roop.globals.target_path):
|
210 |
+
preview_slider.pack_forget()
|
211 |
+
if is_video(roop.globals.target_path):
|
212 |
+
video_frame_total = get_video_frame_total(roop.globals.target_path)
|
213 |
+
preview_slider.configure(to=video_frame_total)
|
214 |
+
preview_slider.pack(fill='x')
|
215 |
+
preview_slider.set(0)
|
216 |
+
|
217 |
+
|
218 |
+
def update_preview(frame_number: int = 0) -> None:
|
219 |
+
if roop.globals.source_path and roop.globals.target_path:
|
220 |
+
temp_frame = get_video_frame(roop.globals.target_path, frame_number)
|
221 |
+
if predict_frame(temp_frame):
|
222 |
+
quit()
|
223 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
224 |
+
temp_frame = frame_processor.process_frame(
|
225 |
+
get_one_face(cv2.imread(roop.globals.source_path)),
|
226 |
+
temp_frame
|
227 |
+
)
|
228 |
+
image = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB))
|
229 |
+
image = ImageOps.contain(image, (PREVIEW_MAX_WIDTH, PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
230 |
+
image = ctk.CTkImage(image, size=image.size)
|
231 |
+
preview_label.configure(image=image)
|