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区域。
本文详细介绍了如何使用OpenCV进行人脸检测,包括加载预训练模型、调整参数以提高精度,并演示了如何在图像中绘制人脸的边界框。通过这个教程,读者可以掌握基础的人脸检测技术并应用于实际项目中。
1546

被折叠的 条评论
为什么被折叠?



