Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEAM AVENGERS #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added TEAM AVENGERS/Team Avengers.pptx
Binary file not shown.
Binary file added TEAM AVENGERS/Team Avengers.txt.docx
Binary file not shown.
370 changes: 370 additions & 0 deletions TEAM AVENGERS/mask detection final (1).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'with mask': 0, 'without mask': 1}\n",
"['with mask', 'without mask']\n",
"[0, 1]\n"
]
}
],
"source": [
"import cv2,os\n",
"\n",
"data_path='D:/face mask detection/dataset'\n",
"categories=os.listdir(data_path)\n",
"labels=[i for i in range(len(categories))]\n",
"\n",
"label_dict=dict(zip(categories,labels))\n",
"\n",
"print(label_dict)\n",
"print(categories)\n",
"print(labels)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"img_size=100\n",
"data=[]\n",
"target=[]\n",
"\n",
"\n",
"for category in categories:\n",
" folder_path=os.path.join(data_path,category)\n",
" img_names=os.listdir(folder_path)\n",
" \n",
" for img_name in img_names:\n",
" img_path=os.path.join(folder_path,img_name)\n",
" img=cv2.imread(img_path)\n",
"\n",
" try:\n",
" gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) \n",
" #Coverting the image into gray scale\n",
" resized=cv2.resize(gray,(img_size,img_size))\n",
" #resizing the gray scale into 100x100, since we need a fixed common size for all the images in the dataset\n",
" data.append(resized)\n",
" target.append(label_dict[category])\n",
" #appending the image and the label(categorized) into the list (dataset)\n",
"\n",
" except Exception as e:\n",
" print('Exception:',e)\n",
" #if any exception rasied, the exception will be printed here. And pass to the next image"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"data=np.array(data)/255.0\n",
"data=np.reshape(data,(data.shape[0],img_size,img_size,1))\n",
"target=np.array(target)\n",
"\n",
"from keras.utils import np_utils\n",
"\n",
"new_target=np_utils.to_categorical(target)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"np.save('data',data)\n",
"np.save('target',new_target)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"data=np.load('data.npy')\n",
"target=np.load('target.npy')\n",
"\n",
"#loading the save numpy arrays in the previous code"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3376: The name tf.log is deprecated. Please use tf.math.log instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n"
]
}
],
"source": [
"from keras.models import Sequential\n",
"from keras.layers import Dense,Activation,Flatten,Dropout\n",
"from keras.layers import Conv2D,MaxPooling2D\n",
"from keras.callbacks import ModelCheckpoint\n",
"\n",
"model=Sequential()\n",
"\n",
"model.add(Conv2D(100,(3,3),input_shape=data.shape[1:]))\n",
"model.add(Activation('relu'))\n",
"model.add(MaxPooling2D(pool_size=(2,2)))\n",
"#The first CNN layer followed by Relu and MaxPooling layers\n",
"\n",
"model.add(Conv2D(100,(3,3)))\n",
"model.add(Activation('relu'))\n",
"model.add(MaxPooling2D(pool_size=(2,2)))\n",
"#The second convolution layer followed by Relu and MaxPooling layers\n",
"\n",
"model.add(Flatten())\n",
"model.add(Dropout(0.5))\n",
"#Flatten layer to stack the output convolutions from second convolution layer\n",
"model.add(Dense(50,activation='relu'))\n",
"#Dense layer of 64 neurons\n",
"model.add(Dense(2,activation='softmax'))\n",
"#The Final layer with two outputs for two categories\n",
"\n",
"model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"train_data,test_data,train_target,test_target=train_test_split(data,target,test_size=0.1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 990 samples, validate on 248 samples\n",
"Epoch 1/20\n",
"990/990 [==============================] - 50s 50ms/step - loss: 0.7056 - acc: 0.5030 - val_loss: 0.6675 - val_acc: 0.5403\n",
"Epoch 2/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.5983 - acc: 0.6707 - val_loss: 0.6071 - val_acc: 0.6290\n",
"Epoch 3/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.4896 - acc: 0.7505 - val_loss: 0.3372 - val_acc: 0.8750\n",
"Epoch 4/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.2950 - acc: 0.8899 - val_loss: 0.2297 - val_acc: 0.9073\n",
"Epoch 5/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.2045 - acc: 0.9232 - val_loss: 0.1656 - val_acc: 0.9395\n",
"Epoch 6/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.1493 - acc: 0.9485 - val_loss: 0.1352 - val_acc: 0.9637\n",
"Epoch 7/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.1162 - acc: 0.9657 - val_loss: 0.1308 - val_acc: 0.9637\n",
"Epoch 8/20\n",
"990/990 [==============================] - 50s 50ms/step - loss: 0.0882 - acc: 0.9687 - val_loss: 0.1061 - val_acc: 0.9677\n",
"Epoch 9/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0564 - acc: 0.9879 - val_loss: 0.1027 - val_acc: 0.9677\n",
"Epoch 10/20\n",
"990/990 [==============================] - 50s 50ms/step - loss: 0.0464 - acc: 0.9848 - val_loss: 0.0861 - val_acc: 0.9718\n",
"Epoch 11/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0453 - acc: 0.9818 - val_loss: 0.1713 - val_acc: 0.9395\n",
"Epoch 12/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0391 - acc: 0.9838 - val_loss: 0.0742 - val_acc: 0.9798\n",
"Epoch 13/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0388 - acc: 0.9889 - val_loss: 0.0797 - val_acc: 0.9758\n",
"Epoch 14/20\n",
"990/990 [==============================] - 50s 50ms/step - loss: 0.0303 - acc: 0.9909 - val_loss: 0.0828 - val_acc: 0.9798\n",
"Epoch 15/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0173 - acc: 0.9960 - val_loss: 0.0945 - val_acc: 0.9637\n",
"Epoch 16/20\n",
"990/990 [==============================] - 49s 49ms/step - loss: 0.0353 - acc: 0.9909 - val_loss: 0.0663 - val_acc: 0.9798\n",
"Epoch 17/20\n",
"990/990 [==============================] - 49s 49ms/step - loss: 0.0376 - acc: 0.9869 - val_loss: 0.1402 - val_acc: 0.9355\n",
"Epoch 18/20\n",
"990/990 [==============================] - 49s 49ms/step - loss: 0.0452 - acc: 0.9808 - val_loss: 0.1066 - val_acc: 0.9597\n",
"Epoch 19/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0123 - acc: 0.9980 - val_loss: 0.0814 - val_acc: 0.9718\n",
"Epoch 20/20\n",
"990/990 [==============================] - 49s 50ms/step - loss: 0.0277 - acc: 0.9889 - val_loss: 0.0722 - val_acc: 0.9758\n"
]
}
],
"source": [
"checkpoint = ModelCheckpoint('model-{epoch:03d}.model',monitor='val_loss',verbose=0,save_best_only=True,mode='auto')\n",
"history=model.fit(train_data,train_target,epochs=20,callbacks=[checkpoint],validation_split=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import keras\n",
"from keras.models import load_model\n",
"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:131: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
"WARNING:tensorflow:From C:\\Users\\User\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.\n",
"\n"
]
}
],
"source": [
" model = load_model('model-017.model', compile=False)\n",
"\n",
"\n",
"face_clsfr=cv2.CascadeClassifier('D:/PyPower/Project1/PyPower_face-detection/haarcascade_frontalface_default.xml')\n",
"\n",
"source=cv2.VideoCapture(0)\n",
"\n",
"labels_dict={0:'with_mask',1:'without_mask'}\n",
"color_dict={0:(0,255,0),1:(0,0,255)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while(True):\n",
"\n",
" ret,img=source.read()\n",
" gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
" faces=face_clsfr.detectMultiScale(gray,1.3,5) \n",
"\n",
" for x,y,w,h in faces:\n",
" \n",
" face_img=gray[y:y+w,x:x+w]\n",
" resized=cv2.resize(face_img,(100,100))\n",
" normalized=resized/255.0\n",
" reshaped=np.reshape(normalized,(1,100,100,1))\n",
" result=model.predict(reshaped)\n",
"\n",
" label=np.argmax(result,axis=1)[0]\n",
" \n",
" cv2.rectangle(img,(x,y),(x+w,y+h),color_dict[label],2)\n",
" cv2.rectangle(img,(x,y-40),(x+w,y),color_dict[label],-1)\n",
" cv2.putText(img, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)\n",
" \n",
" \n",
" cv2.imshow('LIVE',img)\n",
" key=cv2.waitKey(1)\n",
" \n",
" if(key==27):\n",
" break\n",
" \n",
"cv2.destroyAllWindows()\n",
"source.release()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added Team Avengers.pptx
Binary file not shown.
Binary file added Team Avengers.txt.docx
Binary file not shown.
Loading