Update ui.py

I pushed it to premain
pull/877/head
Makaru 2025-01-06 23:31:33 +08:00 committed by Kenneth Estanislao
parent 09f0343639
commit d07d4a6a26
1 changed files with 60 additions and 15 deletions

View File

@ -262,6 +262,7 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
command=lambda: (
setattr(modules.globals, "map_faces", map_faces.get()),
save_switch_states(),
close_mapper_window() if not map_faces.get() else None
),
)
map_faces_switch.place(relx=0.1, rely=0.75)
@ -376,6 +377,15 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
return root
def close_mapper_window():
global POPUP, POPUP_LIVE
if POPUP and POPUP.winfo_exists():
POPUP.destroy()
POPUP = None
if POPUP_LIVE and POPUP_LIVE.winfo_exists():
POPUP_LIVE.destroy()
POPUP_LIVE = None
def analyze_target(start: Callable[[], None], root: ctk.CTk):
if POPUP != None and POPUP.winfo_exists():
@ -495,7 +505,7 @@ def update_popup_source(
x_min, y_min, x_max, y_max = face["bbox"]
map[button_num]["source"] = {
"cv2": cv2_img[int(y_min) : int(y_max), int(x_min) : int(x_max)],
"cv2": cv2_img[int(y_min): int(y_max), int(x_min): int(x_max)],
"face": face,
}
@ -760,6 +770,13 @@ def update_preview(frame_number: int = 0) -> None:
def webcam_preview(root: ctk.CTk, camera_index: int):
global POPUP_LIVE
if POPUP_LIVE and POPUP_LIVE.winfo_exists():
update_status("Source x Target Mapper is already open.")
POPUP_LIVE.focus()
return
if not modules.globals.map_faces:
if modules.globals.source_path is None:
update_status("Please select a source image first")
@ -772,6 +789,7 @@ def webcam_preview(root: ctk.CTk, camera_index: int):
)
def get_available_cameras():
"""Returns a list of available camera names and indices."""
if platform.system() == "Windows":
@ -946,9 +964,9 @@ def create_source_target_popup_for_webcam(
def on_submit_click():
if has_valid_map():
POPUP_LIVE.destroy()
simplify_maps()
create_webcam_preview(camera_index)
update_pop_live_status("Mappings successfully submitted!")
create_webcam_preview(camera_index) # Open the preview window
else:
update_pop_live_status("At least 1 source with target is required!")
@ -957,16 +975,43 @@ def create_source_target_popup_for_webcam(
refresh_data(map)
update_pop_live_status("Please provide mapping!")
def on_clear_click():
clear_source_target_images(map)
refresh_data(map)
update_pop_live_status("All mappings cleared!")
popup_status_label_live = ctk.CTkLabel(POPUP_LIVE, text=None, justify="center")
popup_status_label_live.grid(row=1, column=0, pady=15)
add_button = ctk.CTkButton(POPUP_LIVE, text="Add", command=lambda: on_add_click())
add_button.place(relx=0.2, rely=0.92, relwidth=0.2, relheight=0.05)
add_button.place(relx=0.1, rely=0.92, relwidth=0.2, relheight=0.05)
clear_button = ctk.CTkButton(POPUP_LIVE, text="Clear", command=lambda: on_clear_click())
clear_button.place(relx=0.4, rely=0.92, relwidth=0.2, relheight=0.05)
close_button = ctk.CTkButton(
POPUP_LIVE, text="Submit", command=lambda: on_submit_click()
)
close_button.place(relx=0.6, rely=0.92, relwidth=0.2, relheight=0.05)
close_button.place(relx=0.7, rely=0.92, relwidth=0.2, relheight=0.05)
def clear_source_target_images(map: list):
global source_label_dict_live, target_label_dict_live
for item in map:
if "source" in item:
del item["source"]
if "target" in item:
del item["target"]
for button_num in list(source_label_dict_live.keys()):
source_label_dict_live[button_num].destroy()
del source_label_dict_live[button_num]
for button_num in list(target_label_dict_live.keys()):
target_label_dict_live[button_num].destroy()
del target_label_dict_live[button_num]
def refresh_data(map: list):
@ -1075,7 +1120,7 @@ def update_webcam_source(
x_min, y_min, x_max, y_max = face["bbox"]
map[button_num]["source"] = {
"cv2": cv2_img[int(y_min) : int(y_max), int(x_min) : int(x_max)],
"cv2": cv2_img[int(y_min): int(y_max), int(x_min): int(x_max)],
"face": face,
}
@ -1127,7 +1172,7 @@ def update_webcam_target(
x_min, y_min, x_max, y_max = face["bbox"]
map[button_num]["target"] = {
"cv2": cv2_img[int(y_min) : int(y_max), int(x_min) : int(x_max)],
"cv2": cv2_img[int(y_min): int(y_max), int(x_min): int(x_max)],
"face": face,
}