# -*- coding: utf-8 -*-
"""
Created on Fri Apr 12 21:43:17 2019
@author: weizh
"""
import cv2 as cv
# haar特征+AdaBoost级联器
# 图像输入
def facedetect(image):
image = cv.imread(image)
# 级联分类器
detector = cv.CascadeClassifier('haarcascade_frontalface_alt.xml')
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, \
minSize=(10, 10), \
flags=cv.CASCADE_SCALE_IMAGE)
for (x, y, w, h) in rects:
cv.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv.imshow('image', image)
cv.waitKey(0)
# facedetect('face.png')
# 视频读入
def video_capture():
capture = cv.VideoCapture(0)
while True:
ret, frame = capture.read()
if ret==False:
break
frame = cv.flip(frame, 1)
# print(frame)
# cv.imshow('video', frame)
if cv.waitKey(25) & 0xff == ord('q'):
break
detector = cv.CascadeClassifier('haarcascade_frontalface_alt.xml')
rects = detector.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=2, \
minSize=(10, 10), \
flags=cv.CASCADE_SCALE_IMAGE)
for (x, y, w, h) in rects:
cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv.imshow('video', frame)
if cv.waitKey(25) & 0xff == ord('q'):
break
capture.release()
cv.destroyAllWindows()
video_capture()