File size: 5,223 Bytes
4983aaa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
{
"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
}
|