# -*- coding: utf-8 -*-
"""
Created on Wed Jul 22 15:56:47 2020

@author: Karan
"""


import numpy as np
import keras
import keras.backend as k
from keras.layers import Conv2D,MaxPooling2D,SpatialDropout2D,Flatten,Dropout,Dense
from keras.models import Sequential,load_model
from tensorflow.keras.optimizers import Adam
from keras.preprocessing import image
import cv2
import datetime
import face_recognition

# UNCOMMENT THE FOLLOWING CODE TO TRAIN THE CNN FROM SCRATCH
knowning={"media/biden.jpg":"<NAME>","media/om.jpg":"OM","media/tabish.jpg":"<NAME>adkar","media/zahid.jpg":"<NAME>"}
# BUILDING MODEL TO CLASSIFY BETWEEN MASK AND NO MASK

model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))
model.add(MaxPooling2D() )
model.add(Conv2D(32,(3,3),activation='relu'))
model.add(MaxPooling2D() )
model.add(Conv2D(32,(3,3),activation='relu'))
model.add(MaxPooling2D() )
model.add(Flatten())
model.add(Dense(100,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('train',target_size=(150,150),batch_size=16 ,class_mode='binary')
test_set = test_datagen.flow_from_directory('test',	target_size=(150,150), batch_size=16, class_mode='binary')
model_saved=model.fit_generator(training_set,epochs=10,validation_data=test_set,)
model.save('mymodel.h5',model_saved)
#To test for individual images
mymodel=load_model('mymodel.h5')
#test_image=image.load_img('C:/Users/<NAME>/Desktop/1/Dataset/test/without_mask/30.jpg',target_size=(150,150,3))
test_image=image.load_img(r'F:/1/test/with_mask/1-with-mask.jpg',target_size=(150,150,3))
test_image
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=0)
mymodel.predict(test_image)[0][0]
# IMPLEMENTING LIVE DETECTION OF FACE MASK
mymodel=load_model('mymodel.h5')
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while cap.isOpened():
	_,img=cap.read()
	face=face_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=4)
	for(x,y,w,h) in face:
		face_img = img[y:y+h, x:x+w]
		cv2.imwrite('temp.jpg',face_img)
		test_image=image.load_img('temp.jpg',target_size=(150,150,3))
		test_image=image.img_to_array(test_image)
		test_image=np.expand_dims(test_image,axis=0)
		pred=mymodel.predict(test_image)[0][0]
		if pred==1:
			cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
			cv2.putText(img,'NO MASK',((x+w)//2,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3)
		else:
			cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
			cv2.putText(img,'MASK',((x+w)//2,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),3)
		datet=str(datetime.datetime.now())
		cv2.putText(img,datet,(400,450),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
		if pred==1:
			for key, value in knowning.items():
				known_image = face_recognition.load_image_file(key)
				unknown_image = face_recognition.load_image_file("temp.jpg")
				biden_encoding = face_recognition.face_encodings(known_image)[0]
				unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
				results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
				if results==[True]:
					print(value+" has not wore masked properly")
	cv2.imshow('img',img)
	if cv2.waitKey(1)==ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 22 15:56:47 2020

@author: Karan
"""


import numpy as np
import keras
import keras.backend as k
from keras.layers import Conv2D,MaxPooling2D,SpatialDropout2D,Flatten,Dropout,Dense
from keras.models import Sequential,load_model
from tensorflow.keras.optimizers import Adam
from keras.preprocessing import image
import cv2
import datetime
import face_recognition

# UNCOMMENT THE FOLLOWING CODE TO TRAIN THE CNN FROM SCRATCH
knowning={"media/biden.jpg":"<NAME>","media/om.jpg":"OM","media/tabish.jpg":"<NAME>adkar","media/zahid.jpg":"<NAME>"}
# BUILDING MODEL TO CLASSIFY BETWEEN MASK AND NO MASK

model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))
model.add(MaxPooling2D() )
model.add(Conv2D(32,(3,3),activation='relu'))
model.add(MaxPooling2D() )
model.add(Conv2D(32,(3,3),activation='relu'))
model.add(MaxPooling2D() )
model.add(Flatten())
model.add(Dense(100,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('train',target_size=(150,150),batch_size=16 ,class_mode='binary')
test_set = test_datagen.flow_from_directory('test',	target_size=(150,150), batch_size=16, class_mode='binary')
model_saved=model.fit_generator(training_set,epochs=10,validation_data=test_set,)
model.save('mymodel.h5',model_saved)
#To test for individual images
mymodel=load_model('mymodel.h5')
#test_image=image.load_img('C:/Users/<NAME>/Desktop/1/Dataset/test/without_mask/30.jpg',target_size=(150,150,3))
test_image=image.load_img(r'F:/1/test/with_mask/1-with-mask.jpg',target_size=(150,150,3))
test_image
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=0)
mymodel.predict(test_image)[0][0]
# IMPLEMENTING LIVE DETECTION OF FACE MASK
mymodel=load_model('mymodel.h5')
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while cap.isOpened():
	_,img=cap.read()
	face=face_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=4)
	for(x,y,w,h) in face:
		face_img = img[y:y+h, x:x+w]
		cv2.imwrite('temp.jpg',face_img)
		test_image=image.load_img('temp.jpg',target_size=(150,150,3))
		test_image=image.img_to_array(test_image)
		test_image=np.expand_dims(test_image,axis=0)
		pred=mymodel.predict(test_image)[0][0]
		if pred==1:
			cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
			cv2.putText(img,'NO MASK',((x+w)//2,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3)
		else:
			cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
			cv2.putText(img,'MASK',((x+w)//2,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),3)
		datet=str(datetime.datetime.now())
		cv2.putText(img,datet,(400,450),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
		if pred==1:
			for key, value in knowning.items():
				known_image = face_recognition.load_image_file(key)
				unknown_image = face_recognition.load_image_file("temp.jpg")
				biden_encoding = face_recognition.face_encodings(known_image)[0]
				unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
				results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
				if results==[True]:
					print(value+" has not wore masked properly")
	cv2.imshow('img',img)
	if cv2.waitKey(1)==ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

# -*- coding: utf-8 -*- Created on Wed Jul 22 15:56:47 2020  @author: Karan # UNCOMMENT THE FOLLOWING CODE TO TRAIN THE CNN FROM SCRATCH # BUILDING MODEL TO CLASSIFY BETWEEN MASK AND NO MASK #To test for individual images #test_image=image.load_img('C:/Users/<NAME>/Desktop/1/Dataset/test/without_mask/30.jpg',target_size=(150,150,3)) # IMPLEMENTING LIVE DETECTION OF FACE MASK