import os import re import customtkinter import json from scripts.theme import Theme def create_folder(folder_name): """ Creates a folder in the current directory with the specified folder name. Parameters: folder_name (str): The name of the folder to create. Returns: None """ try: # Construct the path where to create the new folder new_folder_path = os.path.join(os.getcwd(), folder_name) # Create the folder os.makedirs(new_folder_path, exist_ok=True) except Exception as e: print(f"An error occurred while creating the folder: {e}") return new_folder_path def load_json_file(): global data, window_width, window_height, window_state, skipable_frames ,img_format, used_threads, thread_detection_mode, thread_options, btn_img_size, data_mode, color, thickness # Check if the file exists if not os.path.exists(json_file_path): # Create the file with default values data = { "window_width": 800, "window_height": 1000, "window_state": "zoomed", # there are zoomed and normal "btn_img_size": (75, 75), "skipable_frames": 30, "img_format": ".jpg", "used_threads": calculate_automatic_threads(), "thread_detection_method": "automatic", "data_mode": "Resize", "color": "#5f00c7", "thickness": 4 } with open(json_file_path, 'w') as f: json.dump(data, f, indent=2) # Load the file with open(json_file_path, 'r') as f: data = json.load(f) window_width = data["window_width"] window_height = data["window_height"] window_state = data["window_state"] btn_img_size = data["btn_img_size"] skipable_frames = data["skipable_frames"] img_format = data["img_format"] used_threads = data["used_threads"] thread_detection_mode = data["thread_detection_method"] data_mode = data["data_mode"] color = data["color"] thickness = data["thickness"] thread_options = set_thread_options() def calculate_automatic_threads(): global used_threads pc_threads = os.cpu_count() used_threads = int(pc_threads) // 4 return used_threads def set_thread_options(): global used_threads, thread_options count = os.cpu_count() - 2 thread_options = [i for i in range(2, count, 2)] return thread_options def save_setting_change(): global data, used_threads, thread_detection_mode, img_format, data_mode, color, thickness # Get current window information if thread_detection_mode == "manual": thread_data = { "used_threads": used_threads, } else: thread_data = { "used_threads": calculate_automatic_threads(), } save_data = { "img_format": img_format, "skipable_frames": skipable_frames, "thread_detection_method": thread_detection_mode, "data_mode": data_mode, "color": color, "thickness": thickness, } save_data.update(thread_data) # Update only the changed values for key in save_data: if data[key] != save_data[key]: data[key] = save_data[key] # Save the updated JSON save_data with open(json_file_path, 'w') as f: json.dump(data, f, indent=2) def save_windows_information(geometry, state): global data """Save window information to JSON file.""" width, height, _, _ = map(int, re.findall(r'\d+', geometry)) # Get current window information if state == "zoomed": window_info = { "window_width": 800, "window_height": 1000, "window_state": state, } else: window_info = { 'window_width': width, 'window_height': height, "window_state": state, } # Update only the changed values for key in window_info: if data[key] != window_info[key]: data[key] = window_info[key] # Save the updated JSON data with open(json_file_path, 'w') as f: json.dump(data, f, indent=2) def save_img_btn_size(btn_size): global data window_info = { "btn_img_size": btn_size, } # Update only the changed values for key in window_info: if data[key] != window_info[key]: data[key] = window_info[key] # Save the updated JSON data with open(json_file_path, 'w') as f: json.dump(data, f, indent=2) def set_theme(): # Get the current working directory # Create the path to the Json file customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark customtkinter.set_default_color_theme(theme.get_theme()) data_path = create_folder("MPENNconfigs") json_file_path = data_path + "/program.json" theme = Theme(path=data_path) window_width = None window_height = None window_state = None btn_img_size = None skipable_frames = None img_format = None img_format_options = (".gif", ".jpg", ".png", ".tif") data_modes = ("Resize", "Labeling") thread_switcher = ("automatic", "manual") used_threads = None thread_detection_mode = None thread_options = [] data = {} data_mode = None color = None thickness = None