birdnet-analyzer 2.1.0__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 (120) hide show
  1. birdnet_analyzer/__init__.py +9 -9
  2. birdnet_analyzer/analyze/__init__.py +19 -19
  3. birdnet_analyzer/analyze/__main__.py +3 -3
  4. birdnet_analyzer/analyze/cli.py +30 -30
  5. birdnet_analyzer/analyze/core.py +268 -246
  6. birdnet_analyzer/analyze/utils.py +700 -694
  7. birdnet_analyzer/audio.py +368 -368
  8. birdnet_analyzer/cli.py +732 -732
  9. birdnet_analyzer/config.py +243 -243
  10. birdnet_analyzer/eBird_taxonomy_codes_2024E.json +13045 -13045
  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 -70
  15. birdnet_analyzer/embeddings/utils.py +173 -220
  16. birdnet_analyzer/evaluation/__init__.py +189 -189
  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 -364
  20. birdnet_analyzer/evaluation/assessment/plotting.py +378 -378
  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 -179
  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 -36
  31. birdnet_analyzer/gui/assets/gui.js +89 -93
  32. birdnet_analyzer/gui/embeddings.py +638 -638
  33. birdnet_analyzer/gui/evaluation.py +801 -801
  34. birdnet_analyzer/gui/localization.py +75 -75
  35. birdnet_analyzer/gui/multi_file.py +265 -265
  36. birdnet_analyzer/gui/review.py +472 -472
  37. birdnet_analyzer/gui/segments.py +191 -191
  38. birdnet_analyzer/gui/settings.py +149 -149
  39. birdnet_analyzer/gui/single_file.py +264 -264
  40. birdnet_analyzer/gui/species.py +95 -95
  41. birdnet_analyzer/gui/train.py +687 -687
  42. birdnet_analyzer/gui/utils.py +803 -797
  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 -341
  81. birdnet_analyzer/lang/en.json +342 -341
  82. birdnet_analyzer/lang/fi.json +342 -341
  83. birdnet_analyzer/lang/fr.json +342 -341
  84. birdnet_analyzer/lang/id.json +342 -341
  85. birdnet_analyzer/lang/pt-br.json +342 -341
  86. birdnet_analyzer/lang/ru.json +342 -341
  87. birdnet_analyzer/lang/se.json +342 -341
  88. birdnet_analyzer/lang/tlh.json +342 -341
  89. birdnet_analyzer/lang/zh_TW.json +342 -341
  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 -425
  113. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/METADATA +147 -146
  114. birdnet_analyzer-2.1.1.dist-info/RECORD +124 -0
  115. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/licenses/LICENSE +18 -18
  116. birdnet_analyzer/playground.py +0 -5
  117. birdnet_analyzer-2.1.0.dist-info/RECORD +0 -125
  118. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/WHEEL +0 -0
  119. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/entry_points.txt +0 -0
  120. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/top_level.txt +0 -0
