-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (56 loc) · 1.92 KB
/
Copy pathmain.py
File metadata and controls
74 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""MaskDetectionScript.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1E46tSToZWnOlESxou1PgRzTuy9XmqQYg
"""
import cv2
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
model = load_model('saved_model.h5')
img_width , img_height = 150,150
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture('video.mp4')
# cap = cv2.VideoCapture(0) # for using system camera
img_count_full = 0
font = cv2.FONT_HERSHEY_SIMPLEX
org = (1,1)
class_label = ''
fontScale = 1
color = (255,0,0)
thickness = 2
while True:
img_count_full += 1
response , color_img = cap.read()
if response == False:
break
scale = 50
width = int(color_img.shape[1]*scale/100)
height = int(color_img.shape[0]*scale/100)
dim = (width,height)
color_img = cv2.resize(color_img,dim,interpolation = cv2.INTER_AREA)
gray_img = cv2.cvtColor(color_img,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_img,1.1,6)
img_count = 0
for (x,y,w,h) in faces:
org = (x-10,y-10)
img_count += 1
color_face = color_img[y:y+h,x:x+w]
cv2.imwrite('input/%face%d.png'%(img_count_full,img_count),color_face)
img = load_img('input/%face%d.png'%(img_count_full,img_count),target_size = (img_width,img_height))
img = img_to_array(img)
img = np.expand_dims(img,axis = 0)
prediction = model.predict(img, verbose=0)
if prediction == 0:
class_label = "Mask"
color = (0,255,0)
else:
class_label = "No Mask"
color = (0,0,255)
cv2.rectangle(color_img,(x,y),(x+w,y+h),(255,0,0),3)
cv2.putText(color_img, class_label, org, font, fontScale, color, thickness, cv2.LINE_AA)
cv2.imshow("Face mask detection",color_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()