Skip to content

Commit

Permalink
Fix folder selection (#487)
Browse files Browse the repository at this point in the history
* update state keys

* use tk dialog for segments

---------

Co-authored-by: Josef Haupt <[email protected]>
  • Loading branch information
Josef-Haupt and Josef Haupt authored Oct 28, 2024
1 parent 2e86e4e commit 4f1b461
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
5 changes: 3 additions & 2 deletions birdnet_analyzer/gui/multi_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def build_multi_analysis_tab():
)

def select_directory_on_empty(): # Nishant - Function modified for For Folder selection
folder = gu.select_folder()
folder = gu.select_folder(state_key="batch-analysis-data-dir")

if folder:
files_and_durations = gu.get_files_and_durations(folder)
if len(files_and_durations) > 100:
Expand All @@ -112,7 +113,7 @@ def select_directory_on_empty(): # Nishant - Function modified for For Folder s
)

def select_directory_wrapper(): # Nishant - Function modified for For Folder selection
folder = gu.select_folder()
folder = gu.select_folder(state_key="batch-analysis-output-dir")
return (folder, folder) if folder else ("", "")

select_out_directory_btn.click(
Expand Down
6 changes: 2 additions & 4 deletions birdnet_analyzer/gui/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,10 @@ def select_subdir(new_value: str, next_review_state: dict):
return {review_state: next_review_state}

def start_review(next_review_state):
initial_dir = loc.get_state("review-input-dir", "")
dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir)
dir_name = gu.select_folder(state_key="review-input-dir")

if dir_name:
loc.set_state("review-input-dir", dir_name[0])
next_review_state["input_directory"] = dir_name[0]
next_review_state["input_directory"] = dir_name
specieslist = [e.name for e in os.scandir(next_review_state["input_directory"]) if e.is_dir()]

if not specieslist:
Expand Down
9 changes: 4 additions & 5 deletions birdnet_analyzer/gui/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ def build_species_tab():
)

def select_directory_and_update_tb(name_tb):
initial_dir = loc.get_state("species-output-dir", "")
dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir)
dir_name = gu.select_folder(state_key="species-output-dir")

if dir_name:
loc.set_state("species-output-dir", dir_name[0])
loc.set_state("species-output-dir", dir_name)
return (
dir_name[0],
gr.Textbox(label=dir_name[0] + "\\", visible=True, value=name_tb),
dir_name,
gr.Textbox(label=dir_name[0] + os.sep, visible=True, value=name_tb),
)

return None, name_tb
Expand Down
23 changes: 7 additions & 16 deletions birdnet_analyzer/gui/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ def select_subdirectories(state_key=None):
Returns:
A tuples of (directory, list of subdirectories) or (None, None) if the dialog was canceled.
"""

initial_dir = loc.get_state(state_key, "") if state_key else ""
dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir)
dir_name = gu.select_folder(state_key=state_key)

if dir_name:
if state_key:
loc.set_state(state_key, dir_name[0])

subdirs = utils.list_subdirectories(dir_name[0])
subdirs = utils.list_subdirectories(dir_name)
labels = []

for folder in subdirs:
Expand All @@ -37,7 +32,7 @@ def select_subdirectories(state_key=None):
if not label in labels:
labels.append(label)

return dir_name[0], [[label] for label in sorted(labels)]
return dir_name, [[label] for label in sorted(labels)]

return None, None

Expand Down Expand Up @@ -224,14 +219,12 @@ def build_train_tab():
)

def select_directory_and_update_tb():
initial_dir = loc.get_state("train-output-dir", "")
dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir)
dir_name = gu.select_folder(state_key="train-output-dir")

if dir_name:
loc.set_state("train-output-dir", dir_name[0])
return (
dir_name[0],
gr.Textbox(label=dir_name[0] + "\\", visible=True),
dir_name,
gr.Textbox(label=dir_name + os.sep, visible=True),
gr.Radio(visible=True, interactive=True),
)

Expand Down Expand Up @@ -404,11 +397,9 @@ def on_crop_select(new_crop_mode):
)

def select_directory_and_update():
initial_dir = loc.get_state("train-data-cache-file-output", "")
dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir)
dir_name = gu.select_folder(state_key="train-data-cache-file-output")

if dir_name:
loc.set_state("train-data-cache-file-output", dir_name[0])
return (
dir_name[0],
gr.Textbox(label=dir_name[0] + "\\", visible=True),
Expand Down
10 changes: 7 additions & 3 deletions birdnet_analyzer/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

APPDIR.mkdir(parents=True, exist_ok=True)

sys.stderr = sys.stdout = open(str(APPDIR / "logs.txt"), "w")
sys.stderr = sys.stdout = open(str(APPDIR / "logs.txt"), "a")
cfg.ERROR_LOG_FILE = str(APPDIR / cfg.ERROR_LOG_FILE)
FROZEN = True
else:
Expand All @@ -47,15 +47,19 @@


# Nishant - Following two functions (select_folder andget_files_and_durations) are written for Folder selection
def select_folder():
def select_folder(state_key=None):
from tkinter import Tk, filedialog

tk = Tk()
tk.withdraw()
initial_dir = loc.get_state(state_key, None) if state_key else None

folder_selected = filedialog.askdirectory()
folder_selected = filedialog.askdirectory(initialdir=initial_dir)
tk.destroy()

if folder_selected and state_key:
loc.set_state(state_key, folder_selected)

return folder_selected


Expand Down
2 changes: 1 addition & 1 deletion birdnet_analyzer/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_state_dict() -> dict:
return json.load(f)
except FileNotFoundError:
try:
with open(STATE_SETTINGS_PATH, "w") as f:
with open(STATE_SETTINGS_PATH, "w", encoding="utf-8") as f:
json.dump({}, f)
return {}
except Exception as e:
Expand Down

0 comments on commit 4f1b461

Please sign in to comment.