diff --git a/.gitignore b/.gitignore index 4d4a299..ca1dcf4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.conda/ \ No newline at end of file +.conda/ +output/ +*.mp4 \ No newline at end of file diff --git a/SE8.ipynb b/SE8.ipynb index d13903b..7ef0845 100644 --- a/SE8.ipynb +++ b/SE8.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { @@ -32,6 +33,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -50,6 +52,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -78,6 +81,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -125,6 +129,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -150,6 +155,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -163,6 +169,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -172,6 +179,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -179,6 +187,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -195,6 +204,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -211,6 +221,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -218,6 +229,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -257,6 +269,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -281,6 +294,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -433,6 +447,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -440,6 +455,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -447,11 +463,17 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 1, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "from fer import FER, Video\n", + "import cv2" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -459,11 +481,17 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 2, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "input_video_path = \"face.mp4\"\n", + "output_video_path = \"out.mp4\"" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -471,11 +499,16 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 3, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "video = Video(input_video_path)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -483,11 +516,31 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 5, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:fer:30.00 fps, 80 frames, 2.67 seconds\n", + "INFO:fer:Making directories at output\n", + "100%|██████████| 80/80 [00:06<00:00, 11.67frames/s]\n", + "INFO:fer:Completed analysis: saved to output\\face_output.mp4\n", + "INFO:fer:Starting to Zip\n", + "INFO:fer:Compressing: 62%\n", + "INFO:fer:Zip has finished\n" + ] + } + ], + "source": [ + "detector = FER(mtcnn=True)\n", + "analysis = video.analyze(detector, display=False)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -495,11 +548,24 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 6, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'box0': [72, 192, 207, 269], 'angry0': 0.19, 'disgust0': 0.0, 'fear0': 0.06, 'happy0': 0.0, 'sad0': 0.17, 'surprise0': 0.0, 'neutral0': 0.58}, {'box0': [72, 192, 208, 269], 'angry0': 0.14, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.68}, {'box0': [71, 191, 208, 271], 'angry0': 0.2, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.15, 'surprise0': 0.0, 'neutral0': 0.62}, {'box0': [71, 192, 208, 267], 'angry0': 0.24, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.15, 'surprise0': 0.0, 'neutral0': 0.59}, {'box0': [72, 193, 206, 266], 'angry0': 0.28, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.13, 'surprise0': 0.0, 'neutral0': 0.56}, {'box0': [69, 189, 209, 265], 'angry0': 0.33, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.11, 'surprise0': 0.0, 'neutral0': 0.51}, {'box0': [70, 191, 205, 262], 'angry0': 0.3, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.12, 'surprise0': 0.0, 'neutral0': 0.56}, {'box0': [69, 190, 206, 263], 'angry0': 0.25, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.11, 'surprise0': 0.0, 'neutral0': 0.62}, {'box0': [69, 190, 206, 262], 'angry0': 0.22, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.62}, {'box0': [73, 189, 203, 264], 'angry0': 0.17, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.67}, {'box0': [72, 188, 203, 262], 'angry0': 0.17, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.1, 'surprise0': 0.0, 'neutral0': 0.7}, {'box0': [72, 187, 202, 263], 'angry0': 0.16, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.11, 'surprise0': 0.0, 'neutral0': 0.7}, {'box0': [72, 187, 204, 263], 'angry0': 0.12, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.08, 'surprise0': 0.0, 'neutral0': 0.78}, {'box0': [72, 186, 204, 263], 'angry0': 0.1, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.09, 'surprise0': 0.0, 'neutral0': 0.79}, {'box0': [70, 185, 206, 264], 'angry0': 0.15, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.12, 'surprise0': 0.0, 'neutral0': 0.7}, {'box0': [69, 186, 206, 260], 'angry0': 0.18, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.64}, {'box0': [70, 188, 205, 255], 'angry0': 0.14, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.67}, {'box0': [71, 189, 204, 254], 'angry0': 0.17, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.17, 'surprise0': 0.0, 'neutral0': 0.63}, {'box0': [61, 180, 213, 265], 'angry0': 0.36, 'disgust0': 0.0, 'fear0': 0.01, 'happy0': 0.0, 'sad0': 0.08, 'surprise0': 0.0, 'neutral0': 0.54}, {'box0': [71, 189, 203, 257], 'angry0': 0.24, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.59}, {'box0': [68, 188, 204, 258], 'angry0': 0.15, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.17, 'surprise0': 0.0, 'neutral0': 0.64}, {'box0': [64, 181, 210, 265], 'angry0': 0.24, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.24, 'surprise0': 0.0, 'neutral0': 0.49}, {'box0': [64, 179, 209, 264], 'angry0': 0.35, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.13, 'surprise0': 0.0, 'neutral0': 0.5}, {'box0': [69, 182, 205, 255], 'angry0': 0.44, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.23, 'surprise0': 0.0, 'neutral0': 0.31}, {'box0': [71, 182, 202, 252], 'angry0': 0.41, 'disgust0': 0.0, 'fear0': 0.01, 'happy0': 0.0, 'sad0': 0.3, 'surprise0': 0.0, 'neutral0': 0.27}, {'box0': [71, 180, 201, 251], 'angry0': 0.44, 'disgust0': 0.0, 'fear0': 0.01, 'happy0': 0.0, 'sad0': 0.25, 'surprise0': 0.0, 'neutral0': 0.3}, {'box0': [65, 177, 207, 258], 'angry0': 0.41, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.0, 'sad0': 0.23, 'surprise0': 0.0, 'neutral0': 0.34}, {'box0': [73, 181, 200, 248], 'angry0': 0.48, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.29, 'surprise0': 0.0, 'neutral0': 0.2}, {'box0': [72, 179, 201, 251], 'angry0': 0.4, 'disgust0': 0.0, 'fear0': 0.05, 'happy0': 0.0, 'sad0': 0.34, 'surprise0': 0.0, 'neutral0': 0.21}, {'box0': [61, 172, 208, 257], 'angry0': 0.39, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.15, 'surprise0': 0.0, 'neutral0': 0.42}, {'box0': [63, 174, 205, 252], 'angry0': 0.4, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.19, 'surprise0': 0.0, 'neutral0': 0.37}, {'box0': [64, 175, 205, 252], 'angry0': 0.49, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.25, 'surprise0': 0.0, 'neutral0': 0.22}, {'box0': [65, 174, 204, 251], 'angry0': 0.42, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.21, 'surprise0': 0.0, 'neutral0': 0.32}, {'box0': [64, 175, 204, 250], 'angry0': 0.34, 'disgust0': 0.0, 'fear0': 0.06, 'happy0': 0.0, 'sad0': 0.27, 'surprise0': 0.0, 'neutral0': 0.33}, {'box0': [64, 174, 205, 251], 'angry0': 0.42, 'disgust0': 0.0, 'fear0': 0.05, 'happy0': 0.0, 'sad0': 0.24, 'surprise0': 0.0, 'neutral0': 0.28}, {'box0': [64, 174, 205, 251], 'angry0': 0.45, 'disgust0': 0.0, 'fear0': 0.05, 'happy0': 0.0, 'sad0': 0.23, 'surprise0': 0.0, 'neutral0': 0.26}, {'box0': [63, 173, 206, 253], 'angry0': 0.48, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.2, 'surprise0': 0.01, 'neutral0': 0.27}, {'box0': [62, 173, 207, 255], 'angry0': 0.38, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.21, 'surprise0': 0.01, 'neutral0': 0.36}, {'box0': [62, 173, 207, 257], 'angry0': 0.35, 'disgust0': 0.0, 'fear0': 0.03, 'happy0': 0.0, 'sad0': 0.22, 'surprise0': 0.01, 'neutral0': 0.38}, {'box0': [66, 176, 202, 254], 'angry0': 0.43, 'disgust0': 0.0, 'fear0': 0.05, 'happy0': 0.01, 'sad0': 0.25, 'surprise0': 0.0, 'neutral0': 0.26}, {'box0': [65, 176, 206, 258], 'angry0': 0.33, 'disgust0': 0.0, 'fear0': 0.04, 'happy0': 0.0, 'sad0': 0.15, 'surprise0': 0.0, 'neutral0': 0.47}, {'box0': [65, 175, 207, 260], 'angry0': 0.17, 'disgust0': 0.0, 'fear0': 0.05, 'happy0': 0.01, 'sad0': 0.14, 'surprise0': 0.0, 'neutral0': 0.64}, {'box0': [65, 174, 209, 263], 'angry0': 0.22, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.01, 'sad0': 0.15, 'surprise0': 0.0, 'neutral0': 0.6}, {'box0': [66, 177, 208, 259], 'angry0': 0.14, 'disgust0': 0.0, 'fear0': 0.02, 'happy0': 0.07, 'sad0': 0.11, 'surprise0': 0.0, 'neutral0': 0.67}, {'box0': [67, 173, 210, 262], 'angry0': 0.07, 'disgust0': 0.0, 'fear0': 0.01, 'happy0': 0.35, 'sad0': 0.05, 'surprise0': 0.0, 'neutral0': 0.52}, {'box0': [67, 174, 209, 262], 'angry0': 0.02, 'disgust0': 0.0, 'fear0': 0.01, 'happy0': 0.66, 'sad0': 0.03, 'surprise0': 0.0, 'neutral0': 0.29}, {'box0': [68, 175, 208, 260], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.89, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.1}, {'box0': [72, 176, 202, 256], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.93, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.07}, {'box0': [69, 174, 204, 259], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.97, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.03}, {'box0': [65, 173, 208, 266], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [61, 172, 213, 269], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [61, 174, 211, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [63, 174, 210, 269], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [63, 174, 210, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [63, 175, 213, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [64, 174, 209, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [64, 173, 210, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [66, 174, 209, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [67, 174, 208, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [66, 173, 211, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [62, 172, 224, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [65, 171, 216, 276], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [62, 172, 224, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [60, 172, 224, 272], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [67, 173, 214, 274], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [60, 172, 223, 272], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [61, 171, 224, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [62, 172, 223, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [66, 170, 213, 275], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [59, 171, 225, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [61, 172, 223, 269], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [60, 172, 223, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [61, 171, 223, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [64, 165, 216, 280], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [60, 170, 222, 271], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.99, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.01}, {'box0': [63, 172, 221, 270], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 1.0, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.0}, {'box0': [66, 174, 209, 269], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.98, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.02}, {'box0': [70, 178, 211, 264], 'angry0': 0.0, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.93, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.07}, {'box0': [71, 181, 209, 266], 'angry0': 0.01, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.58, 'sad0': 0.0, 'surprise0': 0.0, 'neutral0': 0.4}, {'box0': [73, 186, 198, 251], 'angry0': 0.03, 'disgust0': 0.0, 'fear0': 0.0, 'happy0': 0.06, 'sad0': 0.01, 'surprise0': 0.0, 'neutral0': 0.89}]\n" + ] + } + ], + "source": [ + "print(analysis)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -507,6 +573,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -516,11 +583,22 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 7, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "import os\n", + "import soundfile\n", + "import numpy as np\n", + "import librosa\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.neural_network import MLPClassifier\n", + "from sklearn.metrics import accuracy_score" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -528,11 +606,32 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 14, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "def extract_features(file_name, mfcc, chroma, mel):\n", + " with soundfile.SoundFile(file_name) as sound_file:\n", + " X = sound_file.read(dtype=\"float32\")\n", + " sample_rate = sound_file.samplerate\n", + " if chroma:\n", + " stft = np.abs(librosa.stft(X))\n", + " result = np.array([])\n", + " if mfcc:\n", + " mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)\n", + " result = np.hstack((result, mfccs))\n", + " if chroma:\n", + " chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)\n", + " result = np.hstack((result, chroma))\n", + " if mel:\n", + " mel = np.mean(librosa.feature.melspectrogram(y=X, sr=sample_rate).T, axis=0)\n", + " result = np.hstack((result, mel))\n", + " return result\n" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -540,11 +639,20 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 9, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "emotions = {\n", + " '03': 'happy',\n", + " '04': 'sad',\n", + " '05': 'angry'\n", + "}" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -552,11 +660,44 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 12, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "def load_data(test_size=0.2):\n", + " x, y = [], []\n", + " main_data_folder = \"C:\\\\Users\\\\jadamski\\\\Downloads\\\\speech-emotion-recognition-ravdess-data\"\n", + " \n", + "\n", + " # Iterate through each actor's folder\n", + " for actor_folder in os.listdir(main_data_folder):\n", + " actor_folder_path = os.path.join(main_data_folder, actor_folder)\n", + "\n", + " # Ensure it's a directory\n", + " if os.path.isdir(actor_folder_path):\n", + " \n", + " # Iterate through each file inside the actor's folder\n", + " for file in os.listdir(actor_folder_path):\n", + " emotion_code = file.split(\"-\")[2]\n", + " \n", + " # Process only files with the emotions you're interested in\n", + " if emotion_code in emotions:\n", + " file_name = os.path.join(actor_folder_path, file)\n", + " emotion = emotions[emotion_code]\n", + " \n", + " # Extract features from the audio file\n", + " feature = extract_features(file_name, mfcc=True, chroma=True, mel=True)\n", + " \n", + " # Append features and emotion to the lists\n", + " x.append(feature)\n", + " y.append(emotion)\n", + " \n", + " return train_test_split(np.array(x), y, test_size=test_size, random_state=9)\n" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -564,11 +705,16 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 15, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = load_data(test_size=0.25)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -576,11 +722,24 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 16, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Treningowe: 432, Testowe: 144\n" + ] + } + ], + "source": [ + "print(f'Treningowe: {x_train.shape[0]}, Testowe: {x_test.shape[0]}')" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -588,11 +747,24 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 17, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Liczba cech: 180\n" + ] + } + ], + "source": [ + "print(f'Liczba cech: {x_train.shape[1]}')\n" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -600,11 +772,16 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 18, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "model = MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -612,11 +789,42 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 19, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING:py.warnings:c:\\Users\\jadamski\\.conda\\envs\\empatia\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:686: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.\n", + " warnings.warn(\n", + "\n" + ] + }, + { + "data": { + "text/html": [ + "
MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n",
+       "              learning_rate='adaptive', max_iter=500)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n", + " learning_rate='adaptive', max_iter=500)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model.fit(x_train, y_train)" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -624,11 +832,16 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 20, "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "y_pred = model.predict(x_test)\n" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -636,11 +849,25 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 21, "metadata": {}, - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dokładność: 80.56%\n" + ] + } + ], + "source": [ + "accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)\n", + "print(\"Dokładność: {:.2f}%\".format(accuracy * 100))\n" + ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ diff --git a/data.csv b/data.csv new file mode 100644 index 0000000..bc3b1ad --- /dev/null +++ b/data.csv @@ -0,0 +1,81 @@ +angry0,box0,disgust0,fear0,happy0,neutral0,sad0,surprise0 +0.19,"[72, 192, 207, 269]",0.0,0.06,0.0,0.58,0.17,0.0 +0.14,"[72, 192, 208, 269]",0.0,0.04,0.0,0.68,0.14,0.0 +0.2,"[71, 191, 208, 271]",0.0,0.02,0.0,0.62,0.15,0.0 +0.24,"[71, 192, 208, 267]",0.0,0.02,0.0,0.59,0.15,0.0 +0.28,"[72, 193, 206, 266]",0.0,0.02,0.0,0.56,0.13,0.0 +0.33,"[69, 189, 209, 265]",0.0,0.04,0.0,0.51,0.11,0.0 +0.3,"[70, 191, 205, 262]",0.0,0.02,0.0,0.56,0.12,0.0 +0.25,"[69, 190, 206, 263]",0.0,0.02,0.0,0.62,0.11,0.0 +0.22,"[69, 190, 206, 262]",0.0,0.02,0.0,0.62,0.14,0.0 +0.17,"[73, 189, 203, 264]",0.0,0.02,0.0,0.67,0.14,0.0 +0.17,"[72, 188, 203, 262]",0.0,0.02,0.0,0.7,0.1,0.0 +0.16,"[72, 187, 202, 263]",0.0,0.02,0.0,0.7,0.11,0.0 +0.12,"[72, 187, 204, 263]",0.0,0.02,0.0,0.78,0.08,0.0 +0.1,"[72, 186, 204, 263]",0.0,0.03,0.0,0.79,0.09,0.0 +0.15,"[70, 185, 206, 264]",0.0,0.03,0.0,0.7,0.12,0.0 +0.18,"[69, 186, 206, 260]",0.0,0.04,0.0,0.64,0.14,0.0 +0.14,"[70, 188, 205, 255]",0.0,0.04,0.0,0.67,0.14,0.0 +0.17,"[71, 189, 204, 254]",0.0,0.03,0.0,0.63,0.17,0.0 +0.36,"[61, 180, 213, 265]",0.0,0.01,0.0,0.54,0.08,0.0 +0.24,"[71, 189, 203, 257]",0.0,0.03,0.0,0.59,0.14,0.0 +0.15,"[68, 188, 204, 258]",0.0,0.03,0.0,0.64,0.17,0.0 +0.24,"[64, 181, 210, 265]",0.0,0.03,0.0,0.49,0.24,0.0 +0.35,"[64, 179, 209, 264]",0.0,0.02,0.0,0.5,0.13,0.0 +0.44,"[69, 182, 205, 255]",0.0,0.02,0.0,0.31,0.23,0.0 +0.41,"[71, 182, 202, 252]",0.0,0.01,0.0,0.27,0.3,0.0 +0.44,"[71, 180, 201, 251]",0.0,0.01,0.0,0.3,0.25,0.0 +0.41,"[65, 177, 207, 258]",0.0,0.02,0.0,0.34,0.23,0.0 +0.48,"[73, 181, 200, 248]",0.0,0.03,0.0,0.2,0.29,0.0 +0.4,"[72, 179, 201, 251]",0.0,0.05,0.0,0.21,0.34,0.0 +0.39,"[61, 172, 208, 257]",0.0,0.03,0.0,0.42,0.15,0.0 +0.4,"[63, 174, 205, 252]",0.0,0.04,0.0,0.37,0.19,0.0 +0.49,"[64, 175, 205, 252]",0.0,0.04,0.0,0.22,0.25,0.0 +0.42,"[65, 174, 204, 251]",0.0,0.03,0.0,0.32,0.21,0.0 +0.34,"[64, 175, 204, 250]",0.0,0.06,0.0,0.33,0.27,0.0 +0.42,"[64, 174, 205, 251]",0.0,0.05,0.0,0.28,0.24,0.0 +0.45,"[64, 174, 205, 251]",0.0,0.05,0.0,0.26,0.23,0.0 +0.48,"[63, 173, 206, 253]",0.0,0.04,0.0,0.27,0.2,0.01 +0.38,"[62, 173, 207, 255]",0.0,0.04,0.0,0.36,0.21,0.01 +0.35,"[62, 173, 207, 257]",0.0,0.03,0.0,0.38,0.22,0.01 +0.43,"[66, 176, 202, 254]",0.0,0.05,0.01,0.26,0.25,0.0 +0.33,"[65, 176, 206, 258]",0.0,0.04,0.0,0.47,0.15,0.0 +0.17,"[65, 175, 207, 260]",0.0,0.05,0.01,0.64,0.14,0.0 +0.22,"[65, 174, 209, 263]",0.0,0.02,0.01,0.6,0.15,0.0 +0.14,"[66, 177, 208, 259]",0.0,0.02,0.07,0.67,0.11,0.0 +0.07,"[67, 173, 210, 262]",0.0,0.01,0.35,0.52,0.05,0.0 +0.02,"[67, 174, 209, 262]",0.0,0.01,0.66,0.29,0.03,0.0 +0.0,"[68, 175, 208, 260]",0.0,0.0,0.89,0.1,0.0,0.0 +0.0,"[72, 176, 202, 256]",0.0,0.0,0.93,0.07,0.0,0.0 +0.0,"[69, 174, 204, 259]",0.0,0.0,0.97,0.03,0.0,0.0 +0.0,"[65, 173, 208, 266]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[61, 172, 213, 269]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[61, 174, 211, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[63, 174, 210, 269]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[63, 174, 210, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[63, 175, 213, 271]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[64, 174, 209, 271]",0.0,0.0,0.99,0.0,0.0,0.0 +0.0,"[64, 173, 210, 271]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[66, 174, 209, 271]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[67, 174, 208, 270]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[66, 173, 211, 271]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[62, 172, 224, 271]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[65, 171, 216, 276]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[62, 172, 224, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[60, 172, 224, 272]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[67, 173, 214, 274]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[60, 172, 223, 272]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[61, 171, 224, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[62, 172, 223, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[66, 170, 213, 275]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[59, 171, 225, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[61, 172, 223, 269]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[60, 172, 223, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[61, 171, 223, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[64, 165, 216, 280]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[60, 170, 222, 271]",0.0,0.0,0.99,0.01,0.0,0.0 +0.0,"[63, 172, 221, 270]",0.0,0.0,1.0,0.0,0.0,0.0 +0.0,"[66, 174, 209, 269]",0.0,0.0,0.98,0.02,0.0,0.0 +0.0,"[70, 178, 211, 264]",0.0,0.0,0.93,0.07,0.0,0.0 +0.01,"[71, 181, 209, 266]",0.0,0.0,0.58,0.4,0.0,0.0 +0.03,"[73, 186, 198, 251]",0.0,0.0,0.06,0.89,0.01,0.0