91 对象检测—HAAR级联检测器使用
代码
import cv2 as cv
capture = cv.VideoCapture('../images/sample.mp4')
detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_alt.xml")
while True:
ret, image = capture.read()
if ret is True:
cv.imshow("frame", image)
faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1,
minSize=(30, 30), maxSize=(120, 120))
for x, y, width, height in faces:
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
cv.imshow("faces", image)
c = cv.waitKey(50)
if c == 27:
break
else:
break
cv.destroyAllWindows()
实验结果
解释
HAAR级联检测器,OpenCV中的HAAR级联检测器支持人脸检测、微笑、眼睛与嘴巴检测等,通过加载这些预先训练的HAAR模型数据可以实现相关的对象检测,
objects = cv.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]])
各个参数解释如下:
Objects
人脸框Image
输入图像ScaleFactor
放缩比率minNeighbors
表示最低相邻矩形框flags
标志项OpenCV3.x以后不用啦,minSize
可以检测的最小人脸maxSize
可以检测的最大人脸
所有内容均来源于贾志刚老师的知识星球——OpenCV研习社,本文为个人整理学习,已获得贾老师授权,有兴趣、有能力的可以加入贾老师的知识星球进行深入学习。