参考资料:
【Python+OpenCV 人脸检测—CascadeClassifier 级联分类器实现】_LPY。的博客-CSDN博客
‘cv::CascadeClassifier::detectMultiScale‘_只要思想不滑坡办法总比困难多--小鱼干的博客-CSDN博客
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
#cv.__file__里的cv2目录下有一个data目录,下面存放了训练好的人脸识别分类器
cv.__file__
#加载人脸图片
img = cv.imread("../SampleImages/people.jpg")
#转换为灰度图
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, plt.cm.gray)
#实例化OpenCV人脸和眼睛识别的级联分类器
#cv.CascadeClassifier(fielpath)
#参考资料:https://blog.csdn.net/LPYchengxuyuan/article/details/122028669
# https://blog.csdn.net/weixin_45177786/article/details/123288592
face_cas = cv.CascadeClassifier("haarcascade_frontalface_default.xml")
face_cas.load("D:/Pyton-Opencv/OpencvEnv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")
eye_cas = cv.CascadeClassifier("haarcascade_eye.xml")
eye_cas.load("D:/Pyton-Opencv/OpencvEnv/Lib/site-packages/cv2/data/haarcascade_eye.xml")
#识别人脸
#faceRects = face_cas.detectMultiScale(image, scaleFactor, minNeighbors, minSize, maxSize)
#image: 要进行检测的图片
#scaleFactor: 前后两次扫描中,搜索窗口的比例系数
#minNeighbors: 至少被检测到多少次才会被认为是目标
#minSize/maxSize:目标的最小尺寸和最大尺寸
faceRects = face_cas.detectMultiScale(img_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32,32))
for faceRect in faceRects:
x,y,w,h = faceRect
#画出人脸
cv.rectangle(img, (x,y), (x+h,y+w), (0,255,0), 2)
#检测眼睛
roi_bgr = img[y:y+h,x:x+w]
roi_gray = img_gray[y:y+h,x:x+w]
eyes = eye_cas.detectMultiScale(roi_gray)
for (eye_x, eye_y, eye_w, eye_h) in eyes:
cv.rectangle(roi_bgr, (eye_x,eye_y), (eye_x + eye_w, eye_y + eye_h), (0,255,0), 1)
plt.imshow(img[:,:,::-1])
文章来源:https://www.toymoban.com/news/detail-731517.html
文章来源地址https://www.toymoban.com/news/detail-731517.html
到了这里,关于Python Opencv实践 - 人脸识别CascadeClassifier的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!