Skip to content

Instantly share code, notes, and snippets.

@codedependant
Last active December 14, 2015 09:50
Show Gist options
  • Save codedependant/5068143 to your computer and use it in GitHub Desktop.
Save codedependant/5068143 to your computer and use it in GitHub Desktop.
Face Detection with OpenCV
import numpy as np
import cv2
import cv2.cv as cv
from video import create_capture
from common import clock, draw_str
import MySQLdb
help_message = '''
USAGE: facedetect.py [--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]
'''
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags = cv.CV_HAAR_SCALE_IMAGE)
if len(rects) == 0:
return []
rects[:,2:] += rects[:,:2]
return rects
def draw_rects(img, rects, color):
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
if __name__ == '__main__':
import sys, getopt
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try: video_src = video_src[0]
except: video_src = 'synth:bg=../cpp/lena.jpg:noise=0.05'
args = dict(args)
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)
#cam = create_capture(video_src)
img = cv2.imread(video_src)
t = clock()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment