parent
229375465d
commit
16c1b44927
|
@ -2,38 +2,19 @@ from typing import Any
|
||||||
import cv2
|
import cv2
|
||||||
import modules.globals # Import the globals to check the color correction toggle
|
import modules.globals # Import the globals to check the color correction toggle
|
||||||
|
|
||||||
def list_available_cameras(max_tested: int = 10):
|
|
||||||
""" List all available camera indices. """
|
|
||||||
available_cameras = []
|
|
||||||
for i in range(max_tested):
|
|
||||||
cap = cv2.VideoCapture(i)
|
|
||||||
if cap.isOpened():
|
|
||||||
available_cameras.append(i)
|
|
||||||
cap.release()
|
|
||||||
return available_cameras
|
|
||||||
|
|
||||||
def get_video_frame(video_source: Any, frame_number: int = 0, is_camera: bool = False) -> Any:
|
def get_video_frame(video_path: str, frame_number: int = 0) -> Any:
|
||||||
"""
|
capture = cv2.VideoCapture(video_path)
|
||||||
Capture a video frame from a camera or video file.
|
|
||||||
|
|
||||||
:param video_source: The camera index or video file path.
|
|
||||||
:param frame_number: Frame number to retrieve (only applicable for video files).
|
|
||||||
:param is_camera: Flag to indicate if the source is a camera.
|
|
||||||
:return: The captured frame.
|
|
||||||
"""
|
|
||||||
capture = cv2.VideoCapture(video_source)
|
|
||||||
|
|
||||||
# Set MJPEG format to ensure correct color space handling
|
# Set MJPEG format to ensure correct color space handling
|
||||||
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
|
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
|
||||||
|
|
||||||
# Only force RGB conversion if color correction is enabled
|
# Only force RGB conversion if color correction is enabled
|
||||||
if modules.globals.color_correction:
|
if modules.globals.color_correction:
|
||||||
capture.set(cv2.CAP_PROP_CONVERT_RGB, 1)
|
capture.set(cv2.CAP_PROP_CONVERT_RGB, 1)
|
||||||
|
|
||||||
if not is_camera:
|
frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
||||||
frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
|
||||||
capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
|
|
||||||
|
|
||||||
has_frame, frame = capture.read()
|
has_frame, frame = capture.read()
|
||||||
|
|
||||||
if has_frame and modules.globals.color_correction:
|
if has_frame and modules.globals.color_correction:
|
||||||
|
@ -43,8 +24,8 @@ def get_video_frame(video_source: Any, frame_number: int = 0, is_camera: bool =
|
||||||
capture.release()
|
capture.release()
|
||||||
return frame if has_frame else None
|
return frame if has_frame else None
|
||||||
|
|
||||||
|
|
||||||
def get_video_frame_total(video_path: str) -> int:
|
def get_video_frame_total(video_path: str) -> int:
|
||||||
""" Get total frame count of a video file. """
|
|
||||||
capture = cv2.VideoCapture(video_path)
|
capture = cv2.VideoCapture(video_path)
|
||||||
video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
capture.release()
|
capture.release()
|
||||||
|
|
348
modules/ui.py
348
modules/ui.py
|
@ -7,12 +7,14 @@ from PIL import Image, ImageOps
|
||||||
|
|
||||||
import modules.globals
|
import modules.globals
|
||||||
import modules.metadata
|
import modules.metadata
|
||||||
from modules.face_analyser import get_one_face
|
from modules.face_analyser import get_one_face, get_unique_faces_from_target_image, get_unique_faces_from_target_video, add_blank_map, has_valid_map, simplify_maps
|
||||||
from modules.capturer import get_video_frame, get_video_frame_total
|
from modules.capturer import get_video_frame, get_video_frame_total
|
||||||
from modules.processors.frame.core import get_frame_processors_modules
|
from modules.processors.frame.core import get_frame_processors_modules
|
||||||
from modules.utilities import is_image, is_video, resolve_relative_path, has_image_extension
|
from modules.utilities import is_image, is_video, resolve_relative_path, has_image_extension
|
||||||
|
|
||||||
ROOT = None
|
ROOT = None
|
||||||
|
POPUP = None
|
||||||
|
POPUP_LIVE = None
|
||||||
ROOT_HEIGHT = 700
|
ROOT_HEIGHT = 700
|
||||||
ROOT_WIDTH = 600
|
ROOT_WIDTH = 600
|
||||||
|
|
||||||
|
@ -22,6 +24,22 @@ PREVIEW_MAX_WIDTH = 1200
|
||||||
PREVIEW_DEFAULT_WIDTH = 960
|
PREVIEW_DEFAULT_WIDTH = 960
|
||||||
PREVIEW_DEFAULT_HEIGHT = 540
|
PREVIEW_DEFAULT_HEIGHT = 540
|
||||||
|
|
||||||
|
POPUP_WIDTH = 750
|
||||||
|
POPUP_HEIGHT = 810
|
||||||
|
POPUP_SCROLL_WIDTH = 740,
|
||||||
|
POPUP_SCROLL_HEIGHT = 700
|
||||||
|
|
||||||
|
POPUP_LIVE_WIDTH = 900
|
||||||
|
POPUP_LIVE_HEIGHT = 820
|
||||||
|
POPUP_LIVE_SCROLL_WIDTH = 890,
|
||||||
|
POPUP_LIVE_SCROLL_HEIGHT = 700
|
||||||
|
|
||||||
|
MAPPER_PREVIEW_MAX_HEIGHT = 100
|
||||||
|
MAPPER_PREVIEW_MAX_WIDTH = 100
|
||||||
|
|
||||||
|
DEFAULT_BUTTON_WIDTH = 200
|
||||||
|
DEFAULT_BUTTON_HEIGHT = 40
|
||||||
|
|
||||||
RECENT_DIRECTORY_SOURCE = None
|
RECENT_DIRECTORY_SOURCE = None
|
||||||
RECENT_DIRECTORY_TARGET = None
|
RECENT_DIRECTORY_TARGET = None
|
||||||
RECENT_DIRECTORY_OUTPUT = None
|
RECENT_DIRECTORY_OUTPUT = None
|
||||||
|
@ -31,6 +49,11 @@ preview_slider = None
|
||||||
source_label = None
|
source_label = None
|
||||||
target_label = None
|
target_label = None
|
||||||
status_label = None
|
status_label = None
|
||||||
|
popup_status_label = None
|
||||||
|
popup_status_label_live = None
|
||||||
|
source_label_dict = {}
|
||||||
|
source_label_dict_live = {}
|
||||||
|
target_label_dict_live = {}
|
||||||
|
|
||||||
img_ft, vid_ft = modules.globals.file_types
|
img_ft, vid_ft = modules.globals.file_types
|
||||||
|
|
||||||
|
@ -42,16 +65,6 @@ def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
||||||
PREVIEW = create_preview(ROOT)
|
PREVIEW = create_preview(ROOT)
|
||||||
|
|
||||||
return ROOT
|
return ROOT
|
||||||
|
|
||||||
def list_available_cameras(max_tested: int = 10):
|
|
||||||
""" List all available camera indices. """
|
|
||||||
available_cameras = []
|
|
||||||
for i in range(max_tested):
|
|
||||||
cap = cv2.VideoCapture(i)
|
|
||||||
if cap.isOpened():
|
|
||||||
available_cameras.append(i)
|
|
||||||
cap.release()
|
|
||||||
return available_cameras
|
|
||||||
|
|
||||||
|
|
||||||
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
||||||
|
@ -77,21 +90,11 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
|
||||||
select_face_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
|
select_face_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
|
||||||
|
|
||||||
swap_faces_button = ctk.CTkButton(root, text='↔', cursor='hand2', command=lambda: swap_faces_paths())
|
swap_faces_button = ctk.CTkButton(root, text='↔', cursor='hand2', command=lambda: swap_faces_paths())
|
||||||
swap_faces_button.place(relx=0.45, relwidth=0.1, relheight=0.1)
|
swap_faces_button.place(relx=0.45, rely=0.4, relwidth=0.1, relheight=0.1)
|
||||||
|
|
||||||
select_target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path())
|
select_target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path())
|
||||||
select_target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
|
select_target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
|
||||||
|
|
||||||
# Create dropdown for camera selection
|
|
||||||
available_cameras = list_available_cameras()
|
|
||||||
if available_cameras:
|
|
||||||
camera_var = ctk.StringVar(value=str(available_cameras[0])) # Default to the first available camera
|
|
||||||
camera_dropdown = ctk.CTkOptionMenu(root, variable=camera_var, values=[str(i) for i in available_cameras])
|
|
||||||
camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05)
|
|
||||||
else:
|
|
||||||
camera_var = ctk.StringVar(value="No cameras found")
|
|
||||||
camera_dropdown = ctk.CTkLabel(root, text="No cameras found")
|
|
||||||
camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05)
|
|
||||||
keep_fps_value = ctk.BooleanVar(value=modules.globals.keep_fps)
|
keep_fps_value = ctk.BooleanVar(value=modules.globals.keep_fps)
|
||||||
keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_fps', not modules.globals.keep_fps))
|
keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_fps', not modules.globals.keep_fps))
|
||||||
keep_fps_checkbox.place(relx=0.1, rely=0.6)
|
keep_fps_checkbox.place(relx=0.1, rely=0.6)
|
||||||
|
@ -121,9 +124,12 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
|
||||||
# nsfw_value = ctk.BooleanVar(value=modules.globals.nsfw_filter)
|
# nsfw_value = ctk.BooleanVar(value=modules.globals.nsfw_filter)
|
||||||
# nsfw_switch = ctk.CTkSwitch(root, text='NSFW filter', variable=nsfw_value, cursor='hand2', command=lambda: setattr(modules.globals, 'nsfw_filter', nsfw_value.get()))
|
# nsfw_switch = ctk.CTkSwitch(root, text='NSFW filter', variable=nsfw_value, cursor='hand2', command=lambda: setattr(modules.globals, 'nsfw_filter', nsfw_value.get()))
|
||||||
# nsfw_switch.place(relx=0.6, rely=0.7)
|
# nsfw_switch.place(relx=0.6, rely=0.7)
|
||||||
|
|
||||||
|
|
||||||
start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start))
|
map_faces = ctk.BooleanVar(value=modules.globals.map_faces)
|
||||||
|
map_faces_switch = ctk.CTkSwitch(root, text='Map faces', variable=map_faces, cursor='hand2', command=lambda: setattr(modules.globals, 'map_faces', map_faces.get()))
|
||||||
|
map_faces_switch.place(relx=0.1, rely=0.75)
|
||||||
|
|
||||||
|
start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: analyze_target(start, root))
|
||||||
start_button.place(relx=0.15, rely=0.80, relwidth=0.2, relheight=0.05)
|
start_button.place(relx=0.15, rely=0.80, relwidth=0.2, relheight=0.05)
|
||||||
|
|
||||||
stop_button = ctk.CTkButton(root, text='Destroy', cursor='hand2', command=lambda: destroy())
|
stop_button = ctk.CTkButton(root, text='Destroy', cursor='hand2', command=lambda: destroy())
|
||||||
|
@ -132,7 +138,7 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
|
||||||
preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview())
|
preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview())
|
||||||
preview_button.place(relx=0.65, rely=0.80, relwidth=0.2, relheight=0.05)
|
preview_button.place(relx=0.65, rely=0.80, relwidth=0.2, relheight=0.05)
|
||||||
|
|
||||||
live_button = ctk.CTkButton(root, text='Live', cursor='hand2', command=lambda: webcam_preview(int(camera_var.get())))
|
live_button = ctk.CTkButton(root, text='Live', cursor='hand2', command=lambda: webcam_preview(root))
|
||||||
live_button.place(relx=0.40, rely=0.86, relwidth=0.2, relheight=0.05)
|
live_button.place(relx=0.40, rely=0.86, relwidth=0.2, relheight=0.05)
|
||||||
|
|
||||||
status_label = ctk.CTkLabel(root, text=None, justify='center')
|
status_label = ctk.CTkLabel(root, text=None, justify='center')
|
||||||
|
@ -145,7 +151,108 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
|
def analyze_target(start: Callable[[], None], root: ctk.CTk):
|
||||||
|
if POPUP != None and POPUP.winfo_exists():
|
||||||
|
update_status("Please complete pop-up or close it.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if modules.globals.map_faces:
|
||||||
|
modules.globals.souce_target_map = []
|
||||||
|
|
||||||
|
if is_image(modules.globals.target_path):
|
||||||
|
update_status('Getting unique faces')
|
||||||
|
get_unique_faces_from_target_image()
|
||||||
|
elif is_video(modules.globals.target_path):
|
||||||
|
update_status('Getting unique faces')
|
||||||
|
get_unique_faces_from_target_video()
|
||||||
|
|
||||||
|
if len(modules.globals.souce_target_map) > 0:
|
||||||
|
create_source_target_popup(start, root, modules.globals.souce_target_map)
|
||||||
|
else:
|
||||||
|
update_status("No faces found in target")
|
||||||
|
else:
|
||||||
|
select_output_path(start)
|
||||||
|
|
||||||
|
def create_source_target_popup(start: Callable[[], None], root: ctk.CTk, map: list) -> None:
|
||||||
|
global POPUP, popup_status_label
|
||||||
|
|
||||||
|
POPUP = ctk.CTkToplevel(root)
|
||||||
|
POPUP.title("Source x Target Mapper")
|
||||||
|
POPUP.geometry(f"{POPUP_WIDTH}x{POPUP_HEIGHT}")
|
||||||
|
POPUP.focus()
|
||||||
|
|
||||||
|
def on_submit_click(start):
|
||||||
|
if has_valid_map():
|
||||||
|
POPUP.destroy()
|
||||||
|
select_output_path(start)
|
||||||
|
else:
|
||||||
|
update_pop_status("Atleast 1 source with target is required!")
|
||||||
|
|
||||||
|
scrollable_frame = ctk.CTkScrollableFrame(POPUP, width=POPUP_SCROLL_WIDTH, height=POPUP_SCROLL_HEIGHT)
|
||||||
|
scrollable_frame.grid(row=0, column=0, padx=0, pady=0, sticky='nsew')
|
||||||
|
|
||||||
|
def on_button_click(map, button_num):
|
||||||
|
map = update_popup_source(scrollable_frame, map, button_num)
|
||||||
|
|
||||||
|
for item in map:
|
||||||
|
id = item['id']
|
||||||
|
|
||||||
|
button = ctk.CTkButton(scrollable_frame, text="Select source image", command=lambda id=id: on_button_click(map, id), width=DEFAULT_BUTTON_WIDTH, height=DEFAULT_BUTTON_HEIGHT)
|
||||||
|
button.grid(row=id, column=0, padx=50, pady=10)
|
||||||
|
|
||||||
|
x_label = ctk.CTkLabel(scrollable_frame, text=f"X", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
x_label.grid(row=id, column=2, padx=10, pady=10)
|
||||||
|
|
||||||
|
image = Image.fromarray(cv2.cvtColor(item['target']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
target_image = ctk.CTkLabel(scrollable_frame, text=f"T-{id}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
target_image.grid(row=id, column=3, padx=10, pady=10)
|
||||||
|
target_image.configure(image=tk_image)
|
||||||
|
|
||||||
|
popup_status_label = ctk.CTkLabel(POPUP, text=None, justify='center')
|
||||||
|
popup_status_label.grid(row=1, column=0, pady=15)
|
||||||
|
|
||||||
|
close_button = ctk.CTkButton(POPUP, text="Submit", command=lambda: on_submit_click(start))
|
||||||
|
close_button.grid(row=2, column=0, pady=10)
|
||||||
|
|
||||||
|
|
||||||
|
def update_popup_source(scrollable_frame: ctk.CTkScrollableFrame, map: list, button_num: int) -> list:
|
||||||
|
global source_label_dict
|
||||||
|
|
||||||
|
source_path = ctk.filedialog.askopenfilename(title='select an source image', initialdir=RECENT_DIRECTORY_SOURCE, filetypes=[img_ft])
|
||||||
|
|
||||||
|
if "source" in map[button_num]:
|
||||||
|
map[button_num].pop("source")
|
||||||
|
source_label_dict[button_num].destroy()
|
||||||
|
del source_label_dict[button_num]
|
||||||
|
|
||||||
|
if source_path == "":
|
||||||
|
return map
|
||||||
|
else:
|
||||||
|
cv2_img = cv2.imread(source_path)
|
||||||
|
face = get_one_face(cv2_img)
|
||||||
|
|
||||||
|
if face:
|
||||||
|
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)],
|
||||||
|
'face' : face
|
||||||
|
}
|
||||||
|
|
||||||
|
image = Image.fromarray(cv2.cvtColor(map[button_num]['source']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
source_image = ctk.CTkLabel(scrollable_frame, text=f"S-{button_num}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
source_image.grid(row=button_num, column=1, padx=10, pady=10)
|
||||||
|
source_image.configure(image=tk_image)
|
||||||
|
source_label_dict[button_num] = source_image
|
||||||
|
else:
|
||||||
|
update_pop_status("Face could not be detected in last upload!")
|
||||||
|
return map
|
||||||
|
|
||||||
|
|
||||||
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
||||||
|
@ -170,6 +277,11 @@ def update_status(text: str) -> None:
|
||||||
status_label.configure(text=text)
|
status_label.configure(text=text)
|
||||||
ROOT.update()
|
ROOT.update()
|
||||||
|
|
||||||
|
def update_pop_status(text: str) -> None:
|
||||||
|
popup_status_label.configure(text=text)
|
||||||
|
|
||||||
|
def update_pop_live_status(text: str) -> None:
|
||||||
|
popup_status_label_live.configure(text=text)
|
||||||
|
|
||||||
def update_tumbler(var: str, value: bool) -> None:
|
def update_tumbler(var: str, value: bool) -> None:
|
||||||
modules.globals.fp_ui[var] = value
|
modules.globals.fp_ui[var] = value
|
||||||
|
@ -338,14 +450,20 @@ def update_preview(frame_number: int = 0) -> None:
|
||||||
update_status('Processing succeed!')
|
update_status('Processing succeed!')
|
||||||
PREVIEW.deiconify()
|
PREVIEW.deiconify()
|
||||||
|
|
||||||
def webcam_preview(camera_index=0):
|
def webcam_preview(root: ctk.CTk):
|
||||||
if modules.globals.source_path is None:
|
if not modules.globals.map_faces:
|
||||||
# No image selected
|
if modules.globals.source_path is None:
|
||||||
return
|
# No image selected
|
||||||
|
return
|
||||||
|
create_webcam_preview()
|
||||||
|
else:
|
||||||
|
modules.globals.souce_target_map = []
|
||||||
|
create_source_target_popup_for_webcam(root, modules.globals.souce_target_map)
|
||||||
|
|
||||||
|
def create_webcam_preview():
|
||||||
global preview_label, PREVIEW
|
global preview_label, PREVIEW
|
||||||
|
|
||||||
camera = cv2.VideoCapture(camera_index) # Use the selected camera index
|
camera = cv2.VideoCapture(0) # Use index for the webcam (adjust the index accordingly if necessary)
|
||||||
camera.set(cv2.CAP_PROP_FRAME_WIDTH, PREVIEW_DEFAULT_WIDTH) # Set the width of the resolution
|
camera.set(cv2.CAP_PROP_FRAME_WIDTH, PREVIEW_DEFAULT_WIDTH) # Set the width of the resolution
|
||||||
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, PREVIEW_DEFAULT_HEIGHT) # Set the height of the resolution
|
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, PREVIEW_DEFAULT_HEIGHT) # Set the height of the resolution
|
||||||
camera.set(cv2.CAP_PROP_FPS, 60) # Set the frame rate of the webcam
|
camera.set(cv2.CAP_PROP_FPS, 60) # Set the frame rate of the webcam
|
||||||
|
@ -363,20 +481,26 @@ def webcam_preview(camera_index=0):
|
||||||
if not ret:
|
if not ret:
|
||||||
break
|
break
|
||||||
|
|
||||||
# Select and save face image only once
|
temp_frame = frame.copy() #Create a copy of the frame
|
||||||
if source_image is None and modules.globals.source_path:
|
|
||||||
source_image = get_one_face(cv2.imread(modules.globals.source_path))
|
|
||||||
|
|
||||||
temp_frame = frame.copy() # Create a copy of the frame
|
|
||||||
|
|
||||||
if modules.globals.live_mirror:
|
if modules.globals.live_mirror:
|
||||||
temp_frame = cv2.flip(temp_frame, 1) # Horizontal flipping
|
temp_frame = cv2.flip(temp_frame, 1) # horizontal flipping
|
||||||
|
|
||||||
if modules.globals.live_resizable:
|
if modules.globals.live_resizable:
|
||||||
temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height())
|
temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height())
|
||||||
|
|
||||||
for frame_processor in frame_processors:
|
if not modules.globals.map_faces:
|
||||||
temp_frame = frame_processor.process_frame(source_image, temp_frame)
|
# Select and save face image only once
|
||||||
|
if source_image is None and modules.globals.source_path:
|
||||||
|
source_image = get_one_face(cv2.imread(modules.globals.source_path))
|
||||||
|
|
||||||
|
for frame_processor in frame_processors:
|
||||||
|
temp_frame = frame_processor.process_frame(source_image, temp_frame)
|
||||||
|
else:
|
||||||
|
modules.globals.target_path = None
|
||||||
|
|
||||||
|
for frame_processor in frame_processors:
|
||||||
|
temp_frame = frame_processor.process_frame_v2(temp_frame)
|
||||||
|
|
||||||
image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) # Convert the image to RGB format to display it with Tkinter
|
image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) # Convert the image to RGB format to display it with Tkinter
|
||||||
image = Image.fromarray(image)
|
image = Image.fromarray(image)
|
||||||
|
@ -390,3 +514,151 @@ def webcam_preview(camera_index=0):
|
||||||
|
|
||||||
camera.release()
|
camera.release()
|
||||||
PREVIEW.withdraw() # Close preview window when loop is finished
|
PREVIEW.withdraw() # Close preview window when loop is finished
|
||||||
|
|
||||||
|
|
||||||
|
def create_source_target_popup_for_webcam(root: ctk.CTk, map: list) -> None:
|
||||||
|
global POPUP_LIVE, popup_status_label_live
|
||||||
|
|
||||||
|
POPUP_LIVE = ctk.CTkToplevel(root)
|
||||||
|
POPUP_LIVE.title("Source x Target Mapper")
|
||||||
|
POPUP_LIVE.geometry(f"{POPUP_LIVE_WIDTH}x{POPUP_LIVE_HEIGHT}")
|
||||||
|
POPUP_LIVE.focus()
|
||||||
|
|
||||||
|
def on_submit_click():
|
||||||
|
if has_valid_map():
|
||||||
|
POPUP_LIVE.destroy()
|
||||||
|
simplify_maps()
|
||||||
|
create_webcam_preview()
|
||||||
|
else:
|
||||||
|
update_pop_live_status("Atleast 1 source with target is required!")
|
||||||
|
|
||||||
|
def on_add_click():
|
||||||
|
add_blank_map()
|
||||||
|
refresh_data(map)
|
||||||
|
update_pop_live_status("Please provide mapping!")
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_data(map: list):
|
||||||
|
global POPUP_LIVE
|
||||||
|
|
||||||
|
scrollable_frame = ctk.CTkScrollableFrame(POPUP_LIVE, width=POPUP_LIVE_SCROLL_WIDTH, height=POPUP_LIVE_SCROLL_HEIGHT)
|
||||||
|
scrollable_frame.grid(row=0, column=0, padx=0, pady=0, sticky='nsew')
|
||||||
|
|
||||||
|
def on_sbutton_click(map, button_num):
|
||||||
|
map = update_webcam_source(scrollable_frame, map, button_num)
|
||||||
|
|
||||||
|
def on_tbutton_click(map, button_num):
|
||||||
|
map = update_webcam_target(scrollable_frame, map, button_num)
|
||||||
|
|
||||||
|
for item in map:
|
||||||
|
id = item['id']
|
||||||
|
|
||||||
|
button = ctk.CTkButton(scrollable_frame, text="Select source image", command=lambda id=id: on_sbutton_click(map, id), width=DEFAULT_BUTTON_WIDTH, height=DEFAULT_BUTTON_HEIGHT)
|
||||||
|
button.grid(row=id, column=0, padx=30, pady=10)
|
||||||
|
|
||||||
|
x_label = ctk.CTkLabel(scrollable_frame, text=f"X", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
x_label.grid(row=id, column=2, padx=10, pady=10)
|
||||||
|
|
||||||
|
button = ctk.CTkButton(scrollable_frame, text="Select target image", command=lambda id=id: on_tbutton_click(map, id), width=DEFAULT_BUTTON_WIDTH, height=DEFAULT_BUTTON_HEIGHT)
|
||||||
|
button.grid(row=id, column=3, padx=20, pady=10)
|
||||||
|
|
||||||
|
if "source" in item:
|
||||||
|
image = Image.fromarray(cv2.cvtColor(item['source']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
source_image = ctk.CTkLabel(scrollable_frame, text=f"S-{id}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
source_image.grid(row=id, column=1, padx=10, pady=10)
|
||||||
|
source_image.configure(image=tk_image)
|
||||||
|
|
||||||
|
if "target" in item:
|
||||||
|
image = Image.fromarray(cv2.cvtColor(item['target']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
target_image = ctk.CTkLabel(scrollable_frame, text=f"T-{id}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
target_image.grid(row=id, column=4, padx=20, pady=10)
|
||||||
|
target_image.configure(image=tk_image)
|
||||||
|
|
||||||
|
|
||||||
|
def update_webcam_source(scrollable_frame: ctk.CTkScrollableFrame, map: list, button_num: int) -> list:
|
||||||
|
global source_label_dict_live
|
||||||
|
|
||||||
|
source_path = ctk.filedialog.askopenfilename(title='select an source image', initialdir=RECENT_DIRECTORY_SOURCE, filetypes=[img_ft])
|
||||||
|
|
||||||
|
if "source" in map[button_num]:
|
||||||
|
map[button_num].pop("source")
|
||||||
|
source_label_dict_live[button_num].destroy()
|
||||||
|
del source_label_dict_live[button_num]
|
||||||
|
|
||||||
|
if source_path == "":
|
||||||
|
return map
|
||||||
|
else:
|
||||||
|
cv2_img = cv2.imread(source_path)
|
||||||
|
face = get_one_face(cv2_img)
|
||||||
|
|
||||||
|
if face:
|
||||||
|
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)],
|
||||||
|
'face' : face
|
||||||
|
}
|
||||||
|
|
||||||
|
image = Image.fromarray(cv2.cvtColor(map[button_num]['source']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
source_image = ctk.CTkLabel(scrollable_frame, text=f"S-{button_num}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
source_image.grid(row=button_num, column=1, padx=10, pady=10)
|
||||||
|
source_image.configure(image=tk_image)
|
||||||
|
source_label_dict_live[button_num] = source_image
|
||||||
|
else:
|
||||||
|
update_pop_live_status("Face could not be detected in last upload!")
|
||||||
|
return map
|
||||||
|
|
||||||
|
def update_webcam_target(scrollable_frame: ctk.CTkScrollableFrame, map: list, button_num: int) -> list:
|
||||||
|
global target_label_dict_live
|
||||||
|
|
||||||
|
target_path = ctk.filedialog.askopenfilename(title='select an target image', initialdir=RECENT_DIRECTORY_SOURCE, filetypes=[img_ft])
|
||||||
|
|
||||||
|
if "target" in map[button_num]:
|
||||||
|
map[button_num].pop("target")
|
||||||
|
target_label_dict_live[button_num].destroy()
|
||||||
|
del target_label_dict_live[button_num]
|
||||||
|
|
||||||
|
if target_path == "":
|
||||||
|
return map
|
||||||
|
else:
|
||||||
|
cv2_img = cv2.imread(target_path)
|
||||||
|
face = get_one_face(cv2_img)
|
||||||
|
|
||||||
|
if face:
|
||||||
|
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)],
|
||||||
|
'face' : face
|
||||||
|
}
|
||||||
|
|
||||||
|
image = Image.fromarray(cv2.cvtColor(map[button_num]['target']['cv2'], cv2.COLOR_BGR2RGB))
|
||||||
|
image = image.resize((MAPPER_PREVIEW_MAX_WIDTH, MAPPER_PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
||||||
|
tk_image = ctk.CTkImage(image, size=image.size)
|
||||||
|
|
||||||
|
target_image = ctk.CTkLabel(scrollable_frame, text=f"T-{button_num}", width=MAPPER_PREVIEW_MAX_WIDTH, height=MAPPER_PREVIEW_MAX_HEIGHT)
|
||||||
|
target_image.grid(row=button_num, column=4, padx=20, pady=10)
|
||||||
|
target_image.configure(image=tk_image)
|
||||||
|
target_label_dict_live[button_num] = target_image
|
||||||
|
else:
|
||||||
|
update_pop_live_status("Face could not be detected in last upload!")
|
||||||
|
return map
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue