-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelAudio.py
More file actions
116 lines (94 loc) · 3.9 KB
/
LabelAudio.py
File metadata and controls
116 lines (94 loc) · 3.9 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# select the folder
# split the data with preprcess
# play sound for user
# sort the data with 0, 1, or something else if confused
import os
import shutil
import librosa
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd
import soundfile as sf
from rich import print
import audioUtils as utils
DATADIR = "dataset/"
TRAINING_DIR = DATADIR+ "training/"
VALIDATE_DIR = DATADIR+ "validate/"
ARCHIVE_DIR = DATADIR+ "archiveAudio/"
LABELPATH = {
"y": "1yes/",
"n": "0no/",
"i": DATADIR+"notSure/",
"0": DATADIR+"notLabeled/"
}
LabelVisualSelection = " [green]_______[/] [red]_______[/] [cyan]_______[/] [yellow]_______[/]\n"\
"[green]| |[/] [red]| |[/] [cyan]| |[/] [yellow]| |[/]\n"\
"[green]| Yes |[/] [red]| No |[/] [cyan]| Idk |[/] [yellow]| Exit |[/]\n"\
"[green]|___y___|[/] [red]|__n____|[/] [cyan]|___i___|[/] [yellow]|___e___|[/]"
SplitVisualSelection = " [green]_______[/] [red]_______[/] \n"\
"[green]| |[/] [red]| |[/]\n"\
"[green]| Yes |[/] [red]| No |[/]\n"\
"[green]|___y___|[/] [red]|___n___|[/]"
TypeVisualSelection = " [green]_______[/] [red]_______[/]\n"\
"[green]| |[/] [red]| |[/]\n"\
"[green]| Train |[/] [red]| Valid |[/]\n"\
"[green]|___t___|[/] [red]|___v___|[/] "
# Clear the terminal window
def clearTerminal():
os.system('cls' if os.name == 'nt' else 'clear')
def getUserInput():
while True:
user_input = input("Please enter 'y' for Yes, 'n' for No, or 'i' for Idk(e): ")
if user_input.lower() not in ('y', 'n', 'i', 'e'):
print("Invalid input. Please enter 'y', 'n', or 'i'.")
else:
break
print("You entered:", user_input.lower())
return user_input.lower()
def LabelAudio(folderDir, split):
for fileName in os.listdir(LABELPATH["0"]):
folder_path = LABELPATH["0"] + fileName
y, sr = librosa.load(folder_path)
y_split = utils.cut_audio(y, 10000, split)
index = 0
# Plot the sound file
for i in y_split:
sd.play(i, 22050,blocking=True)
newfile = fileName.replace(".wav", "_" + ('s' if split else '') + str(index) + ".wav")
index = index + 1
clearTerminal()
print("playing ", newfile)
print(LabelVisualSelection)
input2 = getUserInput()
if (input2 == 'e'):
break
fileOutPath = (folderDir + LABELPATH[input2] if (input2 != 'i') else LABELPATH[input2]) + newfile
print("writing to ", fileOutPath)
sf.write(fileOutPath, i, sr, 'PCM_24')
shutil.move(folder_path, ARCHIVE_DIR + fileName)
print("Moved og file to ", ARCHIVE_DIR )
# librosa.write_wav('training/' + newfile, i, sr)
if __name__ == '__main__':
while True:
print(TypeVisualSelection)
user_input = input("Please enter to 't' or 'v' to label training or validation: ")
if user_input.lower() not in ('t', 'v'):
print("Invalid input. Please enter to 't' or 'v' to label training or validation: ")
else:
break
if (user_input.lower() == "t"):
dir = TRAINING_DIR
else:
dir = VALIDATE_DIR
while True:
print(SplitVisualSelection)
user_input = input("Please enter 'y' for Yes, 'n' for No to filter data files: ")
if user_input.lower() not in ('y', 'n'):
print("Invalid input. Please enter 'y' for Yes, 'n' for No to filter data files: ")
else:
break
if (user_input.lower() == "y"):
split = True
else:
split = False
LabelAudio(dir, split)