hello ,我是小白,今天又是学习的好日子,话不多说,上笔记
识别人脸首先需要什么,需要特征,例如眼睛,鼻子,耳朵,嘴巴.......
所以我们要先找资料人脸检测特征链接
应该是这样的,直接CTRL+S保存到你的python 文件夹中,
下载的是 人脸特征详细说明,一万多行,计算机根据这些特征进行人脸检测
符合一部分也算是人脸
import numpy as np
import cv2
if __name__ == '__main__':
img=cv2.imread('./456.jpg')
#人脸特征详细说明,一万多行,计算机根据这些特征进行人脸检测
#符合一部分,算作人脸
#级联分类器,检测器
face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
faces=face_detector.detectMultiScale(img) #检测出的人脸坐标
print(faces)
for x,y,w,h in faces:
cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
cv2.imshow('bao',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
对文件进行导入
faces=face_detector.detectMultiScale(img) #检测出的人脸坐标
得到一个二维列表[[x,y,w,h]], (x,y)左上角坐标,w:宽度,h:高度
cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
画出矩形,pt1 左上角, pt2右下角 thickness设置线的厚度
多个人脸检测
import numpy as np
import cv2
if __name__ == '__main__':
img=cv2.imread('./1927su.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#人脸特征详细说明,一万多行,计算机根据这些特征进行人脸检测
#符合一部分,算作人脸
#级联分类器,检测器
face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
faces=face_detector.detectMultiScale(gray,scaleFactor=1.05,minNeighbors=4) #检测出的人脸坐标
print(faces)
for x,y,w,h in faces:
cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
cv2.imshow('face',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
由于一般图片是彩色的,检测效果不是很好,可以采用灰度图进行检测,框出你想要的,
scaleFactor=1.05 识别不好往小调这个,这个是缩放,默认1.1
minNeighbors=4 多识别调这个 ,最小邻居
进行微调即可