-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmask_detector_image.py
52 lines (43 loc) · 1.77 KB
/
mask_detector_image.py
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
# Importing Libraries
import cv2
import numpy as np
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
# Loading the Model
model = load_model('mask_detector.h5')
# Loading the face
# detector
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Initiating the image
image = cv2.imread("testing faces\Cool1.jpg")
image = cv2.resize(image, (600, 600), interpolation=cv2.INTER_AREA)
faces_detected = face_cascade.detectMultiScale(image, 1.2, 7, minSize=(60, 60), flags=cv2.CASCADE_SCALE_IMAGE)
faces_images = []
for (x, y, w, h) in faces_detected:
cropped_faces = image[y:y + h, x:x + w]
cropped_faces = cv2.cvtColor(cropped_faces, cv2.COLOR_BGR2RGB)
cropped_faces = cv2.resize(cropped_faces, (224, 224))
cropped_faces = img_to_array(cropped_faces)
faces_images.append(cropped_faces)
faces_images = np.array(faces_images)
faces = preprocess_input(faces_images)
preds = model.predict(faces)
for pred in preds:
(WithoutMask, CorrectMask, InCorrectMask) = pred
if max(pred) == CorrectMask:
label = " Correct Mask"
color = (0, 255, 0)
elif max(pred) == InCorrectMask:
label = " Incorrect Mask"
color = (205, 00, 0)
else:
label = " No Mask"
color = (0, 0, 255)
# include the probability in the label
label = "{}: {:.2f}%".format(label, max(WithoutMask, CorrectMask, InCorrectMask) * 100)
cv2.rectangle(image, (x, y + 20), (x + 5 + w, y + h + 15), color, 1)
cv2.putText(image, label, (x, y + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, color, 1)
# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)