1.示例代码
import cv2
if __name__ == '__main__':
img = cv2.imread(r'..\img\burenshi.jpg')
#加载人脸检测模型
face_detector = cv2.CascadeClassifier(r'c:\program files (x86)\microsoft visual studio\shared\python37_64\lib\site-packages\cv2\data\haarcascade_frontalface_alt2.xml')
#调参增加检测精度
faces = face_detector.detectMultiScale(img,
scaleFactor=1.1, #缩放每次检测范围,默认1.1
minNeighbors=3)
#绘制人脸检测ROI
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()
2.结果展示
总结
基于openCV的人脸检测,首先要引入人脸检测模型,然后在使用其绘图函数绘制脸部ROI区域。