@@ -1,383 +1,383 @@
1
- """Extract segments from audio files based on BirdNET detections.
2
-
3
- Can be used to save the segments of the audio files for each detection.
4
- """
5
-
6
- import os
7
-
8
- import numpy as np
9
-
10
- import birdnet_analyzer.config as cfg
11
- from birdnet_analyzer import audio, utils
12
-
13
- # Set numpy random seed
14
- RNG = np.random.default_rng(cfg.RANDOM_SEED)
15
- SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
16
-
17
-
18
- def detect_rtype(line: str):
19
- """Detects the type of result file.
20
-
21
- Args:
22
- line: First line of text.
23
-
24
- Returns:
25
- Either "table", "kaleidoscope", "csv" or "audacity".
26
- """
27
- if line.lower().startswith("selection"):
28
- return "table"
29
-
30
- if line.lower().startswith("indir"):
31
- return "kaleidoscope"
32
-
33
- if line.lower().startswith("start (s)"):
34
- return "csv"
35
-
36
- return "audacity"
37
-
38
-
39
- def get_header_mapping(line: str) -> dict:
40
- """
41
- Parses a header line and returns a mapping of column names to their indices.
42
-
43
- Args:
44
- line (str): A string representing the header line of a file.
45
-
46
- Returns:
47
- dict: A dictionary where the keys are column names and the values are their respective indices.
48
- """
49
- rtype = detect_rtype(line)
50
-
51
- sep = "\t" if rtype in ("table", "audacity") else ","
52
-
53
- cols = line.split(sep)
54
-
55
- return {col: i for i, col in enumerate(cols)}
56
-
57
-
58
- def parse_folders(apath: str, rpath: str, allowed_result_filetypes: tuple[str] = ("txt", "csv")) -> list[dict]:
59
- """Read audio and result files.
60
-
61
- Reads all audio files and BirdNET output inside directory recursively.
62
-
63
- Args:
64
- apath (str): Path to search for audio files.
65
- rpath (str): Path to search for result files.
66
- allowed_result_filetypes (tuple[str]): List of extensions for the result files.
67
-
68
- Returns:
69
- list[dict]: A list of {"audio": path_to_audio, "result": path_to_result }.
70
- """
71
- data = {}
72
- apath = apath.replace("/", os.sep).replace("\\", os.sep)
73
- rpath = rpath.replace("/", os.sep).replace("\\", os.sep)
74
-
75
- # Check if combined selection table is present and read that.
76
- if os.path.exists(os.path.join(rpath, cfg.OUTPUT_RAVEN_FILENAME)):
77
- # Read combined Raven selection table
78
- rfile = os.path.join(rpath, cfg.OUTPUT_RAVEN_FILENAME)
79
- data["combined"] = {"isCombinedFile": True, "result": rfile}
80
- elif os.path.exists(os.path.join(rpath, cfg.OUTPUT_CSV_FILENAME)):
81
- rfile = os.path.join(rpath, cfg.OUTPUT_CSV_FILENAME)
82
- data["combined"] = {"isCombinedFile": True, "result": rfile}
83
- elif os.path.exists(os.path.join(rpath, cfg.OUTPUT_KALEIDOSCOPE_FILENAME)):
84
- rfile = os.path.join(rpath, cfg.OUTPUT_KALEIDOSCOPE_FILENAME)
85
- data["combined"] = {"isCombinedFile": True, "result": rfile}
86
- else:
87
- # Get all audio files
88
- for root, _, files in os.walk(apath):
89
- for f in files:
90
- if f.rsplit(".", 1)[-1].lower() in cfg.ALLOWED_FILETYPES and not f.startswith("."):
91
- table_key = os.path.join(root.strip(apath), f.rsplit(".", 1)[0])
92
- data[table_key] = {"audio": os.path.join(root, f), "result": ""}
93
-
94
- # Get all result files
95
- for root, _, files in os.walk(rpath):
96
- for f in files:
97
- if f.rsplit(".", 1)[-1] in allowed_result_filetypes and ".BirdNET." in f:
98
- table_key = os.path.join(root.strip(rpath), f.split(".BirdNET.", 1)[0])
99
- if table_key in data:
100
- data[table_key]["result"] = os.path.join(root, f)
101
-
102
- # Convert to list
103
- flist = [f for f in data.values() if f["result"]]
104
-
105
- print(f"Found {len(flist)} audio files with valid result file.")
106
-
107
- return flist
108
-
109
-
110
- def parse_files(flist: list[dict], max_segments=100):
111
- """
112
- Parses a list of files to extract and organize bird call segments by species.
113
-
114
- Args:
115
- flist (list[dict]): A list of dictionaries, each containing 'audio' and 'result' file paths.
116
- Optionally, a dictionary can have 'isCombinedFile' set to True to indicate
117
- that it is a combined result file.
118
- max_segments (int, optional): The maximum number of segments to retain per species. Defaults to 100.
119
- Returns:
120
- list[tuple]: A list of tuples where each tuple contains an audio file path and a list of segments
121
- associated with that audio file.
122
- Raises:
123
- KeyError: If the dictionaries in flist do not contain the required keys ('audio' and 'result').
124
- Example:
125
- flist = [
126
- {"audio": "path/to/audio1.wav", "result": "path/to/result1.csv"},
127
- {"audio": "path/to/audio2.wav", "result": "path/to/result2.csv"}
128
- ]
129
- segments = parseFiles(flist, max_segments=50)
130
- """
131
- species_segments: dict[str, list] = {}
132
-
133
- is_combined_rfile = len(flist) == 1 and flist[0].get("isCombinedFile", False)
134
-
135
- if is_combined_rfile:
136
- rfile = flist[0]["result"]
137
- segments = find_segments_from_combined(rfile)
138
-
139
- # Parse segments by species
140
- for s in segments:
141
- if s["species"] not in species_segments:
142
- species_segments[s["species"]] = []
143
-
144
- species_segments[s["species"]].append(s)
145
- else:
146
- for f in flist:
147
- # Paths
148
- afile = f["audio"]
149
- rfile = f["result"]
150
-
151
- # Get all segments for result file
152
- segments = find_segments(afile, rfile)
153
-
154
- # Parse segments by species
155
- for s in segments:
156
- if s["species"] not in species_segments:
157
- species_segments[s["species"]] = []
158
-
159
- species_segments[s["species"]].append(s)
160
-
161
- # Shuffle segments for each species and limit to max_segments
162
- for s in species_segments:
163
- RNG.shuffle(species_segments[s])
164
- species_segments[s] = species_segments[s][:max_segments]
165
-
166
- # Make dict of segments per audio file
167
- segments: dict[str, list] = {}
168
- seg_cnt = 0
169
-
170
- for s in species_segments:
171
- for seg in species_segments[s]:
172
- if seg["audio"] not in segments:
173
- segments[seg["audio"]] = []
174
-
175
- segments[seg["audio"]].append(seg)
176
- seg_cnt += 1
177
-
178
- print(f"Found {seg_cnt} segments in {len(segments)} audio files.")
179
-
180
- # Convert to list
181
- return [tuple(e) for e in segments.items()]
182
-
183
-
184
- def find_segments_from_combined(rfile: str) -> list[dict]:
185
- """Extracts the segments from a combined results file
186
-
187
- Args:
188
- rfile (str): Path to the result file.
189
-
190
- Returns:
191
- list[dict]: A list of dicts in the form of
192
- {"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence}
193
- """
194
- segments: list[dict] = []
195
-
196
- # Open and parse result file
197
- lines = utils.read_lines(rfile)
198
-
199
- # Auto-detect result type
200
- rtype = detect_rtype(lines[0])
201
-
202
- if rtype == "audacity":
203
- raise Exception("Audacity files are not supported for combined results.")
204
-
205
- # Get mapping from the header column
206
- header_mapping = get_header_mapping(lines[0])
207
-
208
- # Get start and end times based on rtype
209
- confidence = 0
210
- start = end = 0.0
211
- species = ""
212
- afile = ""
213
-
214
- for i, line in enumerate(lines):
215
- if rtype == "table" and i > 0:
216
- d = line.split("\t")
217
- file_offset = float(d[header_mapping["File Offset (s)"]])
218
- start = file_offset
219
- end = file_offset + (float(d[header_mapping["End Time (s)"]]) - float(d[header_mapping["Begin Time (s)"]]))
220
- species = d[header_mapping["Common Name"]]
221
- confidence = float(d[header_mapping["Confidence"]])
222
- afile = d[header_mapping["Begin Path"]].replace("/", os.sep).replace("\\", os.sep)
223
-
224
- elif rtype == "kaleidoscope" and i > 0:
225
- d = line.split(",")
226
- start = float(d[header_mapping["OFFSET"]])
227
- end = float(d[header_mapping["DURATION"]]) + start
228
- species = d[header_mapping["scientific_name"]]
229
- confidence = float(d[header_mapping["confidence"]])
230
- in_dir = d[header_mapping["INDIR"]]
231
- folder = d[header_mapping["FOLDER"]]
232
- in_file = d[header_mapping["IN FILE"]]
233
- afile = os.path.join(in_dir, folder, in_file).replace("/", os.sep).replace("\\", os.sep)
234
-
235
- elif rtype == "csv" and i > 0:
236
- d = line.split(",")
237
- start = float(d[header_mapping["Start (s)"]])
238
- end = float(d[header_mapping["End (s)"]])
239
- species = d[header_mapping["Common name"]]
240
- confidence = float(d[header_mapping["Confidence"]])
241
- afile = d[header_mapping["File"]].replace("/", os.sep).replace("\\", os.sep)
242
-
243
- # Check if confidence is high enough and label is not "nocall"
244
- if confidence >= cfg.MIN_CONFIDENCE and species.lower() != "nocall" and afile:
245
- segments.append({"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence})
246
-
247
- return segments
248
-
249
-
250
- def find_segments(afile: str, rfile: str):
251
- """Extracts the segments for an audio file from the results file
252
-
253
- Args:
254
- afile: Path to the audio file.
255
- rfile: Path to the result file.
256
-
257
- Returns:
258
- A list of dicts in the form of
259
- {"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence}
260
- """
261
- segments: list[dict] = []
262
-
263
- # Open and parse result file
264
- lines = utils.read_lines(rfile)
265
-
266
- # Auto-detect result type
267
- rtype = detect_rtype(lines[0])
268
-
269
- # Get mapping from the header column
270
- header_mapping = get_header_mapping(lines[0])
271
-
272
- # Get start and end times based on rtype
273
- confidence = 0
274
- start = end = 0.0
275
- species = ""
276
-
277
- for i, line in enumerate(lines):
278
- if rtype == "table" and i > 0:
279
- d = line.split("\t")
280
- start = float(d[header_mapping["Begin Time (s)"]])
281
- end = float(d[header_mapping["End Time (s)"]])
282
- species = d[header_mapping["Common Name"]]
283
- confidence = float(d[header_mapping["Confidence"]])
284
-
285
- elif rtype == "audacity":
286
- d = line.split("\t")
287
- start = float(d[0])
288
- end = float(d[1])
289
- species = d[2].split(", ")[1]
290
- confidence = float(d[-1])
291
-
292
- elif rtype == "kaleidoscope" and i > 0:
293
- d = line.split(",")
294
- start = float(d[header_mapping["OFFSET"]])
295
- end = float(d[header_mapping["DURATION"]]) + start
296
- species = d[header_mapping["scientific_name"]]
297
- confidence = float(d[header_mapping["confidence"]])
298
-
299
- elif rtype == "csv" and i > 0:
300
- d = line.split(",")
301
- start = float(d[header_mapping["Start (s)"]])
302
- end = float(d[header_mapping["End (s)"]])
303
- species = d[header_mapping["Common name"]]
304
- confidence = float(d[header_mapping["Confidence"]])
305
-
306
- # Check if confidence is high enough and label is not "nocall"
307
- if confidence >= cfg.MIN_CONFIDENCE and species.lower() != "nocall":
308
- segments.append({"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence})
309
-
310
- return segments
311
-
312
-
313
- def extract_segments(item: tuple[tuple[str, list[dict]], float, dict[str]]):
314
- """
315
- Extracts audio segments from a given audio file based on provided segment information.
316
- Args:
317
- item (tuple): A tuple containing:
318
- - A tuple with:
319
- - A string representing the path to the audio file.
320
- - A list of dictionaries, each containing segment information with keys "start", "end", "species", "confidence", and "audio".
321
- - A float representing the segment length.
322
- - A dictionary containing configuration settings.
323
- Returns:
324
- bool: True if segments were successfully extracted, False otherwise.
325
- Raises:
326
- Exception: If there is an error opening the audio file or extracting segments.
327
- """
328
- # Paths and config
329
- afile = item[0][0]
330
- segments = item[0][1]
331
- seg_length = item[1]
332
- cfg.set_config(item[2])
333
-
334
- # Status
335
- print(f"Extracting segments from {afile}")
336
-
337
- try:
338
- # Open audio file
339
- sig, rate = audio.open_audio_file(afile, cfg.SAMPLE_RATE, speed=cfg.AUDIO_SPEED)
340
- except Exception as ex:
341
- print(f"Error: Cannot open audio file {afile}", flush=True)
342
- utils.write_error_log(ex)
343
-
344
- return None
345
-
346
- # Extract segments
347
- for seg_cnt, seg in enumerate(segments, 1):
348
- try:
349
- # Get start and end times
350
- start = int((seg["start"] * rate) / cfg.AUDIO_SPEED)
351
- end = int((seg["end"] * rate) / cfg.AUDIO_SPEED)
352
-
353
- offset = max(0, ((seg_length * rate) - (end - start)) // 2)
354
- start = max(0, start - offset)
355
- end = min(len(sig), end + offset)
356
-
357
- # Make sure segment is long enough
358
- if end > start:
359
- # Get segment raw audio from signal
360
- seg_sig = sig[int(start) : int(end)]
361
-
362
- # Make output path
363
- outpath = os.path.join(cfg.OUTPUT_PATH, seg["species"])
364
- os.makedirs(outpath, exist_ok=True)
365
-
366
- # Save segment
367
- seg_name = "{:.3f}_{}_{}_{:.1f}s_{:.1f}s.wav".format(
368
- seg["confidence"],
369
- seg_cnt,
370
- seg["audio"].rsplit(os.sep, 1)[-1].rsplit(".", 1)[0],
371
- seg["start"],
372
- seg["end"],
373
- )
374
- seg_path = os.path.join(outpath, seg_name)
375
- audio.save_signal(seg_sig, seg_path, rate)
376
-
377
- except Exception as ex:
378
- # Write error log
379
- print(f"Error: Cannot extract segments from {afile}.", flush=True)
380
- utils.write_error_log(ex)
381
- return False
382
-
383
- return True
1
+ """Extract segments from audio files based on BirdNET detections.
2
+
3
+ Can be used to save the segments of the audio files for each detection.
4
+ """
5
+
6
+ import os
7
+
8
+ import numpy as np
9
+
10
+ import birdnet_analyzer.config as cfg
11
+ from birdnet_analyzer import audio, utils
12
+
13
+ # Set numpy random seed
14
+ RNG = np.random.default_rng(cfg.RANDOM_SEED)
15
+ SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
16
+
17
+
18
+ def detect_rtype(line: str):
19
+ """Detects the type of result file.
20
+
21
+ Args:
22
+ line: First line of text.
23
+
24
+ Returns:
25
+ Either "table", "kaleidoscope", "csv" or "audacity".
26
+ """
27
+ if line.lower().startswith("selection"):
28
+ return "table"
29
+
30
+ if line.lower().startswith("indir"):
31
+ return "kaleidoscope"
32
+
33
+ if line.lower().startswith("start (s)"):
34
+ return "csv"
35
+
36
+ return "audacity"
37
+
38
+
39
+ def get_header_mapping(line: str) -> dict:
40
+ """
41
+ Parses a header line and returns a mapping of column names to their indices.
42
+
43
+ Args:
44
+ line (str): A string representing the header line of a file.
45
+
46
+ Returns:
47
+ dict: A dictionary where the keys are column names and the values are their respective indices.
48
+ """
49
+ rtype = detect_rtype(line)
50
+
51
+ sep = "\t" if rtype in ("table", "audacity") else ","
52
+
53
+ cols = line.split(sep)
54
+
55
+ return {col: i for i, col in enumerate(cols)}
56
+
57
+
58
+ def parse_folders(apath: str, rpath: str, allowed_result_filetypes: tuple[str] = ("txt", "csv")) -> list[dict]:
59
+ """Read audio and result files.
60
+
61
+ Reads all audio files and BirdNET output inside directory recursively.
62
+
63
+ Args:
64
+ apath (str): Path to search for audio files.
65
+ rpath (str): Path to search for result files.
66
+ allowed_result_filetypes (tuple[str]): List of extensions for the result files.
67
+
68
+ Returns:
69
+ list[dict]: A list of {"audio": path_to_audio, "result": path_to_result }.
70
+ """
71
+ data = {}
72
+ apath = apath.replace("/", os.sep).replace("\\", os.sep)
73
+ rpath = rpath.replace("/", os.sep).replace("\\", os.sep)
74
+
75
+ # Check if combined selection table is present and read that.
76
+ if os.path.exists(os.path.join(rpath, cfg.OUTPUT_RAVEN_FILENAME)):
77
+ # Read combined Raven selection table
78
+ rfile = os.path.join(rpath, cfg.OUTPUT_RAVEN_FILENAME)
79
+ data["combined"] = {"isCombinedFile": True, "result": rfile}
80
+ elif os.path.exists(os.path.join(rpath, cfg.OUTPUT_CSV_FILENAME)):
81
+ rfile = os.path.join(rpath, cfg.OUTPUT_CSV_FILENAME)
82
+ data["combined"] = {"isCombinedFile": True, "result": rfile}
83
+ elif os.path.exists(os.path.join(rpath, cfg.OUTPUT_KALEIDOSCOPE_FILENAME)):
84
+ rfile = os.path.join(rpath, cfg.OUTPUT_KALEIDOSCOPE_FILENAME)
85
+ data["combined"] = {"isCombinedFile": True, "result": rfile}
86
+ else:
87
+ # Get all audio files
88
+ for root, _, files in os.walk(apath):
89
+ for f in files:
90
+ if f.rsplit(".", 1)[-1].lower() in cfg.ALLOWED_FILETYPES and not f.startswith("."):
91
+ table_key = os.path.join(root.strip(apath), f.rsplit(".", 1)[0])
92
+ data[table_key] = {"audio": os.path.join(root, f), "result": ""}
93
+
94
+ # Get all result files
95
+ for root, _, files in os.walk(rpath):
96
+ for f in files:
97
+ if f.rsplit(".", 1)[-1] in allowed_result_filetypes and ".BirdNET." in f:
98
+ table_key = os.path.join(root.strip(rpath), f.split(".BirdNET.", 1)[0])
99
+ if table_key in data:
100
+ data[table_key]["result"] = os.path.join(root, f)
101
+
102
+ # Convert to list
103
+ flist = [f for f in data.values() if f["result"]]
104
+
105
+ print(f"Found {len(flist)} audio files with valid result file.")
106
+
107
+ return flist
108
+
109
+
110
+ def parse_files(flist: list[dict], max_segments=100):
111
+ """
112
+ Parses a list of files to extract and organize bird call segments by species.
113
+
114
+ Args:
115
+ flist (list[dict]): A list of dictionaries, each containing 'audio' and 'result' file paths.
116
+ Optionally, a dictionary can have 'isCombinedFile' set to True to indicate
117
+ that it is a combined result file.
118
+ max_segments (int, optional): The maximum number of segments to retain per species. Defaults to 100.
119
+ Returns:
120
+ list[tuple]: A list of tuples where each tuple contains an audio file path and a list of segments
121
+ associated with that audio file.
122
+ Raises:
123
+ KeyError: If the dictionaries in flist do not contain the required keys ('audio' and 'result').
124
+ Example:
125
+ flist = [
126
+ {"audio": "path/to/audio1.wav", "result": "path/to/result1.csv"},
127
+ {"audio": "path/to/audio2.wav", "result": "path/to/result2.csv"}
128
+ ]
129
+ segments = parseFiles(flist, max_segments=50)
130
+ """
131
+ species_segments: dict[str, list] = {}
132
+
133
+ is_combined_rfile = len(flist) == 1 and flist[0].get("isCombinedFile", False)
134
+
135
+ if is_combined_rfile:
136
+ rfile = flist[0]["result"]
137
+ segments = find_segments_from_combined(rfile)
138
+
139
+ # Parse segments by species
140
+ for s in segments:
141
+ if s["species"] not in species_segments:
142
+ species_segments[s["species"]] = []
143
+
144
+ species_segments[s["species"]].append(s)
145
+ else:
146
+ for f in flist:
147
+ # Paths
148
+ afile = f["audio"]
149
+ rfile = f["result"]
150
+
151
+ # Get all segments for result file
152
+ segments = find_segments(afile, rfile)
153
+
154
+ # Parse segments by species
155
+ for s in segments:
156
+ if s["species"] not in species_segments:
157
+ species_segments[s["species"]] = []
158
+
159
+ species_segments[s["species"]].append(s)
160
+
161
+ # Shuffle segments for each species and limit to max_segments
162
+ for s in species_segments:
163
+ RNG.shuffle(species_segments[s])
164
+ species_segments[s] = species_segments[s][:max_segments]
165
+
166
+ # Make dict of segments per audio file
167
+ segments: dict[str, list] = {}
168
+ seg_cnt = 0
169
+
170
+ for s in species_segments:
171
+ for seg in species_segments[s]:
172
+ if seg["audio"] not in segments:
173
+ segments[seg["audio"]] = []
174
+
175
+ segments[seg["audio"]].append(seg)
176
+ seg_cnt += 1
177
+
178
+ print(f"Found {seg_cnt} segments in {len(segments)} audio files.")
179
+
180
+ # Convert to list
181
+ return [tuple(e) for e in segments.items()]
182
+
183
+
184
+ def find_segments_from_combined(rfile: str) -> list[dict]:
185
+ """Extracts the segments from a combined results file
186
+
187
+ Args:
188
+ rfile (str): Path to the result file.
189
+
190
+ Returns:
191
+ list[dict]: A list of dicts in the form of
192
+ {"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence}
193
+ """
194
+ segments: list[dict] = []
195
+
196
+ # Open and parse result file
197
+ lines = utils.read_lines(rfile)
198
+
199
+ # Auto-detect result type
200
+ rtype = detect_rtype(lines[0])
201
+
202
+ if rtype == "audacity":
203
+ raise Exception("Audacity files are not supported for combined results.")
204
+
205
+ # Get mapping from the header column
206
+ header_mapping = get_header_mapping(lines[0])
207
+
208
+ # Get start and end times based on rtype
209
+ confidence = 0
210
+ start = end = 0.0
211
+ species = ""
212
+ afile = ""
213
+
214
+ for i, line in enumerate(lines):
215
+ if rtype == "table" and i > 0:
216
+ d = line.split("\t")
217
+ file_offset = float(d[header_mapping["File Offset (s)"]])
218
+ start = file_offset
219
+ end = file_offset + (float(d[header_mapping["End Time (s)"]]) - float(d[header_mapping["Begin Time (s)"]]))
220
+ species = d[header_mapping["Common Name"]]
221
+ confidence = float(d[header_mapping["Confidence"]])
222
+ afile = d[header_mapping["Begin Path"]].replace("/", os.sep).replace("\\", os.sep)
223
+
224
+ elif rtype == "kaleidoscope" and i > 0:
225
+ d = line.split(",")
226
+ start = float(d[header_mapping["OFFSET"]])
227
+ end = float(d[header_mapping["DURATION"]]) + start
228
+ species = d[header_mapping["scientific_name"]]
229
+ confidence = float(d[header_mapping["confidence"]])
230
+ in_dir = d[header_mapping["INDIR"]]
231
+ folder = d[header_mapping["FOLDER"]]
232
+ in_file = d[header_mapping["IN FILE"]]
233
+ afile = os.path.join(in_dir, folder, in_file).replace("/", os.sep).replace("\\", os.sep)
234
+
235
+ elif rtype == "csv" and i > 0:
236
+ d = line.split(",")
237
+ start = float(d[header_mapping["Start (s)"]])
238
+ end = float(d[header_mapping["End (s)"]])
239
+ species = d[header_mapping["Common name"]]
240
+ confidence = float(d[header_mapping["Confidence"]])
241
+ afile = d[header_mapping["File"]].replace("/", os.sep).replace("\\", os.sep)
242
+
243
+ # Check if confidence is high enough and label is not "nocall"
244
+ if confidence >= cfg.MIN_CONFIDENCE and species.lower() != "nocall" and afile:
245
+ segments.append({"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence})
246
+
247
+ return segments
248
+
249
+
250
+ def find_segments(afile: str, rfile: str):
251
+ """Extracts the segments for an audio file from the results file
252
+
253
+ Args:
254
+ afile: Path to the audio file.
255
+ rfile: Path to the result file.
256
+
257
+ Returns:
258
+ A list of dicts in the form of
259
+ {"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence}
260
+ """
261
+ segments: list[dict] = []
262
+
263
+ # Open and parse result file
264
+ lines = utils.read_lines(rfile)
265
+
266
+ # Auto-detect result type
267
+ rtype = detect_rtype(lines[0])
268
+
269
+ # Get mapping from the header column
270
+ header_mapping = get_header_mapping(lines[0])
271
+
272
+ # Get start and end times based on rtype
273
+ confidence = 0
274
+ start = end = 0.0
275
+ species = ""
276
+
277
+ for i, line in enumerate(lines):
278
+ if rtype == "table" and i > 0:
279
+ d = line.split("\t")
280
+ start = float(d[header_mapping["Begin Time (s)"]])
281
+ end = float(d[header_mapping["End Time (s)"]])
282
+ species = d[header_mapping["Common Name"]]
283
+ confidence = float(d[header_mapping["Confidence"]])
284
+
285
+ elif rtype == "audacity":
286
+ d = line.split("\t")
287
+ start = float(d[0])
288
+ end = float(d[1])
289
+ species = d[2].split(", ")[1]
290
+ confidence = float(d[-1])
291
+
292
+ elif rtype == "kaleidoscope" and i > 0:
293
+ d = line.split(",")
294
+ start = float(d[header_mapping["OFFSET"]])
295
+ end = float(d[header_mapping["DURATION"]]) + start
296
+ species = d[header_mapping["scientific_name"]]
297
+ confidence = float(d[header_mapping["confidence"]])
298
+
299
+ elif rtype == "csv" and i > 0:
300
+ d = line.split(",")
301
+ start = float(d[header_mapping["Start (s)"]])
302
+ end = float(d[header_mapping["End (s)"]])
303
+ species = d[header_mapping["Common name"]]
304
+ confidence = float(d[header_mapping["Confidence"]])
305
+
306
+ # Check if confidence is high enough and label is not "nocall"
307
+ if confidence >= cfg.MIN_CONFIDENCE and species.lower() != "nocall":
308
+ segments.append({"audio": afile, "start": start, "end": end, "species": species, "confidence": confidence})
309
+
310
+ return segments
311
+
312
+
313
+ def extract_segments(item: tuple[tuple[str, list[dict]], float, dict[str]]):
314
+ """
315
+ Extracts audio segments from a given audio file based on provided segment information.
316
+ Args:
317
+ item (tuple): A tuple containing:
318
+ - A tuple with:
319
+ - A string representing the path to the audio file.
320
+ - A list of dictionaries, each containing segment information with keys "start", "end", "species", "confidence", and "audio".
321
+ - A float representing the segment length.
322
+ - A dictionary containing configuration settings.
323
+ Returns:
324
+ bool: True if segments were successfully extracted, False otherwise.
325
+ Raises:
326
+ Exception: If there is an error opening the audio file or extracting segments.
327
+ """
328
+ # Paths and config
329
+ afile = item[0][0]
330
+ segments = item[0][1]
331
+ seg_length = item[1]
332
+ cfg.set_config(item[2])
333
+
334
+ # Status
335
+ print(f"Extracting segments from {afile}")
336
+
337
+ try:
338
+ # Open audio file
339
+ sig, rate = audio.open_audio_file(afile, cfg.SAMPLE_RATE, speed=cfg.AUDIO_SPEED)
340
+ except Exception as ex:
341
+ print(f"Error: Cannot open audio file {afile}", flush=True)
342
+ utils.write_error_log(ex)
343
+
344
+ return None
345
+
346
+ # Extract segments
347
+ for seg_cnt, seg in enumerate(segments, 1):
348
+ try:
349
+ # Get start and end times
350
+ start = int((seg["start"] * rate) / cfg.AUDIO_SPEED)
351
+ end = int((seg["end"] * rate) / cfg.AUDIO_SPEED)
352
+
353
+ offset = max(0, ((seg_length * rate) - (end - start)) // 2)
354
+ start = max(0, start - offset)
355
+ end = min(len(sig), end + offset)
356
+
357
+ # Make sure segment is long enough
358
+ if end > start:
359
+ # Get segment raw audio from signal
360
+ seg_sig = sig[int(start) : int(end)]
361
+
362
+ # Make output path
363
+ outpath = os.path.join(cfg.OUTPUT_PATH, seg["species"])
364
+ os.makedirs(outpath, exist_ok=True)
365
+
366
+ # Save segment
367
+ seg_name = "{:.3f}_{}_{}_{:.1f}s_{:.1f}s.wav".format(
368
+ seg["confidence"],
369
+ seg_cnt,
370
+ seg["audio"].rsplit(os.sep, 1)[-1].rsplit(".", 1)[0],
371
+ seg["start"],
372
+ seg["end"],
373
+ )
374
+ seg_path = os.path.join(outpath, seg_name)
375
+ audio.save_signal(seg_sig, seg_path, rate)
376
+
377
+ except Exception as ex:
378
+ # Write error log
379
+ print(f"Error: Cannot extract segments from {afile}.", flush=True)
380
+ utils.write_error_log(ex)
381
+ return False
382
+
383
+ return True