-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmask_detect.py
More file actions
35 lines (34 loc) · 1.36 KB
/
Copy pathmask_detect.py
File metadata and controls
35 lines (34 loc) · 1.36 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
# import necessary packages
import cvlib as cv
import cv2
import numpy as np
from keras.applications.efficientnet import preprocess_input
from keras_preprocessing.image import img_to_array
from tensorflow.python.keras.models import load_model
model = load_model('mask_detector.h5')
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
face_num, confi = cv.detect_face(frame)
for i, f in enumerate(face_num):
(startX, startY) = f[0], f[1]
(endX, endY) = f[2], f[3]
face = frame[startY:endY, startX:endX]
face = cv2.resize(face, (224, 224), interpolation=cv2.INTER_AREA)
x = img_to_array(face)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
confidence = model.predict(x)
if confidence < 0.5:
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
text = "NO MASK!!!"
cv2.putText(frame, text, (startX, startY), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
else:
cv2.rectangle(frame, (startX, startY), (endX, endY), (255, 0, 0), 2)
text = "OK"
cv2.putText(frame, text, (startX, startY), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2)
print(confidence)
cv2.imshow("Mask Detector", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break