mirror of
https://github.com/macaodha/batdetect2.git
synced 2025-06-30 15:12:06 +02:00
Compare commits
No commits in common. "4282e2ae70a50848012f495fbfbffd82443a368d" and "54ca55558775c43b72c768f6360c996b58629b9a" have entirely different histories.
4282e2ae70
...
54ca555587
@ -99,7 +99,6 @@ consult the API documentation in the code.
|
||||
import warnings
|
||||
from typing import List, Optional, Tuple, BinaryIO, Any, Union
|
||||
|
||||
from .types import AudioPath
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
@ -245,7 +244,9 @@ def generate_spectrogram(
|
||||
|
||||
|
||||
def process_file(
|
||||
path: AudioPath,
|
||||
path: Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
],
|
||||
model: DetectionModel = MODEL,
|
||||
config: Optional[ProcessingConfiguration] = None,
|
||||
device: torch.device = DEVICE,
|
||||
@ -255,7 +256,9 @@ def process_file(
|
||||
|
||||
Parameters
|
||||
----------
|
||||
path : AudioPath
|
||||
path : Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
]
|
||||
Path to audio data.
|
||||
model : DetectionModel, optional
|
||||
Detection model. Uses default model if not specified.
|
||||
|
@ -1,10 +1,6 @@
|
||||
"""Types used in the code base."""
|
||||
|
||||
from typing import List, NamedTuple, Optional, Union, Any, BinaryIO
|
||||
|
||||
import audioread
|
||||
import os
|
||||
import soundfile as sf
|
||||
from typing import List, NamedTuple, Optional, Union
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
@ -44,9 +40,6 @@ __all__ = [
|
||||
"SpectrogramParameters",
|
||||
]
|
||||
|
||||
AudioPath = Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
]
|
||||
|
||||
class SpectrogramParameters(TypedDict):
|
||||
"""Parameters for generating spectrograms."""
|
||||
|
@ -1,8 +1,6 @@
|
||||
import warnings
|
||||
from typing import Optional, Tuple, Union, Any, BinaryIO
|
||||
|
||||
from ..types import AudioPath
|
||||
|
||||
import librosa
|
||||
import librosa.core.spectrum
|
||||
import numpy as np
|
||||
@ -11,6 +9,7 @@ import torch
|
||||
import audioread
|
||||
import os
|
||||
import soundfile as sf
|
||||
import io
|
||||
|
||||
from batdetect2.detector import parameters
|
||||
|
||||
@ -18,7 +17,7 @@ from . import wavfile
|
||||
|
||||
__all__ = [
|
||||
"load_audio",
|
||||
"load_audio_and_samplerate",
|
||||
"load_audio_data",
|
||||
"generate_spectrogram",
|
||||
"pad_audio",
|
||||
]
|
||||
@ -148,7 +147,9 @@ def generate_spectrogram(
|
||||
return spec, spec_for_viz
|
||||
|
||||
def load_audio(
|
||||
path: AudioPath,
|
||||
path: Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
],
|
||||
time_exp_fact: float,
|
||||
target_samp_rate: int,
|
||||
scale: bool = False,
|
||||
@ -173,11 +174,13 @@ def load_audio(
|
||||
ValueError: If the audio file is stereo.
|
||||
|
||||
"""
|
||||
sample_rate, audio_data, _ = load_audio_and_samplerate(path, time_exp_fact, target_samp_rate, scale, max_duration)
|
||||
sample_rate, audio_data, _ = load_audio_data(path, time_exp_fact, target_samp_rate, scale, max_duration)
|
||||
return sample_rate, audio_data
|
||||
|
||||
def load_audio_and_samplerate(
|
||||
path: AudioPath,
|
||||
def load_audio_data(
|
||||
path: Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
],
|
||||
time_exp_fact: float,
|
||||
target_samp_rate: int,
|
||||
scale: bool = False,
|
||||
@ -197,7 +200,6 @@ def load_audio_and_samplerate(
|
||||
Returns:
|
||||
sampling_rate: The sampling rate of the audio.
|
||||
audio_raw: The audio signal in a numpy array.
|
||||
file_sampling_rate: The original sampling rate of the audio
|
||||
|
||||
Raises:
|
||||
ValueError: If the audio file is stereo.
|
||||
|
@ -2,8 +2,7 @@ import json
|
||||
import os
|
||||
from typing import Any, Iterator, List, Optional, Tuple, Union, BinaryIO
|
||||
|
||||
from ..types import AudioPath
|
||||
|
||||
import librosa
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import torch
|
||||
@ -737,7 +736,9 @@ def process_audio_array(
|
||||
|
||||
|
||||
def process_file(
|
||||
path: AudioPath,
|
||||
path: Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
],
|
||||
model: DetectionModel,
|
||||
config: ProcessingConfiguration,
|
||||
device: torch.device,
|
||||
@ -750,7 +751,7 @@ def process_file(
|
||||
|
||||
Parameters
|
||||
----------
|
||||
path : AudioPath
|
||||
path : str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
Path to audio file.
|
||||
|
||||
model : torch.nn.Module
|
||||
@ -759,9 +760,6 @@ def process_file(
|
||||
config : ProcessingConfiguration
|
||||
Configuration for processing.
|
||||
|
||||
file_id: Optional[str],
|
||||
Give the data an id. Defaults to the filename if path is a string. Otherwise an md5 will be calculated from the binary data.
|
||||
|
||||
Returns
|
||||
-------
|
||||
results : Results or Any
|
||||
@ -775,7 +773,7 @@ def process_file(
|
||||
spec_slices = []
|
||||
|
||||
# load audio file
|
||||
sampling_rate, audio_full, file_samp_rate = au.load_audio_and_samplerate(
|
||||
sampling_rate, audio_full, file_samp_rate = au.load_audio_data(
|
||||
path,
|
||||
time_exp_fact=config.get("time_expansion", 1) or 1,
|
||||
target_samp_rate=config["target_samp_rate"],
|
||||
@ -859,7 +857,9 @@ def process_file(
|
||||
|
||||
return results
|
||||
|
||||
def _generate_id(path: AudioPath) -> str:
|
||||
def _generate_id(path: Union[
|
||||
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
|
||||
]) -> str:
|
||||
""" Generate an id based on the path.
|
||||
|
||||
If the path is a str or PathLike it will parsed as the basename.
|
||||
|
@ -284,7 +284,7 @@ def test_process_file_with_empty_predictions_does_not_fail(
|
||||
assert len(results["pred_dict"]["annotation"]) == 0
|
||||
|
||||
def test_process_file_file_id_defaults_to_basename():
|
||||
"""Test that process_file assigns basename as an id if no file_id is provided."""
|
||||
"""Test that no detections are made above the nyquist frequency."""
|
||||
# Recording donated by @@kdarras
|
||||
basename = "20230322_172000_selec2.wav"
|
||||
path = os.path.join(DATA_DIR, basename)
|
||||
@ -295,7 +295,7 @@ def test_process_file_file_id_defaults_to_basename():
|
||||
assert id == basename
|
||||
|
||||
def test_bytesio_file_id_defaults_to_md5():
|
||||
"""Test that process_file assigns an md5 sum as an id if no file_id is provided when using binary data."""
|
||||
"""Test that no detections are made above the nyquist frequency."""
|
||||
# Recording donated by @@kdarras
|
||||
basename = "20230322_172000_selec2.wav"
|
||||
path = os.path.join(DATA_DIR, basename)
|
||||
|
@ -146,9 +146,9 @@ def test_load_audio_using_bytesio():
|
||||
with open(path, "rb") as f:
|
||||
data = io.BytesIO(f.read())
|
||||
|
||||
sample_rate, audio_data, file_sample_rate = audio_utils.load_audio_and_samplerate(data, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ)
|
||||
sample_rate, audio_data, file_sample_rate = audio_utils.load_audio_data(data, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ)
|
||||
|
||||
expected_sample_rate, expected_audio_data, exp_file_sample_rate = audio_utils.load_audio_and_samplerate(path, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ)
|
||||
expected_sample_rate, expected_audio_data, exp_file_sample_rate = audio_utils.load_audio_data(path, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ)
|
||||
|
||||
assert expected_sample_rate == sample_rate
|
||||
assert exp_file_sample_rate == file_sample_rate
|
||||
|
Loading…
Reference in New Issue
Block a user