This is a post to introduce you to the idea of encapsulating functionality with object-oriented programming.
We simply put the emotional classification of speech that was demonstrated in this post in a python class like this:
import opensmile
import os
import audformat
from sklearn import svm
import sounddevice as sd
import soundfile as sf
from scipy.io.wavfile import write
class EmoRec():
root = './emodb/'
clf = None
filename = 'emorec.wav'
sr = 16000
def __init__(self):
self.smile = opensmile.Smile(
feature_set=opensmile.FeatureSet.GeMAPSv01b,
feature_level=opensmile.FeatureLevel.Functionals,
)
if not os.path.isdir(self.root):
self.download_emodb()
db = audformat.Database.load(self.root)
db.map_files(lambda x: os.path.join(self.root, x))
self.df_emo = db.tables['emotion'].df
self.df_files = db.tables['files'].df
if not self.clf:
self.train_model()
def download_emodb(self):
os.system('wget -c https://tubcloud.tu-berlin.de/s/LzPWz83Fjneb6SP/download')
os.system('mv download emodb_audformat.zip')
os.system('unzip emodb_audformat.zip')
os.system('rm emodb_audformat.zip')
def train_model(self):
print('training a model...')
df_feats = self.smile.process_files(self.df_emo.index)
train_labels = self.df_emo.emotion
train_feats = df_feats
self.clf = svm.SVC(kernel='linear', C=.001)
self.clf.fit(train_feats, train_labels)
print('done')
def classify(self, wavefile):
test_feats = self.smile.process_file(wavefile)
return self.clf.predict(test_feats)
def classify_from_micro(self, seconds):
self.record(seconds)
return self.classify(self.filename)[0]
def record(self, seconds):
data = sd.rec(int(seconds * self.sr), samplerate=self.sr, channels=1)
sd.wait()
write(self.filename, self.sr, data)
def main():
test = EmoRec()
print(test.classify_from_micro(3))
if __name__ == "__main__":
main()
To try this you could store the above in a file called , for example, 'emorec.py' and then in a jupyter notebook, call the constructor
import emorec
emoRec = emorec.EmoRec()
and use the functionality
result = emoRec.classify_from_micro(3)
print(f'emodb thinks your emotion is {result}')