birdnet-analyzer 2.0.1__py3-none-any.whl → 2.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. birdnet_analyzer/__init__.py +9 -9
  2. birdnet_analyzer/analyze/__init__.py +19 -5
  3. birdnet_analyzer/analyze/__main__.py +3 -3
  4. birdnet_analyzer/analyze/cli.py +30 -25
  5. birdnet_analyzer/analyze/core.py +268 -241
  6. birdnet_analyzer/analyze/utils.py +700 -692
  7. birdnet_analyzer/audio.py +368 -368
  8. birdnet_analyzer/cli.py +732 -709
  9. birdnet_analyzer/config.py +243 -242
  10. birdnet_analyzer/eBird_taxonomy_codes_2024E.json +13046 -0
  11. birdnet_analyzer/embeddings/__init__.py +3 -3
  12. birdnet_analyzer/embeddings/__main__.py +3 -3
  13. birdnet_analyzer/embeddings/cli.py +12 -12
  14. birdnet_analyzer/embeddings/core.py +70 -69
  15. birdnet_analyzer/embeddings/utils.py +173 -179
  16. birdnet_analyzer/evaluation/__init__.py +189 -196
  17. birdnet_analyzer/evaluation/__main__.py +3 -3
  18. birdnet_analyzer/evaluation/assessment/metrics.py +388 -388
  19. birdnet_analyzer/evaluation/assessment/performance_assessor.py +364 -409
  20. birdnet_analyzer/evaluation/assessment/plotting.py +378 -379
  21. birdnet_analyzer/evaluation/preprocessing/data_processor.py +631 -631
  22. birdnet_analyzer/evaluation/preprocessing/utils.py +98 -98
  23. birdnet_analyzer/gui/__init__.py +19 -19
  24. birdnet_analyzer/gui/__main__.py +3 -3
  25. birdnet_analyzer/gui/analysis.py +179 -175
  26. birdnet_analyzer/gui/assets/arrow_down.svg +4 -4
  27. birdnet_analyzer/gui/assets/arrow_left.svg +4 -4
  28. birdnet_analyzer/gui/assets/arrow_right.svg +4 -4
  29. birdnet_analyzer/gui/assets/arrow_up.svg +4 -4
  30. birdnet_analyzer/gui/assets/gui.css +36 -28
  31. birdnet_analyzer/gui/assets/gui.js +89 -93
  32. birdnet_analyzer/gui/embeddings.py +638 -619
  33. birdnet_analyzer/gui/evaluation.py +801 -795
  34. birdnet_analyzer/gui/localization.py +75 -75
  35. birdnet_analyzer/gui/multi_file.py +265 -245
  36. birdnet_analyzer/gui/review.py +472 -519
  37. birdnet_analyzer/gui/segments.py +191 -191
  38. birdnet_analyzer/gui/settings.py +149 -128
  39. birdnet_analyzer/gui/single_file.py +264 -267
  40. birdnet_analyzer/gui/species.py +95 -95
  41. birdnet_analyzer/gui/train.py +687 -696
  42. birdnet_analyzer/gui/utils.py +803 -810
  43. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_af.txt +6522 -6522
  44. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ar.txt +6522 -6522
  45. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_bg.txt +6522 -6522
  46. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ca.txt +6522 -6522
  47. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_cs.txt +6522 -6522
  48. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_da.txt +6522 -6522
  49. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_de.txt +6522 -6522
  50. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_el.txt +6522 -6522
  51. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_en_uk.txt +6522 -6522
  52. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_es.txt +6522 -6522
  53. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_fi.txt +6522 -6522
  54. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_fr.txt +6522 -6522
  55. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_he.txt +6522 -6522
  56. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_hr.txt +6522 -6522
  57. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_hu.txt +6522 -6522
  58. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_in.txt +6522 -6522
  59. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_is.txt +6522 -6522
  60. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_it.txt +6522 -6522
  61. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ja.txt +6522 -6522
  62. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ko.txt +6522 -6522
  63. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_lt.txt +6522 -6522
  64. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ml.txt +6522 -6522
  65. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_nl.txt +6522 -6522
  66. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_no.txt +6522 -6522
  67. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pl.txt +6522 -6522
  68. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pt_BR.txt +6522 -6522
  69. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pt_PT.txt +6522 -6522
  70. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ro.txt +6522 -6522
  71. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ru.txt +6522 -6522
  72. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sk.txt +6522 -6522
  73. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sl.txt +6522 -6522
  74. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sr.txt +6522 -6522
  75. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sv.txt +6522 -6522
  76. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_th.txt +6522 -6522
  77. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_tr.txt +6522 -6522
  78. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_uk.txt +6522 -6522
  79. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_zh.txt +6522 -6522
  80. birdnet_analyzer/lang/de.json +342 -334
  81. birdnet_analyzer/lang/en.json +342 -334
  82. birdnet_analyzer/lang/fi.json +342 -334
  83. birdnet_analyzer/lang/fr.json +342 -334
  84. birdnet_analyzer/lang/id.json +342 -334
  85. birdnet_analyzer/lang/pt-br.json +342 -334
  86. birdnet_analyzer/lang/ru.json +342 -334
  87. birdnet_analyzer/lang/se.json +342 -334
  88. birdnet_analyzer/lang/tlh.json +342 -334
  89. birdnet_analyzer/lang/zh_TW.json +342 -334
  90. birdnet_analyzer/model.py +1213 -1212
  91. birdnet_analyzer/search/__init__.py +3 -3
  92. birdnet_analyzer/search/__main__.py +3 -3
  93. birdnet_analyzer/search/cli.py +11 -11
  94. birdnet_analyzer/search/core.py +78 -78
  95. birdnet_analyzer/search/utils.py +104 -107
  96. birdnet_analyzer/segments/__init__.py +3 -3
  97. birdnet_analyzer/segments/__main__.py +3 -3
  98. birdnet_analyzer/segments/cli.py +13 -13
  99. birdnet_analyzer/segments/core.py +81 -81
  100. birdnet_analyzer/segments/utils.py +383 -383
  101. birdnet_analyzer/species/__init__.py +3 -3
  102. birdnet_analyzer/species/__main__.py +3 -3
  103. birdnet_analyzer/species/cli.py +13 -13
  104. birdnet_analyzer/species/core.py +35 -35
  105. birdnet_analyzer/species/utils.py +73 -74
  106. birdnet_analyzer/train/__init__.py +3 -3
  107. birdnet_analyzer/train/__main__.py +3 -3
  108. birdnet_analyzer/train/cli.py +13 -13
  109. birdnet_analyzer/train/core.py +113 -113
  110. birdnet_analyzer/train/utils.py +878 -877
  111. birdnet_analyzer/translate.py +132 -133
  112. birdnet_analyzer/utils.py +425 -426
  113. {birdnet_analyzer-2.0.1.dist-info → birdnet_analyzer-2.1.1.dist-info}/METADATA +147 -137
  114. birdnet_analyzer-2.1.1.dist-info/RECORD +124 -0
  115. {birdnet_analyzer-2.0.1.dist-info → birdnet_analyzer-2.1.1.dist-info}/WHEEL +1 -1
  116. {birdnet_analyzer-2.0.1.dist-info → birdnet_analyzer-2.1.1.dist-info}/licenses/LICENSE +18 -18
  117. birdnet_analyzer/eBird_taxonomy_codes_2021E.json +0 -25280
  118. birdnet_analyzer/playground.py +0 -5
  119. birdnet_analyzer-2.0.1.dist-info/RECORD +0 -125
  120. {birdnet_analyzer-2.0.1.dist-info → birdnet_analyzer-2.1.1.dist-info}/entry_points.txt +0 -0
  121. {birdnet_analyzer-2.0.1.dist-info → birdnet_analyzer-2.1.1.dist-info}/top_level.txt +0 -0
