Skip to content

Instantly share code, notes, and snippets.

@inaniwa3
Created December 26, 2018 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inaniwa3/ccd652d620f00760fe254b3480164874 to your computer and use it in GitHub Desktop.
Save inaniwa3/ccd652d620f00760fe254b3480164874 to your computer and use it in GitHub Desktop.
# coding: utf-8
import glob
import cv2
import os
import shutil
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
smile_classifier = cv2.CascadeClassifier('haarcascade_smile.xml')
def detect_smile(path_in):
print(path_in)
img = cv2.imread(path_in)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=2, minSize=(250, 250))
is_detect = False
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_color = img[y:y+h, x:x+w]
roi_gray = gray[y:y+h, x:x+w]
smiles = smile_classifier.detectMultiScale(roi_gray, scaleFactor=1.7, minNeighbors=22)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (0, 255, 0), 2)
is_detect = True
basebame = os.path.basename(path_in)
cv2.imwrite('out.all/' + basebame, img)
if is_detect:
shutil.copy(path_in, 'out.smile.cp/' + basebame)
cv2.imwrite('out.smile.rect/' + basebame, img)
paths_in = sorted(glob.glob('in/*.png'))
for path_in in paths_in:
detect_smile(path_in)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment