-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmask.py
72 lines (50 loc) · 1.62 KB
/
mask.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import cv2
from tensorflow.keras.models import load_model
from keras.preprocessing.image import load_img , img_to_array
import numpy as np
model =load_model('model.h5')
img_width , img_height = 150,150
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture('test video 1.mp4')
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/%d%dface.jpg'%(img_count_full,img_count),color_face)
img = load_img('input/%d%dface.jpg'%(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)
if prediction==0:
class_label = "Mask"
color = (255,0,0)
else:
class_label = "No Mask"
color = (0,255,0)
cv2.rectangle(color_img,(x,y),(x+w,y+h),(0,0,255),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()