@@ -1,267 +1,264 @@
1
- import os
2
-
3
- import gradio as gr
4
-
5
- import birdnet_analyzer.config as cfg
6
- import birdnet_analyzer.gui.localization as loc
7
- import birdnet_analyzer.gui.utils as gu
8
- from birdnet_analyzer import audio, utils
9
-
10
-
11
- @gu.gui_runtime_error_handler
12
- def run_single_file_analysis(
13
- input_path,
14
- use_top_n,
15
- top_n,
16
- confidence,
17
- sensitivity,
18
- overlap,
19
- merge_consecutive,
20
- audio_speed,
21
- fmin,
22
- fmax,
23
- species_list_choice,
24
- species_list_file,
25
- lat,
26
- lon,
27
- week,
28
- use_yearlong,
29
- sf_thresh,
30
- custom_classifier_file,
31
- locale,
32
- ):
33
- import csv
34
- from datetime import timedelta
35
-
36
- from birdnet_analyzer.gui.analysis import run_analysis
37
-
38
- if species_list_choice == gu._CUSTOM_SPECIES:
39
- gu.validate(species_list_file, loc.localize("validation-no-species-list-selected"))
40
-
41
- gu.validate(input_path, loc.localize("validation-no-file-selected"))
42
-
43
- if fmin is None or fmax is None or fmin < cfg.SIG_FMIN or fmax > cfg.SIG_FMAX or fmin > fmax:
44
- raise gr.Error(f"{loc.localize('validation-no-valid-frequency')} [{cfg.SIG_FMIN}, {cfg.SIG_FMAX}]")
45
-
46
- result_filepath = run_analysis(
47
- input_path,
48
- None,
49
- use_top_n,
50
- top_n,
51
- confidence,
52
- sensitivity,
53
- overlap,
54
- merge_consecutive,
55
- audio_speed,
56
- fmin,
57
- fmax,
58
- species_list_choice,
59
- species_list_file,
60
- lat,
61
- lon,
62
- week,
63
- use_yearlong,
64
- sf_thresh,
65
- custom_classifier_file,
66
- "csv",
67
- None,
68
- locale if locale else "en",
69
- 1,
70
- 4,
71
- None,
72
- skip_existing=False,
73
- save_params=False,
74
- progress=None,
75
- )
76
-
77
- if not result_filepath:
78
- raise gr.Error(loc.localize("single-tab-analyze-file-error"))
79
-
80
- # read the result file to return the data to be displayed.
81
- with open(result_filepath, encoding="utf-8") as f:
82
- reader = csv.reader(f)
83
- data = list(reader)
84
- data = [lc[0:-1] for lc in data[1:]] # remove last column (file path) and first row (header)
85
-
86
- for row in data:
87
- for col_idx in range(2):
88
- seconds = float(row[col_idx])
89
- time_str = str(timedelta(seconds=seconds))
90
-
91
- if "." in time_str:
92
- time_str = time_str[: time_str.index(".") + 2]
93
-
94
- row[col_idx] = time_str
95
- row.insert(0, "")
96
-
97
- return data, gr.update(visible=True), result_filepath
98
-
99
-
100
- def build_single_analysis_tab():
101
- with gr.Tab(loc.localize("single-tab-title")):
102
- audio_input = gr.Audio(type="filepath", label=loc.localize("single-audio-label"), sources=["upload"])
103
-
104
- with gr.Group():
105
- spectogram_output = gr.Plot(
106
- label=loc.localize("review-tab-spectrogram-plot-label"), visible=False, show_label=False
107
- )
108
- generate_spectrogram_cb = gr.Checkbox(
109
- value=True,
110
- label=loc.localize("single-tab-spectrogram-checkbox-label"),
111
- info=loc.localize("single-tab-spectrogram-checkbox-info"),
112
- )
113
- audio_path_state = gr.State()
114
- table_path_state = gr.State()
115
-
116
- (
117
- use_top_n,
118
- top_n_input,
119
- confidence_slider,
120
- sensitivity_slider,
121
- overlap_slider,
122
- merge_consecutive_slider,
123
- audio_speed_slider,
124
- fmin_number,
125
- fmax_number,
126
- ) = gu.sample_sliders(False)
127
-
128
- (
129
- species_list_radio,
130
- species_file_input,
131
- lat_number,
132
- lon_number,
133
- week_number,
134
- sf_thresh_number,
135
- yearlong_checkbox,
136
- selected_classifier_state,
137
- map_plot,
138
- ) = gu.species_lists(False)
139
- locale_radio = gu.locale()
140
-
141
- single_file_analyze = gr.Button(
142
- loc.localize("analyze-start-button-label"), variant="huggingface", interactive=False
143
- )
144
-
145
- with gr.Row(visible=False) as action_row:
146
- table_download_button = gr.Button(
147
- loc.localize("single-tab-download-button-label"),
148
- )
149
- segment_audio = gr.Audio(
150
- autoplay=True, type="numpy", show_download_button=True, show_label=False, editable=False, visible=False
151
- )
152
-
153
- output_dataframe = gr.Dataframe(
154
- type="pandas",
155
- headers=[
156
- "",
157
- loc.localize("single-tab-output-header-start"),
158
- loc.localize("single-tab-output-header-end"),
159
- loc.localize("single-tab-output-header-sci-name"),
160
- loc.localize("single-tab-output-header-common-name"),
161
- loc.localize("single-tab-output-header-confidence"),
162
- ],
163
- elem_id="single-file-output",
164
- interactive=False,
165
- )
166
-
167
- def get_audio_path(i, generate_spectrogram):
168
- if i:
169
- try:
170
- return (
171
- i["path"],
172
- gr.Audio(label=os.path.basename(i["path"])),
173
- gr.Plot(visible=True, value=utils.spectrogram_from_file(i["path"], fig_size=(20, 4)))
174
- if generate_spectrogram
175
- else gr.Plot(visible=False),
176
- gr.Button(interactive=True),
177
- )
178
- except Exception as e:
179
- raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
180
- else:
181
- return None, None, gr.Plot(visible=False), gr.update(interactive=False)
182
-
183
- def try_generate_spectrogram(audio_path, generate_spectrogram):
184
- if audio_path and generate_spectrogram:
185
- try:
186
- return gr.Plot(
187
- visible=True, value=utils.spectrogram_from_file(audio_path["path"], fig_size=(20, 4))
188
- )
189
- except Exception as e:
190
- raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
191
- else:
192
- return gr.Plot()
193
-
194
- generate_spectrogram_cb.change(
195
- try_generate_spectrogram,
196
- inputs=[audio_input, generate_spectrogram_cb],
197
- outputs=spectogram_output,
198
- preprocess=False,
199
- )
200
-
201
- audio_input.change(
202
- get_audio_path,
203
- inputs=[audio_input, generate_spectrogram_cb],
204
- outputs=[audio_path_state, audio_input, spectogram_output, single_file_analyze],
205
- preprocess=False,
206
- )
207
-
208
- inputs = [
209
- audio_path_state,
210
- use_top_n,
211
- top_n_input,
212
- confidence_slider,
213
- sensitivity_slider,
214
- overlap_slider,
215
- merge_consecutive_slider,
216
- audio_speed_slider,
217
- fmin_number,
218
- fmax_number,
219
- species_list_radio,
220
- species_file_input,
221
- lat_number,
222
- lon_number,
223
- week_number,
224
- yearlong_checkbox,
225
- sf_thresh_number,
226
- selected_classifier_state,
227
- locale_radio,
228
- ]
229
-
230
- def time_to_seconds(time_str):
231
- try:
232
- hours, minutes, seconds = time_str.split(":")
233
- return int(hours) * 3600 + int(minutes) * 60 + float(seconds)
234
-
235
- except ValueError as e:
236
- raise ValueError("Input must be in the format hh:mm:ss or hh:mm:ss.ssssss with numeric values.") from e
237
-
238
- def get_selected_audio(evt: gr.SelectData, audio_path):
239
- if evt.index[1] == 0 and evt.row_value[1] and evt.row_value[2]:
240
- start = time_to_seconds(evt.row_value[1])
241
- end = time_to_seconds(evt.row_value[2])
242
-
243
- data, sr = audio.open_audio_file(audio_path, offset=start, duration=end - start)
244
- return gr.update(visible=True, value=(sr, data))
245
-
246
- return gr.update()
247
-
248
- def download_table(filepath):
249
- if filepath:
250
- ext = os.path.splitext(filepath)[1]
251
- gu.save_file_dialog(
252
- state_key="single-file-table",
253
- default_filename=os.path.basename(filepath),
254
- filetypes=(f"{ext[1:]} (*{ext})",),
255
- )
256
-
257
- output_dataframe.select(get_selected_audio, inputs=audio_path_state, outputs=segment_audio)
258
- single_file_analyze.click(
259
- run_single_file_analysis, inputs=inputs, outputs=[output_dataframe, action_row, table_path_state]
260
- )
261
- table_download_button.click(download_table, inputs=table_path_state)
262
-
263
- return lat_number, lon_number, map_plot
264
-
265
-
266
- if __name__ == "__main__":
267
- gu.open_window(build_single_analysis_tab)
1
+ import os
2
+
3
+ import gradio as gr
4
+
5
+ import birdnet_analyzer.config as cfg
6
+ import birdnet_analyzer.gui.localization as loc
7
+ import birdnet_analyzer.gui.utils as gu
8
+ from birdnet_analyzer import audio, utils
9
+
10
+ MATPLOTLIB_FIGURE_NUM = "single-file-tab-spectrogram-plot"
11
+
12
+
13
+ @gu.gui_runtime_error_handler
14
+ def run_single_file_analysis(
15
+ input_path,
16
+ use_top_n,
17
+ top_n,
18
+ confidence,
19
+ sensitivity,
20
+ overlap,
21
+ merge_consecutive,
22
+ audio_speed,
23
+ fmin,
24
+ fmax,
25
+ species_list_choice,
26
+ species_list_file,
27
+ lat,
28
+ lon,
29
+ week,
30
+ use_yearlong,
31
+ sf_thresh,
32
+ custom_classifier_file,
33
+ locale,
34
+ ):
35
+ import csv
36
+ from datetime import timedelta
37
+
38
+ from birdnet_analyzer.gui.analysis import run_analysis
39
+
40
+ if species_list_choice == gu._CUSTOM_SPECIES:
41
+ gu.validate(species_list_file, loc.localize("validation-no-species-list-selected"))
42
+
43
+ gu.validate(input_path, loc.localize("validation-no-file-selected"))
44
+
45
+ if fmin is None or fmax is None or fmin < cfg.SIG_FMIN or fmax > cfg.SIG_FMAX or fmin > fmax:
46
+ raise gr.Error(f"{loc.localize('validation-no-valid-frequency')} [{cfg.SIG_FMIN}, {cfg.SIG_FMAX}]")
47
+
48
+ result_filepath = run_analysis(
49
+ input_path,
50
+ None,
51
+ use_top_n,
52
+ top_n,
53
+ confidence,
54
+ sensitivity,
55
+ overlap,
56
+ merge_consecutive,
57
+ audio_speed,
58
+ fmin,
59
+ fmax,
60
+ species_list_choice,
61
+ species_list_file,
62
+ lat,
63
+ lon,
64
+ week,
65
+ use_yearlong,
66
+ sf_thresh,
67
+ custom_classifier_file,
68
+ "csv",
69
+ None,
70
+ False,
71
+ locale if locale else "en",
72
+ 1,
73
+ 4,
74
+ None,
75
+ skip_existing=False,
76
+ save_params=False,
77
+ progress=None,
78
+ )
79
+
80
+ if not result_filepath:
81
+ raise gr.Error(loc.localize("single-tab-analyze-file-error"))
82
+
83
+ # read the result file to return the data to be displayed.
84
+ with open(result_filepath, encoding="utf-8") as f:
85
+ reader = csv.reader(f)
86
+ data = list(reader)
87
+ data = [lc[0:-1] for lc in data[1:]] # remove last column (file path) and first row (header)
88
+
89
+ for row in data:
90
+ for col_idx in range(2):
91
+ seconds = float(row[col_idx])
92
+ time_str = str(timedelta(seconds=seconds))
93
+
94
+ if "." in time_str:
95
+ time_str = time_str[: time_str.index(".") + 2]
96
+
97
+ row[col_idx] = time_str
98
+ row.insert(0, "▶")
99
+
100
+ return data, gr.update(visible=True), result_filepath
101
+
102
+
103
+ def build_single_analysis_tab():
104
+ with gr.Tab(loc.localize("single-tab-title")):
105
+ audio_input = gr.Audio(type="filepath", label=loc.localize("single-audio-label"), sources=["upload"])
106
+
107
+ with gr.Group():
108
+ spectogram_output = gr.Plot(label=loc.localize("review-tab-spectrogram-plot-label"), visible=False, show_label=False)
109
+ generate_spectrogram_cb = gr.Checkbox(
110
+ value=True,
111
+ label=loc.localize("single-tab-spectrogram-checkbox-label"),
112
+ info=loc.localize("single-tab-spectrogram-checkbox-info"),
113
+ )
114
+ audio_path_state = gr.State()
115
+ table_path_state = gr.State()
116
+
117
+ (
118
+ use_top_n,
119
+ top_n_input,
120
+ confidence_slider,
121
+ sensitivity_slider,
122
+ overlap_slider,
123
+ merge_consecutive_slider,
124
+ audio_speed_slider,
125
+ fmin_number,
126
+ fmax_number,
127
+ ) = gu.sample_sliders(False)
128
+
129
+ (
130
+ species_list_radio,
131
+ species_file_input,
132
+ lat_number,
133
+ lon_number,
134
+ week_number,
135
+ sf_thresh_number,
136
+ yearlong_checkbox,
137
+ selected_classifier_state,
138
+ map_plot,
139
+ ) = gu.species_lists(False)
140
+ locale_radio = gu.locale()
141
+
142
+ single_file_analyze = gr.Button(loc.localize("analyze-start-button-label"), variant="huggingface", interactive=False)
143
+
144
+ with gr.Row(visible=False) as action_row:
145
+ table_download_button = gr.Button(
146
+ loc.localize("single-tab-download-button-label"),
147
+ )
148
+ segment_audio = gr.Audio(autoplay=True, type="numpy", show_download_button=True, show_label=False, editable=False, visible=False)
149
+
150
+ output_dataframe = gr.Dataframe(
151
+ type="pandas",
152
+ headers=[
153
+ "",
154
+ loc.localize("single-tab-output-header-start"),
155
+ loc.localize("single-tab-output-header-end"),
156
+ loc.localize("single-tab-output-header-sci-name"),
157
+ loc.localize("single-tab-output-header-common-name"),
158
+ loc.localize("single-tab-output-header-confidence"),
159
+ ],
160
+ elem_id="single-file-output",
161
+ interactive=False,
162
+ )
163
+
164
+ def get_audio_path(i, generate_spectrogram):
165
+ if i:
166
+ try:
167
+ return (
168
+ i["path"],
169
+ gr.Audio(label=os.path.basename(i["path"])),
170
+ gr.Plot(visible=True, value=utils.spectrogram_from_file(i["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
171
+ if generate_spectrogram
172
+ else gr.Plot(visible=False),
173
+ gr.Button(interactive=True),
174
+ )
175
+ except Exception as e:
176
+ raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
177
+ else:
178
+ return None, None, gr.Plot(visible=False), gr.update(interactive=False)
179
+
180
+ def try_generate_spectrogram(audio_path, generate_spectrogram):
181
+ if audio_path and generate_spectrogram:
182
+ try:
183
+ return gr.Plot(visible=True, value=utils.spectrogram_from_file(audio_path["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
184
+ except Exception as e:
185
+ raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
186
+ else:
187
+ return gr.Plot()
188
+
189
+ generate_spectrogram_cb.change(
190
+ try_generate_spectrogram,
191
+ inputs=[audio_input, generate_spectrogram_cb],
192
+ outputs=spectogram_output,
193
+ preprocess=False,
194
+ )
195
+
196
+ audio_input.change(
197
+ get_audio_path,
198
+ inputs=[audio_input, generate_spectrogram_cb],
199
+ outputs=[audio_path_state, audio_input, spectogram_output, single_file_analyze],
200
+ preprocess=False,
201
+ )
202
+
203
+ inputs = [
204
+ audio_path_state,
205
+ use_top_n,
206
+ top_n_input,
207
+ confidence_slider,
208
+ sensitivity_slider,
209
+ overlap_slider,
210
+ merge_consecutive_slider,
211
+ audio_speed_slider,
212
+ fmin_number,
213
+ fmax_number,
214
+ species_list_radio,
215
+ species_file_input,
216
+ lat_number,
217
+ lon_number,
218
+ week_number,
219
+ yearlong_checkbox,
220
+ sf_thresh_number,
221
+ selected_classifier_state,
222
+ locale_radio,
223
+ ]
224
+
225
+ def time_to_seconds(time_str):
226
+ try:
227
+ hours, minutes, seconds = time_str.split(":")
228
+ return int(hours) * 3600 + int(minutes) * 60 + float(seconds)
229
+
230
+ except ValueError as e:
231
+ raise ValueError("Input must be in the format hh:mm:ss or hh:mm:ss.ssssss with numeric values.") from e
232
+
233
+ def get_selected_audio(evt: gr.SelectData, audio_path):
234
+ if evt.index[1] == 0 and evt.row_value[1] and evt.row_value[2]:
235
+ start = time_to_seconds(evt.row_value[1])
236
+ end = time_to_seconds(evt.row_value[2])
237
+
238
+ data, sr = audio.open_audio_file(audio_path, offset=start, duration=end - start)
239
+ return gr.update(visible=True, value=(sr, data))
240
+
241
+ return gr.update()
242
+
243
+ def download_table(filepath):
244
+ if filepath:
245
+ ext = os.path.splitext(filepath)[1]
246
+ file_location = gu.save_file_dialog(
247
+ state_key="single-file-table",
248
+ default_filename=os.path.basename(filepath),
249
+ filetypes=(f"{ext[1:]} (*{ext})",),
250
+ )
251
+
252
+ if file_location:
253
+ with open(filepath, "rb") as src, open(file_location, "wb") as dst:
254
+ dst.write(src.read())
255
+
256
+ output_dataframe.select(get_selected_audio, inputs=audio_path_state, outputs=segment_audio)
257
+ single_file_analyze.click(run_single_file_analysis, inputs=inputs, outputs=[output_dataframe, action_row, table_path_state])
258
+ table_download_button.click(download_table, inputs=table_path_state)
259
+
260
+ return lat_number, lon_number, map_plot
261
+
262
+
263
+ if __name__ == "__main__":
264
+ gu.open_window(build_single_analysis_tab)