perf: defer heavy api imports

This commit is contained in:
mbsantiago 2026-05-05 16:39:43 +01:00
parent 5526ac99fc
commit d83f801515

View File

@ -3,13 +3,12 @@ from __future__ import annotations
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Literal from typing import TYPE_CHECKING, Literal
import numpy as np
from soundevent import data
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Sequence from collections.abc import Sequence
import numpy as np
import torch import torch
from soundevent import data
from batdetect2.audio import AudioConfig, AudioLoader from batdetect2.audio import AudioConfig, AudioLoader
from batdetect2.data import Dataset from batdetect2.data import Dataset
@ -313,6 +312,8 @@ class BatDetect2API:
def get_top_class_name(self, detection: Detection) -> str: def get_top_class_name(self, detection: Detection) -> str:
"""Get highest-confidence class name for one detection.""" """Get highest-confidence class name for one detection."""
import numpy as np
top_index = int(np.argmax(detection.class_scores)) top_index = int(np.argmax(detection.class_scores))
return self.targets.class_names[top_index] return self.targets.class_names[top_index]
@ -368,6 +369,8 @@ class BatDetect2API:
batch_size: int | None = None, batch_size: int | None = None,
detection_threshold: float | None = None, detection_threshold: float | None = None,
) -> ClipDetections: ) -> ClipDetections:
from soundevent import data
from batdetect2.postprocess import ClipDetections from batdetect2.postprocess import ClipDetections
recording = data.Recording.from_file(audio_file, compute_hash=False) recording = data.Recording.from_file(audio_file, compute_hash=False)