Skip to content

Commit

Permalink
Add generic parse label function for filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
wetdog committed Sep 26, 2022
1 parent 1514ec5 commit d8ed865
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions preprocess/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# to split very large audios
chunk_size_seconds = 900
max_audio_duration = 3600
parse_label_fn = get_label_ub8k
parse_label_fn = get_label_generic
label_list = ub8k_labels

parser = argparse.ArgumentParser(description='Preprocess audio files for scatter sound viz')
Expand All @@ -40,7 +40,7 @@
audio_folder = args.dir
assert os.path.isdir(audio_folder), "Invalid audio folder"
audio_name = os.path.basename(audio_folder)
x, metadata = process_clips_from_folder(audio_folder,parse_label_fn,label_list,
x, metadata = process_clips_from_folder(audio_folder,parse_label_fn,
clip_dur=window_size,global_sr=global_sr,target_sr=model_sample_rate)
# Load models from TF-hub
yamnet = hub.load('https://tfhub.dev/google/yamnet/1')
Expand Down
12 changes: 10 additions & 2 deletions preprocess/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def get_metadata(x,filename:str,hop_size:float=0.48, window_size:float=0.96,sr:i
return metadata_dict


def get_label_generic(filename)->str:
"""get labels as filenames, as unlabeled datasets"""
return filename.split(".")[0]

def get_label_ub8k(filename)->str:
"""get label from ub8k dataset"""
return filename.split(".")[0].split("-")[1]
Expand All @@ -79,7 +83,7 @@ def get_label_esc50(filename)->str:
return filename.split(".")[0].split("-")[-1]


def process_clips_from_folder(audio_folder: str,parse_label_fn:callable,labels_str:list,
def process_clips_from_folder(audio_folder: str,parse_label_fn:callable,labels_str:list=None,
clip_dur: float=0.96,global_sr:float=44100,target_sr: int=16000)->tuple[np.ndarray,dict]:
""" Process audio clips inside a folder to extract equal size fragments,
and build a metadata dictionary with audio descriptors, labels and filenames """
Expand Down Expand Up @@ -114,7 +118,11 @@ def process_clips_from_folder(audio_folder: str,parse_label_fn:callable,labels_s
label = parse_label_fn(filename)
metadata_dict["filenames"].append(filename)
metadata_dict["labels"].append(label)
metadata_dict["labelnames"].append(labels_str[int(label)])
if labels_str:
metadata_dict["labelnames"].append(labels_str[int(label)])
else:
metadata_dict["labelnames"].append(filename)

metadata_dict["s_centroid"].append(np.round(np.mean(spectral_centroid),2))
metadata_dict["s_rolloff"].append(np.round(np.mean(spectral_rolloff),2))
metadata_dict["s_bandwidth"].append(np.round(np.mean(spectral_bandwidth),2))
Expand Down

0 comments on commit d8ed865

Please sign in to comment.