from tensorflow.keras.models import load_model
classifier=load_model('my_mask.h5')
from tensorflow.keras.preprocessing import image
import numpy as np
import cv2
#we are starting our web cam
webcam=cv2.VideoCapture(0)
def predictor(test_img):
#test_img=image.load_img(path,target_size=(48,48))
test_img=image.img_to_array(test_img)
test_img=np.expand_dims(test_img,axis=0)
result=classifier.predict(test_img)
if result[0][0] == 0:
prediction = 'with_mask'
else:
prediction = 'without_mask'
return prediction
while(True):
#capture frame by frame
#ret is boolean veriable
#frame capture images bcoz vedio is collection of images
ret,frame=webcam.read()
haar_face_cascade=cv2.CascadeClassifier('C:/Users/lenovo/Downloads/
haarcascadefrontalfaces/haarcascade_frontalface_default.xml')
g_frame=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=haar_face_cascade.detectMultiScale(frame,scaleFactor=1.06,minNeighbors=5)
#print("faces found")
#print(faces)
for(x,y,w,h) in faces:
crop=frame[y:y+h,x:x+w]
roi=cv2.resize(crop,(64,64))
path='s.jpg'
#loaded_model_json = json_file.read()
#loaded_model = model_from_json(loaded_model_json)
cv2.imwrite(path,roi)
name=predictor(roi)
print(name)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(frame,name,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(255, 0, 0),2)
cv2.imshow('frame',frame)
if(cv2.waitKey(1) & 0xFF==ord('q')):
cv2.imwrite("myimage.jpg",frame)
break
#when everythong is done we realese the capture
webcam.release()
|