Skip to content

Commit

Permalink
update to use specific bird tag rather than bird when available and f…
Browse files Browse the repository at this point in the history
…ilter out noise
  • Loading branch information
gferraro committed Feb 27, 2023
1 parent 95ba387 commit d3695db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
16 changes: 13 additions & 3 deletions Melt/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import math


NON_BIRD = ["human", "noise"]


def calc_cacophony_index(tracks, length):
version = "1.0"
other_labels = [other for other in tracks if other["species"] != "human"]
Expand All @@ -33,7 +36,7 @@ def calc_cacophony_index(tracks, length):
period_end = period_length
period = 0
for track in other_labels:
if track["species"] not in ["human", "noise"]:
if track["species"] not in NON_BIRD:
# bird started in existing span
if bird_until >= track["begin_s"] and bird_until < track["end_s"]:
new_span = (bird_until, track["end_s"])
Expand Down Expand Up @@ -66,11 +69,18 @@ def calc_cacophony_index(tracks, length):
return percents, version


def filter_trcks(tracks):
filtered_labels = ["noise"]
filtered = [t for t in tracks if t["species"] not in filtered_labels]
return filtered


def species_identify(file_name, metadata_name, models, bird_model):
# labels = identify_species(file_name, metadata_name, models)
labels = []
labels = identify_species(file_name, metadata_name, models)
other_labels, length = classify(file_name, bird_model)
other_labels = filter_trcks(other_labels)
cacophony_index, version = calc_cacophony_index(other_labels, length)

labels.extend(other_labels)
result = {}
result["species_identify"] = labels
Expand Down
33 changes: 27 additions & 6 deletions Melt/identify_bird.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ def load_samples(path, segment_length, stride, hop_length=640):
sample_size = int(sr * segment_length)
jumps_per_stride = int(sr * stride)
length = len(frames) / sr
end = 0
end = segment_length
mel_samples = []
i = 0
while end < (length - segment_length / 2):
data = frames[i * jumps_per_stride : i * jumps_per_stride + sample_size]
while end < (length + stride):
if end > length:
# always use end ofr last sample
data = frames[-sample_size:]
else:
data = frames[i * jumps_per_stride : i * jumps_per_stride + sample_size]
if len(data) != sample_size:
sample = np.zeros((sample_size))
sample[: len(data)] = data
Expand All @@ -53,7 +57,6 @@ def load_samples(path, segment_length, stride, hop_length=640):
n_mels=80,
)
mel = librosa.power_to_db(mel, ref=np.max)

# end = start + sample_size
mel_m = tf.reduce_mean(mel, axis=1)
mel_m = tf.expand_dims(mel_m, axis=1)
Expand Down Expand Up @@ -92,6 +95,10 @@ def classify(file, model_file):
start = 0
active_tracks = {}
for prediction in predictions:
# last sample always ends at length of audio rec
if start + segment_length > length:
start = length - segment_length
specific_bird = False
results = []
track_labels = []
if multi_label:
Expand All @@ -100,24 +107,38 @@ def classify(file, model_file):
label = labels[i]
results.append((p, label))
track_labels.append(label)
specific_bird = specific_bird or label not in [
"human",
"noise",
"bird",
]

else:
best_i = np.argmax(prediction)
best_p = prediction[best_i]
if best_p >= 0.7:
label = labels[best_i]
results.append((best_p, label))
track_labels.append(label)
specific_bird = label not in ["human", "noise", "bird"]

# remove tracks that have ended
existing_tracks = list(active_tracks.keys())
for existing in existing_tracks:
track = active_tracks[existing]
if track.label not in track_labels:
track.end = min(length, track.end - segment_length / 2)
if track.label not in track_labels or (
track.label == "bird" and specific_bird
):
if specific_bird:
track.end = start
else:
track.end = min(length, track.end - segment_length / 2)
del active_tracks[track.label]

for r in results:
label = r[1]
if specific_bird and label == "bird":
continue
track = active_tracks.get(label, None)
if track is None:
track = Track(label, start, start + segment_length, r[0])
Expand Down

0 comments on commit d3695db

Please sign in to comment.