Spaces:
Running
Running
Update roop/ui.py
Browse files- roop/ui.py +22 -48
roop/ui.py
CHANGED
|
@@ -31,7 +31,6 @@ 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 |
|
|
@@ -40,7 +39,6 @@ def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
|
| 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 |
|
|
@@ -101,7 +99,6 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
|
|
| 101 |
|
| 102 |
return root
|
| 103 |
|
| 104 |
-
|
| 105 |
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
| 106 |
global preview_label, preview_slider
|
| 107 |
|
|
@@ -119,17 +116,15 @@ def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
|
| 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
|
| 133 |
if is_image(source_path):
|
| 134 |
roop.globals.source_path = source_path
|
| 135 |
RECENT_DIRECTORY_SOURCE = os.path.dirname(roop.globals.source_path)
|
|
@@ -139,12 +134,11 @@ def select_source_path() -> None:
|
|
| 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
|
| 148 |
if is_image(target_path):
|
| 149 |
roop.globals.target_path = target_path
|
| 150 |
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
|
@@ -159,7 +153,6 @@ def select_target_path() -> None:
|
|
| 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 |
|
|
@@ -174,58 +167,39 @@ def select_output_path(start: Callable[[], None]) -> None:
|
|
| 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 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
def toggle_preview() -> None:
|
| 200 |
-
if PREVIEW.
|
| 201 |
PREVIEW.withdraw()
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
| 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)
|
|
|
|
| 31 |
target_label = None
|
| 32 |
status_label = None
|
| 33 |
|
|
|
|
| 34 |
def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
| 35 |
global ROOT, PREVIEW
|
| 36 |
|
|
|
|
| 39 |
|
| 40 |
return ROOT
|
| 41 |
|
|
|
|
| 42 |
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
| 43 |
global source_label, target_label, status_label
|
| 44 |
|
|
|
|
| 99 |
|
| 100 |
return root
|
| 101 |
|
|
|
|
| 102 |
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
| 103 |
global preview_label, preview_slider
|
| 104 |
|
|
|
|
| 116 |
|
| 117 |
return preview
|
| 118 |
|
|
|
|
| 119 |
def update_status(text: str) -> None:
|
| 120 |
status_label.configure(text=text)
|
| 121 |
ROOT.update()
|
| 122 |
|
|
|
|
| 123 |
def select_source_path() -> None:
|
| 124 |
global RECENT_DIRECTORY_SOURCE
|
| 125 |
|
| 126 |
PREVIEW.withdraw()
|
| 127 |
+
source_path = ctk.filedialog.askopenfilename(title='select a source image', initialdir=RECENT_DIRECTORY_SOURCE)
|
| 128 |
if is_image(source_path):
|
| 129 |
roop.globals.source_path = source_path
|
| 130 |
RECENT_DIRECTORY_SOURCE = os.path.dirname(roop.globals.source_path)
|
|
|
|
| 134 |
roop.globals.source_path = None
|
| 135 |
source_label.configure(image=None)
|
| 136 |
|
|
|
|
| 137 |
def select_target_path() -> None:
|
| 138 |
global RECENT_DIRECTORY_TARGET
|
| 139 |
|
| 140 |
PREVIEW.withdraw()
|
| 141 |
+
target_path = ctk.filedialog.askopenfilename(title='select a target image or video', initialdir=RECENT_DIRECTORY_TARGET)
|
| 142 |
if is_image(target_path):
|
| 143 |
roop.globals.target_path = target_path
|
| 144 |
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
|
|
|
| 153 |
roop.globals.target_path = None
|
| 154 |
target_label.configure(image=None)
|
| 155 |
|
|
|
|
| 156 |
def select_output_path(start: Callable[[], None]) -> None:
|
| 157 |
global RECENT_DIRECTORY_OUTPUT
|
| 158 |
|
|
|
|
| 167 |
RECENT_DIRECTORY_OUTPUT = os.path.dirname(roop.globals.output_path)
|
| 168 |
start()
|
| 169 |
|
|
|
|
| 170 |
def render_image_preview(image_path: str, size: Tuple[int, int]) -> ctk.CTkImage:
|
| 171 |
image = Image.open(image_path)
|
| 172 |
if size:
|
| 173 |
image = ImageOps.fit(image, size, Image.LANCZOS)
|
| 174 |
return ctk.CTkImage(image, size=image.size)
|
| 175 |
|
|
|
|
| 176 |
def render_video_preview(video_path: str, size: Tuple[int, int], frame_number: int = 0) -> ctk.CTkImage:
|
| 177 |
capture = cv2.VideoCapture(video_path)
|
| 178 |
if frame_number:
|
| 179 |
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
| 180 |
has_frame, frame = capture.read()
|
| 181 |
+
if not has_frame:
|
| 182 |
+
return None
|
| 183 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 184 |
+
image = Image.fromarray(frame)
|
| 185 |
+
if size:
|
| 186 |
+
image = ImageOps.fit(image, size, Image.LANCZOS)
|
| 187 |
+
return ctk.CTkImage(image, size=image.size)
|
| 188 |
|
| 189 |
+
def update_preview(frame_value: float) -> None:
|
| 190 |
+
frame_number = int(frame_value)
|
| 191 |
+
preview_image = None
|
| 192 |
+
if is_video(roop.globals.target_path):
|
| 193 |
+
preview_image = render_video_preview(roop.globals.target_path, (PREVIEW_MAX_WIDTH, PREVIEW_MAX_HEIGHT), frame_number)
|
| 194 |
+
if preview_image:
|
| 195 |
+
preview_label.configure(image=preview_image)
|
| 196 |
|
| 197 |
def toggle_preview() -> None:
|
| 198 |
+
if PREVIEW.winfo_ismapped():
|
| 199 |
PREVIEW.withdraw()
|
| 200 |
+
else:
|
| 201 |
+
if is_video(roop.globals.target_path):
|
| 202 |
+
total_frames = get_video_frame_total(roop.globals.target_path)
|
| 203 |
+
preview_slider.configure(from_=0, to=total_frames, command=lambda frame_value: update_preview(frame_value))
|
| 204 |
PREVIEW.deiconify()
|
| 205 |
+
PREVIEW.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|