-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (69 loc) · 2.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import os
import numpy as np
from ML.Utils.get_timestamps import get_timestamps
from ML.BirdVoxDetect import run_birdvoxdetect_segmentation
from ML.Clusterer.dominantset import get_clusters_using_dominant_sets
from ML.Clusterer.somandkmeans import get_clusters_using_SOM_and_k_means
from ML.FeatureExtraction.birdNET_feature_extraction import get_activations
from ML.output import create_output
def main():
# Takes arguments of form:
# [output_path] [outdir] [clustering strategy (0 for dominant sets, 1 for SOM and K Means)] [threshold]
# [noise_reduction (0/1)] [noise_file if noise_reduction == 1] [input_file_1] [input_file_2] ..... [input_file_n]
# TODO: Better input parsing
args = sys.argv[1:]
output_path = args[0]
outDir = args[1]
dominant_set_clustering = int(args[2]) == 0
threshold = float(args[3])
noise_reduction = int(args[4]) == 1
i = 5
noise_file = None
if noise_reduction:
i = 6
noise_file = args[5]
file_names = args[i:]
# Applying birdvoxdetect
list_of_filenames_and_timestamps = run_birdvoxdetect_segmentation(
file_names=file_names,
noise_reduction=noise_reduction,
noise_file=noise_file,
threshold=threshold,
outdir=outDir,
)
detection_map, birdnet_input = get_timestamps(list_of_filenames_and_timestamps)
# Feature Extraction
list_of_filenames_timestamps_activations = get_activations(birdnet_input)
# Unzipping the list
file_names, timestamps, features = tuple(
zip(*list_of_filenames_timestamps_activations)
)
file_names, timestamps, features = (
list(file_names),
list(timestamps),
np.array(features),
)
# Clustering
if dominant_set_clustering:
cluster_ids = get_clusters_using_dominant_sets(features)
else:
# Using SOM and K Means clustering.
# Assuming the max number of clusters to be 9 times the number of files.
cluster_ids = get_clusters_using_SOM_and_k_means(
features,
max_num_of_clusters=min(
9 * len(list_of_filenames_and_timestamps), features.shape[0]
),
)
# Time to prepare the output!!!
create_output(
filenames=file_names,
clusters=cluster_ids,
timestamps=timestamps,
output_path=output_path,
detection_map=detection_map,
outDir=outDir,
)
if __name__ == "__main__":
main()