Skip to content

Commit

Permalink
refactor: update label handling to use Settings structure
Browse files Browse the repository at this point in the history
- Replaced direct usage of Labels field in BirdNET with Labels from Settings.BirdNET across multiple files.
- Ensured consistent label management by centralizing label storage within the Settings structure, improving maintainability and clarity.
- Updated relevant functions to reflect this change, enhancing the overall architecture of the BirdNET module.
  • Loading branch information
tphakala committed Jan 11, 2025
1 parent 4a5adce commit 1dc4d2c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/birdnet/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (bn *BirdNET) Predict(sample [][]float32) ([]datastore.Results, error) {

confidence := applySigmoidToPredictions(predictions, bn.Settings.BirdNET.Sensitivity)

results, err := pairLabelsAndConfidence(bn.Labels, confidence)
results, err := pairLabelsAndConfidence(bn.Settings.BirdNET.Labels, confidence)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/birdnet/birdnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ var labelsZip []byte
type BirdNET struct {
AnalysisInterpreter *tflite.Interpreter
RangeInterpreter *tflite.Interpreter
Labels []string
Settings *conf.Settings
SpeciesListUpdated time.Time // Timestamp for the last update of the species list.
mu sync.Mutex
Expand Down Expand Up @@ -181,7 +180,7 @@ func (bn *BirdNET) determineThreadCount(configuredThreads int) int {

// loadLabels extracts and loads labels from either the embedded zip file or an external file
func (bn *BirdNET) loadLabels() error {
bn.Labels = []string{} // Reset labels.
bn.Settings.BirdNET.Labels = []string{} // Reset labels.

// Use embedded labels if no external label path is set
if bn.Settings.BirdNET.LabelPath == "" {
Expand Down Expand Up @@ -263,7 +262,7 @@ func (bn *BirdNET) loadLabelsFromZip(file *os.File) error {
func (bn *BirdNET) loadLabelsFromText(file *os.File) error {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
bn.Labels = append(bn.Labels, strings.TrimSpace(scanner.Text()))
bn.Settings.BirdNET.Labels = append(bn.Settings.BirdNET.Labels, strings.TrimSpace(scanner.Text()))
}
return scanner.Err()
}
Expand All @@ -278,7 +277,7 @@ func (bn *BirdNET) readLabelFile(file *zip.File) error {

scanner := bufio.NewScanner(fileReader)
for scanner.Scan() {
bn.Labels = append(bn.Labels, strings.TrimSpace(scanner.Text()))
bn.Settings.BirdNET.Labels = append(bn.Settings.BirdNET.Labels, strings.TrimSpace(scanner.Text()))
}
return scanner.Err() // Returns nil if no errors occurred during scanning.
}
Expand Down
8 changes: 4 additions & 4 deletions internal/birdnet/range_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (bn *BirdNET) GetProbableSpecies(date time.Time, week float32) ([]SpeciesSc
if bn.Settings.BirdNET.Latitude == 0 && bn.Settings.BirdNET.Longitude == 0 {
bn.Debug("Latitude and longitude not set, not using location based prediction filter")
var speciesScores []SpeciesScore
for _, label := range bn.Labels {
for _, label := range bn.Settings.BirdNET.Labels {
speciesScores = append(speciesScores, SpeciesScore{Score: 0.0, Label: label})
}
return speciesScores, nil
Expand Down Expand Up @@ -100,7 +100,7 @@ func addSpeciesWithMaxScore(bn *BirdNET, speciesScores *[]SpeciesScore, speciesN
}

matchFound := false
for _, label := range bn.Labels {
for _, label := range bn.Settings.BirdNET.Labels {
if matchesSpecies(label, speciesName) {
bn.Debug("Adding species with max score: %s (matched with: %s)", label, speciesName)
*speciesScores = append(*speciesScores, SpeciesScore{Score: 1.0, Label: label})
Expand Down Expand Up @@ -173,8 +173,8 @@ func (bn *BirdNET) predictFilter(date time.Time, week float32) ([]Filter, error)
// Filter and label the results, but only for indices that exist in bn.Labels
var results []Filter
for i, score := range filter {
if score >= bn.Settings.BirdNET.RangeFilter.Threshold && i < len(bn.Labels) {
results = append(results, Filter{Score: score, Label: bn.Labels[i]})
if score >= bn.Settings.BirdNET.RangeFilter.Threshold && i < len(bn.Settings.BirdNET.Labels) {
results = append(results, Filter{Score: score, Label: bn.Settings.BirdNET.Labels[i]})
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ type BirdNETConfig struct {
RangeFilter RangeFilterSettings // range filter settings
ModelPath string // path to external model file (empty for embedded)
LabelPath string // path to external label file (empty for embedded)
Labels []string `yaml:"-"` // list of available species labels, runtime value
UseXNNPACK bool // true to use XNNPACK delegate for inference acceleration
}

Expand Down

0 comments on commit 1dc4d2c

Please sign in to comment.