mirror of
https://github.com/macaodha/batdetect2.git
synced 2025-06-29 22:51:58 +02:00
Added api method to process a URL
This commit is contained in:
parent
47dbdc79c2
commit
f62bc99ab2
@ -123,6 +123,8 @@ from batdetect2.utils.detector_utils import list_audio_files, load_model
|
|||||||
import audioread
|
import audioread
|
||||||
import os
|
import os
|
||||||
import soundfile as sf
|
import soundfile as sf
|
||||||
|
import requests
|
||||||
|
import io
|
||||||
|
|
||||||
# Remove warnings from torch
|
# Remove warnings from torch
|
||||||
warnings.filterwarnings("ignore", category=UserWarning, module="torch")
|
warnings.filterwarnings("ignore", category=UserWarning, module="torch")
|
||||||
@ -279,6 +281,49 @@ def process_file(
|
|||||||
file_id
|
file_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def process_url(
|
||||||
|
url: str,
|
||||||
|
model: DetectionModel = MODEL,
|
||||||
|
config: Optional[ProcessingConfiguration] = None,
|
||||||
|
device: torch.device = DEVICE,
|
||||||
|
file_id: str | None = None
|
||||||
|
) -> du.RunResults:
|
||||||
|
"""Process audio file with model.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
url : str
|
||||||
|
HTTP URL to load the audio data from
|
||||||
|
model : DetectionModel, optional
|
||||||
|
Detection model. Uses default model if not specified.
|
||||||
|
config : Optional[ProcessingConfiguration], optional
|
||||||
|
Processing configuration, by default None (uses default parameters).
|
||||||
|
device : torch.device, optional
|
||||||
|
Device to use, by default tries to use GPU if available.
|
||||||
|
file_id: Optional[str],
|
||||||
|
Give the data an id. Defaults to the URL
|
||||||
|
"""
|
||||||
|
if config is None:
|
||||||
|
config = CONFIG
|
||||||
|
|
||||||
|
if file_id is None:
|
||||||
|
file_id = url
|
||||||
|
|
||||||
|
response = requests.get(url)
|
||||||
|
|
||||||
|
# Raise exception on HTTP error
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
# Retrieve body as raw bytes
|
||||||
|
raw_audio_data = response.content
|
||||||
|
|
||||||
|
return du.process_file(
|
||||||
|
io.BytesIO(raw_audio_data),
|
||||||
|
model,
|
||||||
|
config,
|
||||||
|
device,
|
||||||
|
file_id
|
||||||
|
)
|
||||||
|
|
||||||
def process_spectrogram(
|
def process_spectrogram(
|
||||||
spec: torch.Tensor,
|
spec: torch.Tensor,
|
||||||
|
Loading…
Reference in New Issue
Block a user