Skip to content

Commit

Permalink
Add full labels to the returned detections (#87)
Browse files Browse the repository at this point in the history
* Add label to the analyzer's detections

* Update docs with Detection.label

* Increment the version number
  • Loading branch information
joeweiss authored Oct 2, 2023
1 parent fb8e1e7 commit ae4a002
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ print(recording.detections)
'confidence': 0.5744,
'end_time': 12.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 9.0},
'start_time': 9.0,
'label': 'Haemorhous mexicanus_House Finch'},
{'common_name': 'House Finch',
'confidence': 0.4496,
'end_time': 15.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 12.0}]
'start_time': 12.0,
'label': 'Haemorhous mexicanus_House Finch'}]
```

The `Recording` class takes a file path as an argument. You can also use `RecordingFileObject` to analyze an in-memory object, or `RecordingBuffer` for handling an array buffer.
Expand Down
6 changes: 4 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ print(recording.detections)
'confidence': 0.5744,
'end_time': 12.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 9.0},
'start_time': 9.0,
'label': 'Haemorhous mexicanus_House Finch'},
{'common_name': 'House Finch',
'confidence': 0.4496,
'end_time': 15.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 12.0}]
'start_time': 12.0,
'label': 'Haemorhous mexicanus_House Finch'}]
```

The `Recording` class takes a file path as an argument. You can also use `RecordingFileObject` to analyze an in-memory object, or `RecordingBuffer` for handling an array buffer.
6 changes: 4 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ print(recording.detections)
'confidence': 0.5744,
'end_time': 12.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 9.0},
'start_time': 9.0,
'label': 'Haemorhous mexicanus_House Finch'},
{'common_name': 'House Finch',
'confidence': 0.4496,
'end_time': 15.0,
'scientific_name': 'Haemorhous mexicanus',
'start_time': 12.0}]
'start_time': 12.0,
'label': 'Haemorhous mexicanus_House Finch'}]
```

The `Recording` class takes a file path as an argument. You can also use `RecordingFileObject` to analyze an in-memory object, or `RecordingBuffer` for handling an array buffer.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exclude = [

[project]
name = "birdnetlib"
version = "0.10.0"
version = "0.11.0"
authors = [
{ name="Joe Weiss", email="[email protected]" },
]
Expand Down
8 changes: 6 additions & 2 deletions src/birdnetlib/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, start_time, end_time):
self.common_name = ""
self.scientific_name = ""
self.confidence = 0
self.label = ""

@property
def as_dict(self):
Expand All @@ -50,6 +51,7 @@ def as_dict(self):
"start_time": self.start_time,
"end_time": self.end_time,
"confidence": self.confidence,
"label": self.label,
}


Expand Down Expand Up @@ -228,13 +230,15 @@ def detections(self):
end_time = float(key.split("-")[1])
for c in value:
confidence = float(c[1])
scientific_name = c[0].split("_")[0]
common_name = c[0].split("_")[1]
label = c[0]
scientific_name = label.split("_")[0]
common_name = label.split("_")[1]
# print(c[0], f"{c[1]:1.4f}")
d = Detection(start_time, end_time)
d.common_name = common_name
d.scientific_name = scientific_name
d.confidence = confidence
d.label = label
# print(d.as_dict)
detections.append(d)

Expand Down
1 change: 1 addition & 0 deletions src/birdnetlib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def as_dict(self):
"start_time": self.start_time,
"end_time": self.end_time,
"confidence": self.confidence,
"label": self.result,
}


Expand Down
5 changes: 5 additions & 0 deletions tests/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ def test_with_species_list():
assert (
commandline_results[0]["common_name"] == recording.detections[0]["common_name"]
)
assert "label" in recording.detections[0]
assert (
recording.detections[0]["label"]
== f"{commandline_results[0]['scientific_name']}_{commandline_results[0]['common_name']}"
)

commandline_birds = [i["common_name"] for i in commandline_results]
detected_birds = [i["common_name"] for i in recording.detections]
Expand Down
6 changes: 6 additions & 0 deletions tests/test_analyzer_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def test_with_custom_list_path():
assert commandline_results[0]["common_name"] == "Spotted Towhee"
assert pytest.approx(commandline_results[0]["confidence"], 0.01) == 0.55690277

assert "label" in recording.detections[0]
assert (
recording.detections[0]["label"]
== f"{commandline_results[0]['scientific_name']}_{commandline_results[0]['common_name']}"
)


def test_with_custom_list():

Expand Down
1 change: 1 addition & 0 deletions tests/test_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def test_extraction():
"extracted_spectrogram_path": f"{export_dir}/soundscape_7s-14s.png",
"scientific_name": "Haemorhous mexicanus",
"start_time": 9.0,
"label": "Haemorhous mexicanus_House Finch",
}

assert detection == expected_detection

0 comments on commit ae4a002

Please sign in to comment.