1.原理
Haar(哈尔)级联:在进行图像分类和跟踪过程中,提取图像的细节很有用,这些细节也被称为特征,对于给定的图像,特征可能会因区域的大小而有所不同,区域大小也可被称为窗口大小。即使窗口大小不同,仅在尺度上大小不同的图像也应该有相似的特征。这种特征集合被称为级联。Haar 级联具有尺度不变性。OpenCV 提供了尺度不变Haar 级联的分类器和跟踪器。
2.
Haar(哈尔)级联训练模型
在下载Opencv的过程中,OpnCV 原代码副本文件提供了OpenCV 人脸检测所需的XML文件
一般在此路径下,也可以到官网下载
"D:\Python\Lib\site-packages\cv2\data\haarcascade_frontalface_alt_tree.xml"
3.首先导入opencv库
import cv2
4.加载xml文件
detector=cv2.CascadeClassifier(r'D:\Python\Lib\sitepackages\cv2\data\haarcascade_frontalface_default.xml') eye_cascade=cv2.CascadeClassifier(r"D:\Python\Lib\sitepackages\cv2\data\haarcascade_eye_tree_eyeglasses.xml") mouth=cv2.CascadeClassifier(r"D:\Python\Lib\sitepackages\cv2\data\haarcascade_smile.xml") cap=cv2.VideoCapture(0)#打开摄像头
5.进行检测
while cap.isOpened(): ret,img=cap.read() gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces=detector.detectMultiScale(gray,minSize=(100,100),flags=cv2.CASCADE_SCALE_IMAGE) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] # cv2.imshow('roi_gray',roi_gray) cv2.imshow('roi_color', roi_color) cv2.putText(img, 'dog_face', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) eyes = eye_cascade.detectMultiScale(roi_gray, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) mouths = mouth.detectMultiScale(roi_gray, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) cv2.putText(img,'eye',(ex,ey-1),cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,0,255),2) for (mx,my,mw,mh) in mouths: cv2.rectangle(roi_color, (mx, my), (mx + mw, my + mh), (0, 255, 0), 2) cv2.putText(img, 'mouth', (mx+100, my+100), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.imshow('frame', img) if cv2.waitKey(5) & 0xFF == ord('q'): break
cap.release() cv2.destroyAllWindows()
6.完整代码如下
'''opencv检测人脸,双眼,嘴''' import cv2 detector=cv2.CascadeClassifier(r'D:\Python\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier(r"D:\Python\Lib\site-packages\cv2\data\haarcascade_eye_tree_eyeglasses.xml") mouth = cv2.CascadeClassifier(r"D:\Python\Lib\site-packages\cv2\data\haarcascade_smile.xml") cap=cv2.VideoCapture(0) while cap.isOpened(): ret,img=cap.read() gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces=detector.detectMultiScale(gray,minSize=(100,100),flags=cv2.CASCADE_SCALE_IMAGE) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] # cv2.imshow('roi_gray',roi_gray) cv2.imshow('roi_color', roi_color) cv2.putText(img, 'dog_face', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) eyes = eye_cascade.detectMultiScale(roi_gray, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) mouths = mouth.detectMultiScale(roi_gray, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) cv2.putText(img,'eye',(ex,ey-1),cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,0,255),2) for (mx,my,mw,mh) in mouths: cv2.rectangle(roi_color, (mx, my), (mx + mw, my + mh), (0, 255, 0), 2) cv2.putText(img, 'mouth', (mx+100, my+100), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.imshow('frame', img) if cv2.waitKey(5) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()