import cv2
import numpy as np
import sys
# Cascades can be found here
# https://github.com/opencv/opencv/blob/master/data/haarcascades
face = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
smile = cv2.CascadeClassifier("haarcascade_smile.xml")
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
sF = 1.05
while True:
# Read each frame
ret, frame = cap.read()
img = frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(
gray,
scaleFactor=sF,
minNeighbors=8,
minSize=(55, 55),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw red rectangle over the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
newSmile = smile.detectMultiScale(
gray[y:y+h, x:x+w],
scaleFactor= 1.7,
minNeighbors=25,
minSize=(25, 25),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw blue rectangle over the smile
for (x, y, w, h) in newSmile:
print ("New smile :)")
cv2.rectangle(frame[y:y+h, x:x+w], (x, y), (x+w, y+h), (255, 0, 0), 1)
cv2.imshow('Smile Detector (use ESC for exit)', frame)
c = cv2.waitKey(7) % 0x100
if c == 27:
break
cap.release()
cv2.destroyAllWindows()