Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created June 19, 2014 05:13
Show Gist options
  • Save goFrendiAsgard/f18cd5cbb17e7ac0218b to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/f18cd5cbb17e7ac0218b to your computer and use it in GitHub Desktop.
OpenCV-Python haar classifier
import cv2
import numpy as np
c = cv2.VideoCapture(0)
# get the xmls from https://github.com/Itseez/opencv/tree/master/data/haarcascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
while(1):
_,img = c.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
if cv2.waitKey(5)==27:
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment