Skip to content

Commit

Permalink
Builds emotion data from local json saved from jupyter notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarland committed Mar 4, 2021
1 parent d1bf48f commit 6adab8d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 176 deletions.
171 changes: 1 addition & 170 deletions data/test_audio/actor_emotions.json
Original file line number Diff line number Diff line change
@@ -1,170 +1 @@
[
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "hanks",
"predictedEmotion": "happy",
"predictedSex": "Female",
"probabilities": [
0.15,
0.145,
0.215,
0.28,
0.075,
0.135
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "mustard",
"predictedEmotion": "angry",
"predictedSex": "Male",
"probabilities": [
0.265,
0.07,
0.09,
0.245,
0.15,
0.18
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "ewdavid",
"predictedEmotion": "surprise",
"predictedSex": "Female",
"probabilities": [
0.205,
0.07,
0.105,
0.19,
0.125,
0.305
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "gretchen",
"predictedEmotion": "fearful",
"predictedSex": "Male",
"probabilities": [
0.185,
0.08,
0.39,
0.18,
0.075,
0.09
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "nathan",
"predictedEmotion": "sad",
"predictedSex": "Male",
"probabilities": [
0.075,
0.295,
0.035,
0.1,
0.36,
0.135
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "vader",
"predictedEmotion": "happy",
"predictedSex": "Male",
"probabilities": [
0.135,
0.085,
0.215,
0.27,
0.185,
0.11
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "theoffice",
"predictedEmotion": "surprise",
"predictedSex": "Female",
"probabilities": [
0.165,
0.145,
0.14,
0.105,
0.195,
0.25
]
},
{
"emotionCategories": [
"angry",
"disgust",
"fearful",
"happy",
"sad",
"surprise"
],
"keyword": "witch",
"predictedEmotion": "angry",
"predictedSex": "Male",
"probabilities": [
0.28,
0.12,
0.265,
0.165,
0.11,
0.06
]
}
]
[{"predictedEmotion": "happy", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.15, 0.145, 0.215, 0.28, 0.075, 0.135], "predictedSex": "Female", "keyword": "hanks"}, {"predictedEmotion": "angry", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.265, 0.07, 0.09, 0.245, 0.15, 0.18], "predictedSex": "Male", "keyword": "mustard"}, {"predictedEmotion": "surprise", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.205, 0.07, 0.105, 0.19, 0.125, 0.305], "predictedSex": "Female", "keyword": "ewdavid"}, {"predictedEmotion": "fearful", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.185, 0.08, 0.39, 0.18, 0.075, 0.09], "predictedSex": "Male", "keyword": "gretchen"}, {"predictedEmotion": "sad", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.075, 0.295, 0.035, 0.1, 0.36, 0.135], "predictedSex": "Male", "keyword": "nathan"}, {"predictedEmotion": "happy", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.135, 0.085, 0.215, 0.27, 0.185, 0.11], "predictedSex": "Male", "keyword": "vader"}, {"predictedEmotion": "surprise", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.165, 0.145, 0.14, 0.105, 0.195, 0.25], "predictedSex": "Female", "keyword": "theoffice"}, {"predictedEmotion": "angry", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.28, 0.12, 0.265, 0.165, 0.11, 0.06], "predictedSex": "Male", "keyword": "witch"}]
150 changes: 150 additions & 0 deletions data_prep/tv_movies_emotions_prediction.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "biological-cocktail",
"metadata": {},
"outputs": [],
"source": [
"from pylab import *\n",
"import os\n",
"import pandas as pd\n",
"import librosa\n",
"import librosa.display\n",
"from joblib import load"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "exclusive-heritage",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def input_parser(input_file):\n",
" try:\n",
" X, sample_rate = librosa.load(input_file, res_type='kaiser_fast') \n",
" mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=128).T,axis=0) \n",
" except Exception as e:\n",
" print(\"Error encountered while parsing file: \", input_file)\n",
" return None\n",
" feature = mfccs.tolist()\n",
" \n",
" return feature"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "italic-display",
"metadata": {},
"outputs": [],
"source": [
"def model_test(input_file):\n",
" results_dict = {\n",
" \"predictedEmotion\": [],\n",
" \"emotionCategories\": [], \n",
" \"probabilities\": [], \n",
" \"predictedSex\": []\n",
" }\n",
" \n",
" model = load('../models/rf2_model.sav')\n",
" model2 = load('../models/gen_emo_rf_model.sav')\n",
" feature = input_parser(input_file)\n",
" arr = np.array(feature)\n",
" arr2d = np.reshape(arr, (1,128))\n",
" pred_emotion = model.predict(arr2d) \n",
" probs = model.predict_proba(arr2d)\n",
" emotion_labels = model.classes_\n",
" gender = model2.predict(arr2d)\n",
" if gender[0] == 0:\n",
" label = \"Male\"\n",
" elif gender[0] == 1:\n",
" label = \"Female\"\n",
" results_dict[\"predictedEmotion\"] = pred_emotion[0]\n",
" results_dict[\"emotionCategories\"] = emotion_labels.tolist()\n",
" results_dict[\"probabilities\"] = probs[0].tolist()\n",
" results_dict[\"predictedSex\"] = label\n",
" return results_dict"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "meaningful-steal",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'predictedEmotion': 'happy', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.15, 0.145, 0.215, 0.28, 0.075, 0.135], 'predictedSex': 'Female', 'keyword': 'hanks'}, {'predictedEmotion': 'angry', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.265, 0.07, 0.09, 0.245, 0.15, 0.18], 'predictedSex': 'Male', 'keyword': 'mustard'}, {'predictedEmotion': 'surprise', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.205, 0.07, 0.105, 0.19, 0.125, 0.305], 'predictedSex': 'Female', 'keyword': 'ewdavid'}, {'predictedEmotion': 'fearful', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.185, 0.08, 0.39, 0.18, 0.075, 0.09], 'predictedSex': 'Male', 'keyword': 'gretchen'}, {'predictedEmotion': 'sad', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.075, 0.295, 0.035, 0.1, 0.36, 0.135], 'predictedSex': 'Male', 'keyword': 'nathan'}, {'predictedEmotion': 'happy', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.135, 0.085, 0.215, 0.27, 0.185, 0.11], 'predictedSex': 'Male', 'keyword': 'vader'}, {'predictedEmotion': 'surprise', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.165, 0.145, 0.14, 0.105, 0.195, 0.25], 'predictedSex': 'Female', 'keyword': 'theoffice'}, {'predictedEmotion': 'angry', 'emotionCategories': ['angry', 'disgust', 'fearful', 'happy', 'sad', 'surprise'], 'probabilities': [0.28, 0.12, 0.265, 0.165, 0.11, 0.06], 'predictedSex': 'Male', 'keyword': 'witch'}]\n"
]
}
],
"source": [
"audio_file_paths = [\n",
" \"../static/audio/mono_a13_hanks.wav\", \n",
" \"../static/audio/mono_clue_mustard.wav\",\n",
" \"../static/audio/mono_ewdavid.wav\",\n",
" \"../static/audio/mono_meangirls_gretchen.wav\",\n",
" \"../static/audio/mono_nathan.wav\",\n",
" \"../static/audio/mono_starwars_vader.wav\",\n",
" \"../static/audio/mono_theoffice_michael.wav\",\n",
" \"../static/audio/mono_wizardofoz_witch.wav\"\n",
"]\n",
"tv_movie_sounds = []\n",
"keywords = [\"hanks\", \"mustard\", \"ewdavid\", \"gretchen\", \"nathan\", \"vader\", \"theoffice\", \"witch\"]\n",
"for i in range(len(audio_file_paths)):\n",
" a = model_test(audio_file_paths[i])\n",
" a[\"keyword\"] = keywords[i]\n",
" tv_movie_sounds.append(a.copy())\n",
"\n",
"print(tv_movie_sounds)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "assured-characteristic",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"with open('../static/js/actor_emotions.json', 'w') as file:\n",
" json.dump(tv_movie_sounds, file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "burning-wrestling",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions static/js/actor_emotions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"predictedEmotion": "happy", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.15, 0.145, 0.215, 0.28, 0.075, 0.135], "predictedSex": "Female", "keyword": "hanks"}, {"predictedEmotion": "angry", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.265, 0.07, 0.09, 0.245, 0.15, 0.18], "predictedSex": "Male", "keyword": "mustard"}, {"predictedEmotion": "surprise", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.205, 0.07, 0.105, 0.19, 0.125, 0.305], "predictedSex": "Female", "keyword": "ewdavid"}, {"predictedEmotion": "fearful", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.185, 0.08, 0.39, 0.18, 0.075, 0.09], "predictedSex": "Male", "keyword": "gretchen"}, {"predictedEmotion": "sad", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.075, 0.295, 0.035, 0.1, 0.36, 0.135], "predictedSex": "Male", "keyword": "nathan"}, {"predictedEmotion": "happy", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.135, 0.085, 0.215, 0.27, 0.185, 0.11], "predictedSex": "Male", "keyword": "vader"}, {"predictedEmotion": "surprise", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.165, 0.145, 0.14, 0.105, 0.195, 0.25], "predictedSex": "Female", "keyword": "theoffice"}, {"predictedEmotion": "angry", "emotionCategories": ["angry", "disgust", "fearful", "happy", "sad", "surprise"], "probabilities": [0.28, 0.12, 0.265, 0.165, 0.11, 0.06], "predictedSex": "Male", "keyword": "witch"}]
11 changes: 5 additions & 6 deletions static/js/musicCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ document.querySelectorAll('.pButton').forEach(item => {
function play(event) {
var target = event.target;
var url = d3.select(target).select('source').attr('src');
var filename = ''
// var filename = ''
console.log(url);
playAudio(url)

// link.href = url;
filename = url;
d3.json("/tv_movie", function(data) {
d3.json("static/js/actor_emotions.json", function(data) {


["hanks", "mustard", "ewdavid", "gretchen", "nathan", "vader", "theoffice", "witch"].forEach(function(keyword, index) {
["hanks", "mustard", "ewdavid", "gretchen", "nathan", "vader", "theoffice", "witch"].forEach(function(keyword, i) {
if(url.includes(keyword)) {
console.log(keyword, index);
buildBarChart(data[index].predictedEmotion, data[index].emotionCategories, data[index].probabilities, data[index].predictedSex)
buildBarChart(data[i].predictedEmotion, data[i].emotionCategories, data[i].probabilities, data[i].predictedSex);

}
})

Expand Down

0 comments on commit 6adab8d

Please sign in to comment.