This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_smile.py
84 lines (69 loc) · 3.07 KB
/
detect_smile.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
72
73
74
75
76
77
78
79
80
81
82
83
84
# import the necessary packages
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import imutils
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument('-c', '--cascade', required=True,
help='path to where the face cascade resides')
ap.add_argument('-m', '--model', required=True,
help='path to the pre-trained smile detector CNN')
ap.add_argument('-v', '--video',
help='path to the (optional) video file')
args = vars(ap.parse_args())
# load the face detector cascade and smile detector CNN
detector = cv2.CascadeClassifier(args['cascade'])
model = load_model(args['model'])
# if a video path was not supplied, grab the refrences to the webcam
if not args.get('video', False):
print('[INFO] starting video capture...')
camera = cv2.VideoCapture(0)
# otherwise, load the video
else:
camera = cv2.VideoCapture(args['video'])
# keep looping
while True:
# grab the current frame
(grabbed, frame) = camera.read()
# if we are viewing a video and we did no grab a frame, then we
# have reached the end of the video
if args.get('video') and not grabbed:
break
# resize the fram, convert it to grayscale, and then clone the
# orgignal frame so we draw on it later in the program
frame = imutils.resize(frame, width=700)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameClone = frame.copy()
# detect faces in the input frame, then clone the frame so that we can draw onit
rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
for (fX, fY, fW, fH) in rects:
# extract the ROI of the face from the grayscale image
# resize it to a fixed 28x28 pixels, and then prepare the
# ROI for classification via the CNN
roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (28, 28))
roi = roi.astype('float') / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
# determine the probaboilities of both 'smiling' and 'not smiling',
# then set the label accordingly
(notSmiling, Smiling) = model.predict(roi)[0]
label = 'Smiling' if Smiling > notSmiling else "Not Smiling"
# display the label and bounding box on the output frame
if label == 'Smiling':
cv2.putText(frameClone, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 255, 0), 2)
else:
cv2.putText(frameClone, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)
# show our detected face along with smiling/not smiling labels
cv2.imshow('Face', frameClone)
# if 'q' key is pressed, stop the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()