Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/batdetect2/inference/clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from uuid import uuid5

import numpy as np
from loguru import logger
from soundevent import data
from soundfile import LibsndfileError


def get_clips_from_files(
Expand All @@ -16,7 +18,15 @@ def get_clips_from_files(
clips: List[data.Clip] = []

for path in paths:
recording = data.Recording.from_file(path, compute_hash=compute_hash)
try:
recording = data.Recording.from_file(
path,
compute_hash=compute_hash,
)
except LibsndfileError as e:
logger.warning(f"Skipping unreadable audio file {path}: {e}")
continue

clips.extend(
get_recording_clips(
recording,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_cli/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,44 @@ def test_cli_process_directory_merges_clip_outputs_per_recording(
)

assert actual_annotations == expected_annotations


def test_cli_process_directory_skips_corrupted_files(
tmp_path: Path,
contrib_dir: Path,
) -> None:
recording_path = contrib_dir / "jeff37" / "0166_20240531_223911.wav"

source_folder = tmp_path / "audio"
source_folder.mkdir()
shutil.copy2(
recording_path,
source_folder / "example_audio.wav",
)

corrupted_file = source_folder / "corrupted.wav"
corrupted_file.write_text("corrupted")

destination_folder = tmp_path / "results"
destination_folder.mkdir()

result = CliRunner().invoke(
cli,
args=[
"process",
"directory",
str(source_folder),
str(destination_folder),
"--detection-threshold",
"0.3",
],
)

assert result.exit_code == 0
assert destination_folder.exists()

output_json = destination_folder / "example_audio.wav.json"
assert output_json.exists()

corrupted_file_json = destination_folder / "corrupted.wav.json"
assert not corrupted_file_json.exists()
Loading