<jupyter_start><jupyter_text><jupyter_code>import cv2,os,random
import numpy as np

def ImagePreprocess(path2img,train_size=0.7,size=(80,80)):
    x_test=[]
    y_test=[[]]
    x_train=[]
    y_train=[[]]
    for path in path2img:
        numberoffiles=len(os.listdir(path))
        counter=0
        for img in os.listdir(path):
            if counter<(numberoffiles*train_size):
                pic = cv2.cvtColor(cv2.imread(os.path.join(path,img)),cv2.COLOR_BGR2RGB)
                pic = cv2.resize(pic,size)
                pic =pic.reshape((size[0],size[1],3))
                x_train.append([pic])
                y_train[0].append(path2img.index(path))
                counter+=1
            else:
                pic = cv2.cvtColor(cv2.imread(os.path.join(path,img)),cv2.COLOR_BGR2RGB)
                pic = cv2.resize(pic,size)
                pic =pic.reshape((size[0],size[1],3))
                x_test.append([pic])
                y_test[0].append(path2img.index(path))
                counter+=1

    temp0=list(zip(x_train,y_train[0]))
    random.shuffle(temp0)
    x_train,y_train[0]=zip(*temp0)
    temp1=list(zip(x_test,y_test[0]))
    random.shuffle(temp1)
    x_test,y_test[0]=zip(*temp1)

    x_train=np.array(x_train)
    y_train=np.array(y_train)
    x_test=np.array(x_test)
    y_test=np.array(y_test)

    x_train=x_train/255.
    x_test=x_test/255.

    return x_train,x_test,y_train,y_test
from google.colab import files
#Uploading kaggle.json file that has API Key
files.upload()
!pip install -q kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!kaggle datasets download -d ashishjangra27/face-mask-12k-images-dataset
!chmod 600 /root/.kaggle/kaggle.json
!unzip \*.zip  && rm *.zip
path=["/content/Face Mask Dataset/Train/WithoutMask","/content/Face Mask Dataset/Train/WithMask"]
x_train,x_test,y_train,y_test=ImagePreprocess(path)
from keras.utils import to_categorical
encoded_train=to_categorical(y_train)
encoded_test=to_categorical(y_test)
from tensorflow.keras import models
from tensorflow.keras.layers import Dense,Flatten,Input,Conv2D,MaxPooling2D,Dropout
import tensorflow as tf
x_train=np.reshape(x_train,(7000,80,80,3))
x_test=np.reshape(x_test,(3000,80,80,3))
cnn=models.Sequential(
    [
     Conv2D(filters=64,kernel_size=(5,5),activation="relu",input_shape=(80,80,3)),
     Dropout(0.2),
     MaxPooling2D((3,3)),
     Conv2D(filters=32,kernel_size=(5,5),activation="relu"),
     Dropout(0.2),
     MaxPooling2D((3,3)),
     Conv2D(filters=32,kernel_size=(5,5),activation="relu"),
     Dropout(0.2),
     MaxPooling2D((3,3)),
     Flatten(),
     Dense(64,"relu"),
     Dense(2,"sigmoid")
    ]
)
cnn.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"])
cnn.fit(x_train,encoded_train[0],epochs=5,batch_size=128)
accuracy,loss=cnn.evaluate(x_test,encoded_test[0],batch_size=128)<jupyter_output>24/24 [==============================] - 8s 340ms/step - loss: 0.0744 - accuracy: 0.9867
{'fraction_non_alphanumeric': 0.1613121406831248, 'fraction_numerical': 0.047345282380791345, 'mean_word_length': 3.7479935794542536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '327083', 'license': 'MIT', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1143, 'n_words': 139, 'path': 'Covid_Mask_Detection_Model.ipynb', 'repo_name': 'Xessen/Mask_Detection', 'size': '2957'}