#!/usr/bin/env python
'''
face detection using haar cascades
USAGE:
[--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]
'''
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import cv2 as cv
# local modules
from video import create_capture
from common import clock, draw_str
def detect(img, cascade):
rects = (img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
flags=cv.CASCADE_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:
(img, (x1, y1), (x2, y2), color, 2)
def main():
import sys, getopt
args, video_src = ([1:], '', ['cascade=', 'nested-cascade='])
try:
video_src = video_src[0]
except:
video_src = 0
args = dict(args)
cascade_fn = ('--cascade', "data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = ('--nested-cascade', "data/haarcascades/haarcascade_eye.xml")
cascade = ((cascade_fn))
nested = ((nested_fn))
cam = create_capture(video_src, fallback='synth:bg={}:noise=0.05'.format(('data/')))#此处的data文件夹是从opencv示例simple包中复制到该项目中。
while True:
_ret, img = ()
gray = (img, cv.COLOR_BGR2GRAY)
gray = (gray)
t = clock()
rects = detect(gray, cascade)
vis = ()
draw_rects(vis, rects, (0, 255, 0))
if not ():
for x1, y1, x2, y2 in rects:
roi = gray[y1:y2, x1:x2]
vis_roi = vis[y1:y2, x1:x2]
subrects = detect((), nested)
draw_rects(vis_roi, subrects, (255, 0, 0))
dt = clock() - t
draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
('facedetect', vis)
if (5) == 27:
break
print('Done')
if __name__ == '__main__':
print(__doc__)
main()
()