{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'cv2'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel_selection\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m train_test_split\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmetrics\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m accuracy_score\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mcv2\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtensorflow\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mkeras\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Sequential\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'cv2'" ] } ], "source": [ "from sklearn import svm\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import accuracy_score\n", "import cv2\n", "import numpy as np\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Dropout, Flatten\n", "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", "from tensorflow.keras.utils import to_categorical\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "\n", "# Define the emotions\n", "emotions = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']\n", "\n", "# Load the dataset\n", "train_dir = 'path_to_your_dataset/train'\n", "test_dir = 'path_to_your_dataset/test'\n", "\n", "# Define the data generator\n", "train_datagen = ImageDataGenerator(rescale=1./255)\n", "test_datagen = ImageDataGenerator(rescale=1./255)\n", "\n", "train_generator = train_datagen.flow_from_directory(\n", " train_dir,\n", " target_size=(48, 48),\n", " batch_size=32,\n", " class_mode='categorical')\n", "\n", "test_generator = test_datagen.flow_from_directory(\n", " test_dir,\n", " target_size=(48, 48),\n", " batch_size=32,\n", " class_mode='categorical')\n", "\n", "# Define the CNN model\n", "model = Sequential()\n", "model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 3)))\n", "model.add(Conv2D(32, (3, 3), activation='relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Conv2D(64, (3, 3), activation='relu'))\n", "model.add(Conv2D(64, (3, 3), activation='relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Conv2D(128, (3, 3), activation='relu'))\n", "model.add(Conv2D(128, (3, 3), activation='relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Flatten())\n", "model.add(Dense(128, activation='relu'))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(7, activation='softmax'))\n", "\n", "# Compile the model\n", "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n", "\n", "# Train the model\n", "model.fit(train_generator, epochs=10)\n", "\n", "# Define the SVM model\n", "svm_model = svm.SVC(kernel='rbf', C=1)\n", "\n", "# Extract features from the CNN model\n", "cnn_features = model.layers[-2].output\n", "\n", "# Train the SVM model\n", "svm_model.fit(cnn_features, train_generator.classes)\n", "\n", "# Define a function to predict the emotion\n", "def predict_emotion(face):\n", " face = cv2.resize(face, (48, 48))\n", " face = face.reshape(1, 48, 48, 3)\n", " face = face / 255.0\n", " features = model.predict(face)\n", " emotion = svm_model.predict(features)\n", " return emotions[emotion[0]]" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.1" } }, "nbformat": 4, "nbformat_minor": 2 }