-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepDatasetForDNN.py
80 lines (63 loc) · 3.29 KB
/
prepDatasetForDNN.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
79
80
import os
import sys
import glob
import numpy as np
import scipy.io.wavfile
from energy import getTopEnergySegmentsIndices
from featureExtraction import extractFeatures
from featureExtraction import getSegmentFeaturesUsingIndices
def main():
# path to audio file dataset
AUDIO_DATASET = os.path.dirname(os.path.realpath(__file__)) + "/dataset/"
# path to feature extracted dataset for DNN
CSV_DATASET = os.path.dirname(os.path.realpath(__file__)) + "/datasetForDNN.csv"
# CSV_DATASET = os.path.dirname(os.path.realpath(__file__)) + "/datasetForDNN_test.csv"
# maintain a dictionary for emotion label and labelNumber
emotions = {}
emotionLabelNum = 0 # this will be needed to set the target in csv file
emotionDirPaths = glob.glob(AUDIO_DATASET + "*")
csv_dataset = open(CSV_DATASET, "w")
for emotionDirPath in emotionDirPaths:
# keep a count of segment per emotions
countSegmentsPerEmotion = 0
# emotionLabel is also the directory name in the dataset directory
emotionLabel = emotionDirPath.split("/")[-1]
# set the emotion label
emotions[emotionLabel] = emotionLabelNum
# for all files in the emotionLabel directory, generate csv data
print("Generating csv data for : " + emotionLabel)
wavFilesPath = os.path.join(AUDIO_DATASET, emotionLabel, "*")
# print(wavFilesPath)
# setup progressBar
progressBarWidth = 50
sys.stdout.write("[%s]" % (" " * progressBarWidth))
sys.stdout.flush()
sys.stdout.write("\b" * (progressBarWidth+1)) # return to start of line, after '['
wavFiles = glob.glob(wavFilesPath)
numberOfWavFiles = len(wavFiles)
progressBarUpdatePerFiles = int(numberOfWavFiles/progressBarWidth)
countFiles = 0
for wavFile in wavFiles:
utteranceName = wavFile.split("/")[-1]
audioRate, audioData = scipy.io.wavfile.read(wavFile)
frameFeatureMatrix = extractFeatures(audioData, audioRate)
topSegmentIndices = getTopEnergySegmentsIndices(audioData, audioRate)
topSegmentFeatureMatrix = getSegmentFeaturesUsingIndices(frameFeatureMatrix, 25, topSegmentIndices)
# for each top segment in audioData, write the feature vector into csv_dataset along with target emotionLabelNum
for topSegmentIndex in range(len(topSegmentFeatureMatrix)):
featureVector = ",".join(['%.8f' % num for num in topSegmentFeatureMatrix[topSegmentIndex]])
featureVector = utteranceName + "," + featureVector + "," + str(emotionLabelNum) + "\n"
csv_dataset.write(featureVector)
countSegmentsPerEmotion += 1
# update the progressBar
countFiles += 1
if (countFiles % progressBarUpdatePerFiles == 0) and (int(countFiles/progressBarUpdatePerFiles) <= 50): # won't let the progressbar #'s exceed 50 repetitions
sys.stdout.write("#")
sys.stdout.flush()
sys.stdout.write("\n")
# print the count for each emotion
print("Number of segments for emotion : " + emotionLabel + " [ " + str(emotionLabelNum) + " ] : " + str(countSegmentsPerEmotion))
emotionLabelNum += 1
csv_dataset.close()
if __name__ == '__main__':
main()