forked from Open-CT/opendata
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
|
|
from extract_audio_feature import extract_audio_feature
|
|
import numpy as np
|
|
import csv
|
|
import os
|
|
from keras import models
|
|
WAVE_OUTPUT_FILENAME = "../Output/output.wav"
|
|
|
|
|
|
def load_trained_model():
|
|
MODEL_FILE_PATH = '../Output/english_class.h5'
|
|
model = models.load_model(MODEL_FILE_PATH)
|
|
return model
|
|
|
|
model = load_trained_model()
|
|
def predict_emotion():
|
|
header = 'filename emotion confidence'
|
|
header = header.split()
|
|
file = open('../Output/test.csv', 'w', newline='')
|
|
with file:
|
|
writer = csv.writer(file)
|
|
writer.writerow(header)
|
|
for label in os.listdir(f'class'):
|
|
if label != '_desktop.ini':
|
|
for audio in os.listdir(f'class/{label}'):
|
|
if audio.split('.')[-1] == 'wav' and audio != '_desktop.ini':
|
|
audio_path = f'class/{label}/{audio}'
|
|
print(audio_path)
|
|
to_append = f'{"0809"}_{label}_{audio}'
|
|
audio_features = extract_audio_feature(WAVE_OUTPUT_FILENAME).split()
|
|
result1 = model.predict(np.array(audio_features, dtype=float).reshape(1, -1))
|
|
for i in range(0, result1.shape[0]):
|
|
emotion, confidence = handle_onehot(result1[i])
|
|
print('custom model result:[emotion:', emotion, ', confidence:', confidence, ']')
|
|
to_append += f' {emotion}'
|
|
to_append += f' {confidence}'
|
|
file = open('../Output/test.csv', 'a', newline='')
|
|
with file:
|
|
writer = csv.writer(file)
|
|
writer.writerow(to_append.split())
|
|
|
|
# result2 = turicreate_classifier.predict(WAVE_OUTPUT_FILENAME)
|
|
|
|
|
|
# for i in range(0, result2.shape[0]):
|
|
# emotion, confidence = handle_onehot(np.array(result2[i]))
|
|
# print('turicreate result:[emotion:', emotion, ', confidence:', confidence, ']')
|
|
|
|
|
|
def handle_onehot(onehot):
|
|
emotions = 'neutral calm happy sad angry fear disgust surprise'.split()
|
|
confidence = max(onehot)
|
|
itemindex = np.where(onehot == confidence)[0][0]
|
|
emotion = emotions[itemindex]
|
|
return emotion, confidence
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
predict_emotion()
|