Commit 0c1a426e authored by Wandana RAK - IT18227550's avatar Wandana RAK - IT18227550

Merge branch 'it18227550' into 'develop'

It18227550

See merge request !17
parents eac15994 497dbf00
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport os\n\n# Importing Deep Learning Libraries\n\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense,Input,Dropout,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Activation,MaxPooling2D\nfrom keras.models import Model,Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-13T18:08:28.381737Z","iopub.execute_input":"2021-12-13T18:08:28.382443Z","iopub.status.idle":"2021-12-13T18:08:33.857642Z","shell.execute_reply.started":"2021-12-13T18:08:28.382349Z","shell.execute_reply":"2021-12-13T18:08:33.856887Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam,SGD,RMSprop","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:09:40.507653Z","iopub.execute_input":"2021-12-13T18:09:40.508367Z","iopub.status.idle":"2021-12-13T18:09:40.833605Z","shell.execute_reply.started":"2021-12-13T18:09:40.508325Z","shell.execute_reply":"2021-12-13T18:09:40.832816Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"picture_size = 48\nfolder_path = \"../input/face-expression-recognition-dataset/images/\"","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:09:48.028275Z","iopub.execute_input":"2021-12-13T18:09:48.028919Z","iopub.status.idle":"2021-12-13T18:09:48.032993Z","shell.execute_reply.started":"2021-12-13T18:09:48.028877Z","shell.execute_reply":"2021-12-13T18:09:48.032153Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"expression = 'disgust'\n\nplt.figure(figsize= (12,12))\nfor i in range(1, 10, 1):\n plt.subplot(3,3,i)\n img = load_img(folder_path+\"train/\"+expression+\"/\"+\n os.listdir(folder_path + \"train/\" + expression)[i], target_size=(picture_size, picture_size))\n plt.imshow(img) \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:09:54.101252Z","iopub.execute_input":"2021-12-13T18:09:54.101864Z","iopub.status.idle":"2021-12-13T18:09:55.192017Z","shell.execute_reply.started":"2021-12-13T18:09:54.101806Z","shell.execute_reply":"2021-12-13T18:09:55.191333Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\n\ndatagen_train = ImageDataGenerator()\ndatagen_val = ImageDataGenerator()\n\ntrain_set = datagen_train.flow_from_directory(folder_path+\"train\",\n target_size = (picture_size,picture_size),\n color_mode = \"grayscale\",\n batch_size=batch_size,\n class_mode='categorical',\n shuffle=True)\n\n\ntest_set = datagen_val.flow_from_directory(folder_path+\"validation\",\n target_size = (picture_size,picture_size),\n color_mode = \"grayscale\",\n batch_size=batch_size,\n class_mode='categorical',\n shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:10:12.146756Z","iopub.execute_input":"2021-12-13T18:10:12.147500Z","iopub.status.idle":"2021-12-13T18:10:34.791611Z","shell.execute_reply.started":"2021-12-13T18:10:12.147459Z","shell.execute_reply":"2021-12-13T18:10:34.790904Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"markdown","source":"Model Building","metadata":{}},{"cell_type":"code","source":"no_of_classes = 7\n\nmodel = Sequential()\n\n#1st CNN layer\nmodel.add(Conv2D(64,(3,3),padding = 'same',input_shape = (48,48,1)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.25))\n\n#2nd CNN layer\nmodel.add(Conv2D(128,(5,5),padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout (0.25))\n\n#3rd CNN layer\nmodel.add(Conv2D(512,(3,3),padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout (0.25))\n\n#4th CNN layer\nmodel.add(Conv2D(512,(3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\n\n#Fully connected 1st layer\nmodel.add(Dense(256))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\n\n\n# Fully connected layer 2nd layer\nmodel.add(Dense(512))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(no_of_classes, activation='softmax'))\n\n\n\nopt = Adam(lr = 0.0001)\nmodel.compile(optimizer=opt,loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:51:47.491726Z","iopub.execute_input":"2021-12-13T18:51:47.491990Z","iopub.status.idle":"2021-12-13T18:51:47.672808Z","shell.execute_reply.started":"2021-12-13T18:51:47.491955Z","shell.execute_reply":"2021-12-13T18:51:47.672157Z"},"trusted":true},"execution_count":17,"outputs":[]},{"cell_type":"markdown","source":"Fitting the Model with Training and Validation Data","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n\ncheckpoint = ModelCheckpoint(\"./model.h5\", monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n\nearly_stopping = EarlyStopping(monitor='val_loss',\n min_delta=0,\n patience=3,\n verbose=1,\n restore_best_weights=True\n )\n\nreduce_learningrate = ReduceLROnPlateau(monitor='val_loss',\n factor=0.2,\n patience=3,\n verbose=1,\n min_delta=0.0001)\n\ncallbacks_list = [early_stopping,checkpoint,reduce_learningrate]\n\nepochs = 48\n\nmodel.compile(loss='categorical_crossentropy',\n optimizer = Adam(lr=0.001),\n metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:52:04.835357Z","iopub.execute_input":"2021-12-13T18:52:04.835618Z","iopub.status.idle":"2021-12-13T18:52:04.848952Z","shell.execute_reply.started":"2021-12-13T18:52:04.835588Z","shell.execute_reply":"2021-12-13T18:52:04.848233Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(generator=train_set,\n steps_per_epoch=train_set.n//train_set.batch_size,\n epochs=epochs,\n validation_data = test_set,\n validation_steps = test_set.n//test_set.batch_size,\n callbacks=callbacks_list\n )","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:52:09.759448Z","iopub.execute_input":"2021-12-13T18:52:09.759706Z","iopub.status.idle":"2021-12-13T18:57:55.374441Z","shell.execute_reply.started":"2021-12-13T18:52:09.759670Z","shell.execute_reply":"2021-12-13T18:57:55.373647Z"},"trusted":true},"execution_count":19,"outputs":[]},{"cell_type":"code","source":"plt.style.use('dark_background')\n\nplt.figure(figsize=(20,10))\nplt.subplot(1, 2, 1)\nplt.suptitle('Optimizer : Adam', fontsize=10)\nplt.ylabel('Loss', fontsize=16)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend(loc='upper right')\n\nplt.subplot(1, 2, 2)\nplt.ylabel('Accuracy', fontsize=16)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:59:06.255454Z","iopub.execute_input":"2021-12-13T18:59:06.255714Z","iopub.status.idle":"2021-12-13T18:59:06.582348Z","shell.execute_reply.started":"2021-12-13T18:59:06.255683Z","shell.execute_reply":"2021-12-13T18:59:06.581699Z"},"trusted":true},"execution_count":21,"outputs":[]}]}
\ No newline at end of file
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:29:42.213352Z\",\"iopub.execute_input\":\"2022-01-02T18:29:42.213653Z\",\"iopub.status.idle\":\"2022-01-02T18:29:47.460037Z\",\"shell.execute_reply.started\":\"2022-01-02T18:29:42.213574Z\",\"shell.execute_reply\":\"2022-01-02T18:29:47.459214Z\"}}\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport os\n\n# Importing Deep Learning Libraries\n\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense,Input,Dropout,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Activation,MaxPooling2D\nfrom keras.models import Model,Sequential\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:31:26.874049Z\",\"iopub.execute_input\":\"2022-01-02T18:31:26.87433Z\",\"iopub.status.idle\":\"2022-01-02T18:31:27.174872Z\",\"shell.execute_reply.started\":\"2022-01-02T18:31:26.874299Z\",\"shell.execute_reply\":\"2022-01-02T18:31:27.17414Z\"}}\nfrom tensorflow.keras.optimizers import Adam,SGD,RMSprop\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:31:32.493872Z\",\"iopub.execute_input\":\"2022-01-02T18:31:32.494434Z\",\"iopub.status.idle\":\"2022-01-02T18:31:32.498665Z\",\"shell.execute_reply.started\":\"2022-01-02T18:31:32.494395Z\",\"shell.execute_reply\":\"2022-01-02T18:31:32.497919Z\"}}\npicture_size = 48\nfolder_path = \"../input/face-expression-recognition-dataset/images/\"\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:34:59.744327Z\",\"iopub.execute_input\":\"2022-01-02T18:34:59.744602Z\",\"iopub.status.idle\":\"2022-01-02T18:35:00.78588Z\",\"shell.execute_reply.started\":\"2022-01-02T18:34:59.744566Z\",\"shell.execute_reply\":\"2022-01-02T18:35:00.783248Z\"}}\nexpression = 'disgust'\n\nplt.figure(figsize= (12,12))\nfor i in range(1, 10, 1):\n plt.subplot(3,3,i)\n img = load_img(folder_path+\"train/\"+expression+\"/\"+\n os.listdir(folder_path + \"train/\" + expression)[i], target_size=(picture_size, picture_size))\n plt.imshow(img) \nplt.show()\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:35:05.654001Z\",\"iopub.execute_input\":\"2022-01-02T18:35:05.65435Z\",\"iopub.status.idle\":\"2022-01-02T18:35:21.720425Z\",\"shell.execute_reply.started\":\"2022-01-02T18:35:05.654313Z\",\"shell.execute_reply\":\"2022-01-02T18:35:21.719017Z\"}}\nbatch_size = 128\n\ndatagen_train = ImageDataGenerator()\ndatagen_val = ImageDataGenerator()\n\ntrain_set = datagen_train.flow_from_directory(folder_path+\"train\",\n target_size = (picture_size,picture_size),\n color_mode = \"grayscale\",\n batch_size=batch_size,\n class_mode='categorical',\n shuffle=True)\n\n\ntest_set = datagen_val.flow_from_directory(folder_path+\"validation\",\n target_size = (picture_size,picture_size),\n color_mode = \"grayscale\",\n batch_size=batch_size,\n class_mode='categorical',\n shuffle=False)\n\n# %% [markdown]\n# Model Building\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:35:59.134798Z\",\"iopub.execute_input\":\"2022-01-02T18:35:59.135349Z\",\"iopub.status.idle\":\"2022-01-02T18:36:01.780141Z\",\"shell.execute_reply.started\":\"2022-01-02T18:35:59.135309Z\",\"shell.execute_reply\":\"2022-01-02T18:36:01.77949Z\"}}\nno_of_classes = 7\n\nmodel = Sequential()\n\n#1st CNN layer\nmodel.add(Conv2D(64,(3,3),padding = 'same',input_shape = (48,48,1)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.25))\n\n#2nd CNN layer\nmodel.add(Conv2D(128,(5,5),padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout (0.25))\n\n#3rd CNN layer\nmodel.add(Conv2D(512,(3,3),padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout (0.25))\n\n#4th CNN layer\nmodel.add(Conv2D(512,(3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\n\n#Fully connected 1st layer\nmodel.add(Dense(256))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\n\n\n# Fully connected layer 2nd layer\nmodel.add(Dense(512))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(no_of_classes, activation='softmax'))\n\n\n\nopt = Adam(lr = 0.0001)\nmodel.compile(optimizer=opt,loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()\n\n# %% [markdown]\n# Fitting the Model with Training and Validation Data\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:36:45.963735Z\",\"iopub.execute_input\":\"2022-01-02T18:36:45.963996Z\",\"iopub.status.idle\":\"2022-01-02T18:36:45.978165Z\",\"shell.execute_reply.started\":\"2022-01-02T18:36:45.963967Z\",\"shell.execute_reply\":\"2022-01-02T18:36:45.977455Z\"}}\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n\ncheckpoint = ModelCheckpoint(\"./model.h5\", monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n\nearly_stopping = EarlyStopping(monitor='val_loss',\n min_delta=0,\n patience=3,\n verbose=1,\n restore_best_weights=True\n )\n\nreduce_learningrate = ReduceLROnPlateau(monitor='val_loss',\n factor=0.2,\n patience=3,\n verbose=1,\n min_delta=0.0001)\n\ncallbacks_list = [early_stopping,checkpoint,reduce_learningrate]\n\nepochs = 48\n\nmodel.compile(loss='categorical_crossentropy',\n optimizer = Adam(lr=0.001),\n metrics=['accuracy'])\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:37:22.724599Z\",\"iopub.execute_input\":\"2022-01-02T18:37:22.724884Z\",\"iopub.status.idle\":\"2022-01-02T18:47:27.839479Z\",\"shell.execute_reply.started\":\"2022-01-02T18:37:22.724853Z\",\"shell.execute_reply\":\"2022-01-02T18:47:27.838695Z\"}}\nhistory = model.fit_generator(generator=train_set,\n steps_per_epoch=train_set.n//train_set.batch_size,\n epochs=epochs,\n validation_data = test_set,\n validation_steps = test_set.n//test_set.batch_size,\n callbacks=callbacks_list\n )\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T18:52:03.305144Z\",\"iopub.execute_input\":\"2022-01-02T18:52:03.305869Z\",\"iopub.status.idle\":\"2022-01-02T18:52:27.411667Z\",\"shell.execute_reply.started\":\"2022-01-02T18:52:03.305831Z\",\"shell.execute_reply\":\"2022-01-02T18:52:27.410996Z\"}}\nplt.style.use('dark_background')\n\nplt.figure(figsize=(20,10))\nplt.subplot(1, 2, 1)\nplt.suptitle('Optimizer : Adam', fontsize=10)\nplt.ylabel('Loss', fontsize=16)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend(loc='upper right')\n\nplt.subplot(1, 2, 2)\nplt.ylabel('Accuracy', fontsize=16)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.show()\n\n# %% [code] {\"execution\":{\"iopub.status.busy\":\"2022-01-02T19:08:54.788074Z\",\"iopub.execute_input\":\"2022-01-02T19:08:54.788346Z\",\"iopub.status.idle\":\"2022-01-02T19:08:54.950179Z\",\"shell.execute_reply.started\":\"2022-01-02T19:08:54.788317Z\",\"shell.execute_reply\":\"2022-01-02T19:08:54.949443Z\"}}\nmodel.save('/kaggle/working/final_model.h5')","metadata":{"_uuid":"f7dbf1bc-5f64-4f46-a2c9-0831213bb3b1","_cell_guid":"80c6e78c-ee66-49d7-bae1-88859794c451","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}
\ No newline at end of file
This diff is collapsed.
from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np
face_classifier = cv2.CascadeClassifier(r'F:\SLIIT\Research Project\New folder\Emotion Classification\haarcascade_frontalface_default.xml')
classifier =load_model(r'F:\SLIIT\Research Project\New folder\Emotion Classification\model.h5')
emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
labels = []
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
roi_gray = gray[y:y+h,x:x+w]
roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
if np.sum([roi_gray])!=0:
roi = roi_gray.astype('float')/255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi,axis=0)
prediction = classifier.predict(roi)[0]
label=emotion_labels[prediction.argmax()]
label_position = (x,y-10)
cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
else:
cv2.putText(frame,'No Faces',(30,80),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
cv2.imshow('Emotion Detector',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment