{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os \n", "import numpy as np \n", "import pandas as pd \n", "import matplotlib.pyplot as plt \n", "import matplotlib.image as mimage \n", "import cv2 as cv \n", "import random \n", "from PIL import Image\n", " \n", "import warnings \n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d:\\\\Final _proj\\\\Driver Drawsines\\\\research'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "os.chdir(\"..\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d:\\\\Final _proj\\\\Driver Drawsines'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n" ] } ], "source": [ "from tensorflow.keras.models import load_model \n", "model = load_model(\"drowiness_new6.h5\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Final" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "###" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For Yawn , no-Yawn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 137ms/step\n", "[[0.5339012 0.15986206 0.22364037 0.08259626]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 147ms/step\n", "[[0.55441564 0.16944881 0.19709249 0.07904309]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 118ms/step\n", "[[0.4521452 0.13079698 0.31140834 0.10564953]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 111ms/step\n", "[[0.5303667 0.16052838 0.22346936 0.08563555]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 112ms/step\n", "[[0.5102811 0.15651394 0.24725401 0.08595096]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 96ms/step\n", "[[0.39522764 0.5094784 0.08734289 0.00795105]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 125ms/step\n", "[[0.4849061 0.5083012 0.00610906 0.0006836 ]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 124ms/step\n", "[[4.9457058e-01 4.9904898e-01 6.0036671e-03 3.7674789e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 113ms/step\n", "[[4.9783722e-01 4.9717689e-01 4.6812119e-03 3.0464309e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 112ms/step\n", "[[5.2321953e-01 4.6852756e-01 7.8288494e-03 4.2414822e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step\n", "[[4.9153149e-01 5.0222409e-01 5.8816043e-03 3.6273352e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 113ms/step\n", "[[0.54127944 0.44826204 0.00961303 0.00084548]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.50393206 0.4863479 0.00865785 0.0010622 ]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 118ms/step\n", "[[5.4283851e-01 4.5033500e-01 6.4291335e-03 3.9734103e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[5.2413237e-01 4.7132316e-01 4.3296791e-03 2.1475746e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 102ms/step\n", "[[5.1084113e-01 4.8559844e-01 3.2412172e-03 3.1920703e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 121ms/step\n", "[[5.7073164e-01 4.2556059e-01 3.4928408e-03 2.1485660e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 105ms/step\n", "[[5.5285156e-01 4.4224325e-01 4.5622713e-03 3.4294103e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step\n", "[[0.5427735 0.44871727 0.00793501 0.00057425]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 120ms/step\n", "[[5.4114133e-01 4.5342380e-01 5.1169088e-03 3.1804194e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 120ms/step\n", "[[5.7159519e-01 4.2561889e-01 2.5791947e-03 2.0669916e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 117ms/step\n", "[[5.7468766e-01 4.2153805e-01 3.5532070e-03 2.2111020e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[5.3590161e-01 4.5808852e-01 5.6171627e-03 3.9265043e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[5.5657232e-01 4.3508846e-01 7.7924151e-03 5.4685154e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 106ms/step\n", "[[0.49480158 0.49675417 0.00781286 0.00063132]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 116ms/step\n", "[[5.5822819e-01 4.3658417e-01 4.8851566e-03 3.0245088e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 97ms/step\n", "[[0.53632814 0.4521163 0.01081774 0.00073785]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 122ms/step\n", "[[0.5257215 0.4666978 0.00665365 0.00092702]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 113ms/step\n", "[[0.56758076 0.42414907 0.00691421 0.00135598]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[0.5783753 0.41040668 0.01005661 0.00116142]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 123ms/step\n", "[[0.61831737 0.37240994 0.00795365 0.00131897]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 126ms/step\n", "[[0.6174236 0.37296996 0.0084237 0.00118283]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[0.53627264 0.4550183 0.00787264 0.00083647]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 109ms/step\n", "[[6.3499963e-01 3.6145884e-01 3.0721761e-03 4.6933253e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[0.59015673 0.3910087 0.01667546 0.00215918]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 121ms/step\n", "[[0.547751 0.43992066 0.01099813 0.00133022]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 94ms/step\n", "[[6.1030549e-01 3.8535413e-01 3.9123669e-03 4.2805608e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 116ms/step\n", "[[0.5895906 0.39260563 0.01642497 0.00137877]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.4235793 0.56652987 0.00914386 0.00074703]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 119ms/step\n", "[[4.8226121e-01 5.0990009e-01 7.3582488e-03 4.8048754e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 139ms/step\n", "[[0.47468084 0.49073824 0.03259087 0.00199007]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 100ms/step\n", "[[0.48702973 0.5029544 0.00944369 0.00057216]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 106ms/step\n", "[[0.46537527 0.5274027 0.0066941 0.00052791]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 93ms/step\n", "[[0.425588 0.5606394 0.01319939 0.00057326]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[4.6433157e-01 5.3061748e-01 4.6703219e-03 3.8065348e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[5.0271779e-01 4.9165285e-01 5.3839809e-03 2.4538283e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 95ms/step\n", "[[4.9817729e-01 4.9809283e-01 3.4314308e-03 2.9849299e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 92ms/step\n", "[[0.4323189 0.55639476 0.01058941 0.00069697]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 112ms/step\n", "[[4.9058634e-01 5.0517666e-01 3.8562843e-03 3.8071233e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 98ms/step\n", "[[4.6864349e-01 5.2765518e-01 3.3698247e-03 3.3153337e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[4.7328150e-01 5.1489401e-01 1.1425589e-02 3.9892178e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 96ms/step\n", "[[5.6844020e-01 4.2386171e-01 7.2170394e-03 4.8100951e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 122ms/step\n", "[[4.7888020e-01 5.1815802e-01 2.7156596e-03 2.4622082e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step\n", "[[4.4938755e-01 5.4168677e-01 8.4247021e-03 5.0095661e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 97ms/step\n", "[[4.8189956e-01 5.1445591e-01 3.2346470e-03 4.0994020e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 114ms/step\n", "[[4.3830103e-01 5.5796260e-01 3.5162044e-03 2.2019379e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 103ms/step\n", "[[3.8675940e-01 6.1051351e-01 2.5933362e-03 1.3384310e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 122ms/step\n", "[[4.7633487e-01 5.1811528e-01 5.2490053e-03 3.0079822e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[0.5355513 0.4560149 0.00782073 0.00061306]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 97ms/step\n", "[[4.9573547e-01 5.0054139e-01 3.4869814e-03 2.3615584e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 98ms/step\n", "[[4.6156302e-01 5.3393996e-01 4.1647437e-03 3.3234234e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.52708125 0.46093372 0.01130558 0.00067942]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 109ms/step\n", "[[4.4243312e-01 5.5234385e-01 4.9254801e-03 2.9754813e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[4.3317986e-01 5.6243598e-01 4.1413051e-03 2.4284663e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 115ms/step\n", "[[0.52935624 0.46102938 0.00886495 0.00074939]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[4.8261899e-01 5.1293612e-01 4.1731801e-03 2.7175286e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 116ms/step\n", "[[4.4055092e-01 5.5352402e-01 5.6604608e-03 2.6452570e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 110ms/step\n", "[[4.2110297e-01 5.7347727e-01 5.1973891e-03 2.2236633e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 114ms/step\n", "[[3.9519185e-01 5.9414124e-01 1.0202706e-02 4.6415642e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.52715486 0.42906848 0.04193437 0.00184232]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 121ms/step\n", "[[0.4206979 0.55904144 0.01885335 0.00140733]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 103ms/step\n", "[[0.544774 0.14541742 0.25587305 0.05393547]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.4370813 0.49844724 0.06200609 0.00246527]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 119ms/step\n", "[[0.4528224 0.52911925 0.01663947 0.00141889]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 96ms/step\n", "[[0.3819743 0.5466778 0.06750397 0.00384389]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 113ms/step\n", "[[0.40970472 0.5569316 0.03105194 0.00231172]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 94ms/step\n", "[[0.6114612 0.19729745 0.1383356 0.0529057 ]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 135ms/step\n", "[[0.45113012 0.50088024 0.04475248 0.00323708]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 108ms/step\n", "[[0.45435527 0.53013545 0.01427606 0.00123324]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 106ms/step\n", "[[0.47971782 0.5001755 0.01883741 0.00126928]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 99ms/step\n", "[[0.54685706 0.43772423 0.01460937 0.00080937]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 119ms/step\n", "[[0.43435532 0.51326823 0.04905771 0.00331874]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step\n", "[[0.4691491 0.51138616 0.01782178 0.00164296]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 132ms/step\n", "[[0.36563382 0.609435 0.02245951 0.00247164]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 93ms/step\n", "[[0.4674584 0.51042354 0.0199133 0.00220476]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 134ms/step\n", "[[0.35535875 0.62192243 0.02078114 0.0019377 ]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 118ms/step\n", "[[0.35675234 0.5971697 0.04377323 0.00230471]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 108ms/step\n", "[[0.32445276 0.6365426 0.03643773 0.00256694]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 118ms/step\n", "[[0.44510007 0.5342694 0.01953486 0.00109567]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 100ms/step\n", "[[0.30969 0.60378057 0.07928306 0.00724639]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step\n", "[[0.56752884 0.35713732 0.07257319 0.00276066]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 120ms/step\n", "[[0.34694657 0.5160776 0.13258497 0.00439083]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 111ms/step\n", "[[0.4539524 0.46011406 0.08372305 0.00221045]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 105ms/step\n", "[[5.1272041e-01 4.3961889e-01 4.7271658e-02 3.8900448e-04]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 115ms/step\n", "[[0.67923796 0.2438758 0.07580444 0.00108182]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 108ms/step\n", "[[0.5821022 0.27528477 0.13952196 0.00309112]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 115ms/step\n", "[[0.4618073 0.35787314 0.16838866 0.01193077]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 100ms/step\n", "[[0.5675023 0.3495956 0.07748043 0.0054217 ]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 126ms/step\n", "[[0.45714724 0.42329356 0.11500251 0.00455673]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[0.5857304 0.38192827 0.0309583 0.00138313]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 116ms/step\n", "[[0.5372635 0.43943352 0.02258803 0.00071496]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 106ms/step\n", "[[0.5059755 0.46730185 0.02545177 0.00127086]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 97ms/step\n", "[[0.5287636 0.436619 0.03319615 0.00142124]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 122ms/step\n", "[[0.5353085 0.3997285 0.0617837 0.0031793]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 100ms/step\n", "[[0.50711626 0.45882738 0.03308126 0.00097504]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 125ms/step\n", "[[0.47909027 0.4863685 0.03269366 0.00184744]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step\n", "[[0.40050444 0.5718929 0.02541785 0.00218476]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 101ms/step\n", "[[0.3778685 0.5632252 0.05708069 0.00182558]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 150ms/step\n", "[[0.57108206 0.40179658 0.02321302 0.00390833]]\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 109ms/step\n", "[[0.53513265 0.41831055 0.0419606 0.00459616]]\n" ] } ], "source": [ "import cv2 as cv\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "classes = ['no_yawn', 'yawn', 'Closed', 'Open'] # Class labels\n", "IMG_SIZE = 145 # Image size for resizing\n", "face_cascade = cv.CascadeClassifier(\"C:/Users/rohit/Downloads/haarcascade_frontalface_default.xml\")\n", "\n", "# Prepare the frame for prediction\n", "def prepare_frame(frame):\n", " gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Convert to grayscale for better detection\n", " faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5)\n", " face_list = []\n", " coords = []\n", " \n", " if len(faces) > 0:\n", " # Select the face with the maximum area\n", " f = max(faces, key=lambda x: x[2] * x[3])\n", " x, y, w, h = f\n", " face = frame[y:y+h, x:x+w] # Crop the detected face\n", " resized_face = cv.resize(face, (IMG_SIZE, IMG_SIZE)) # Resize to match model input\n", " resized_face = resized_face / 255.0 # Normalize\n", " face_list.append(resized_face.reshape(-1, IMG_SIZE, IMG_SIZE, 3))\n", " coords.append((x, y, w, h))\n", " \n", " return face_list, coords\n", "\n", "# Start video capture\n", "cap = cv.VideoCapture(0)\n", "\n", "while True:\n", " ret, frame = cap.read()\n", " if not ret:\n", " break\n", "\n", " frame = cv.flip(frame, 1) # Flip frame horizontally\n", " processed_frames, coords = prepare_frame(frame)\n", " \n", " if processed_frames:\n", " for i, processed_frame in enumerate(processed_frames):\n", " prediction = model.predict(processed_frame) \n", " print(prediction)\n", " predicted_class = np.argmax(prediction) \n", " class_label = classes[predicted_class]\n", " x, y, w, h = coords[i]\n", "\n", " # Draw rectangle around the face\n", " cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)\n", " \n", " # Display predicted label on frame\n", " cv.putText(frame, f'{class_label}', (x, y-10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n", "\n", " # Smoothen video quality by applying a Gaussian blur\n", " frame = cv.GaussianBlur(frame, (5, 5), 0)\n", "\n", " cv.imshow('Real-time Emotion Detection', frame)\n", "\n", " # Press 'q' to quit\n", " if cv.waitKey(1) & 0xFF == ord('q'):\n", " break\n", "\n", "cap.release()\n", "cv.destroyAllWindows()\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "cap.release()\n", "cv.destroyAllWindows()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For Eye open or close" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pygame 2.6.1 (SDL 2.28.4, Python 3.11.4)\n", "Hello from the pygame community. https://www.pygame.org/contribute.html\n" ] } ], "source": [ "import cv2 as cv\n", "import numpy as np\n", "import time\n", "import pygame\n", "\n", "classes = ['Closed', 'Open'] # Class labels for eyes only\n", "\n", "# Load face and eye cascade classifiers\n", "face_cascade = cv.CascadeClassifier(\"C:/Users/rohit/Downloads/haarcascade_frontalface_default.xml\")\n", "eye_cascade = cv.CascadeClassifier(\"C:/Users/rohit/Downloads/haarcascade_eye.xml\")\n", "\n", "# Initialize alarm\n", "pygame.mixer.init()\n", "pygame.mixer.music.load(\"research/alarm.wav\")\n", "\n", "# Track eye closure duration\n", "closed_start_time = None\n", "alarm_triggered = False\n", "\n", "# Prepare the frame for eye detection\n", "def prepare_frame(frame):\n", " global closed_start_time, alarm_triggered\n", " gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Convert to grayscale for better detection\n", " faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5)\n", " eye_status = 'Closed'\n", " \n", " for (x, y, w, h) in faces:\n", " face_roi = frame[y:y+h, x:x+w]\n", " roi_gray = gray_frame[y:y+h, x:x+w]\n", " eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=1.1, minNeighbors=5)\n", " \n", " # If two or more eyes detected, classify as 'Open'\n", " if len(eyes) >= 2:\n", " eye_status = 'Open'\n", " if alarm_triggered:\n", " pygame.mixer.music.stop()\n", " alarm_triggered = False\n", " closed_start_time = None\n", " else:\n", " if closed_start_time is None:\n", " closed_start_time = time.time()\n", " elif time.time() - closed_start_time >= 3:\n", " if not alarm_triggered:\n", " pygame.mixer.music.play()\n", " alarm_triggered = True\n", " \n", " # Draw a rectangle around the entire face\n", " cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)\n", " break # Stop after detecting the first face to avoid multiple rectangles\n", "\n", " cv.putText(frame, eye_status, (20, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n", " return eye_status\n", "\n", "# Start video capture\n", "cap = cv.VideoCapture(0)\n", "\n", "while True:\n", " ret, frame = cap.read()\n", " if not ret:\n", " break\n", "\n", " frame = cv.flip(frame, 1) # Flip frame horizontally\n", " eye_status = prepare_frame(frame)\n", "\n", " # Display the frame with eye detection\n", " cv.imshow('Real-time Eye Detection', frame)\n", "\n", " # Press 'q' to quit\n", " if cv.waitKey(1) & 0xFF == ord('q'):\n", " break\n", "\n", "cap.release()\n", "cv.destroyAllWindows()\n" ] } ], "metadata": { "kernelspec": { "display_name": "chatbot", "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.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }