欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 【OpenCV】4种人脸检测方法

【OpenCV】4种人脸检测方法

2024/10/25 18:03:24 来源:https://blog.csdn.net/m0_74183164/article/details/143060476  浏览:    关键词:【OpenCV】4种人脸检测方法

代码已上传GitHub:plumqm/OpenCV-Projects at master (github.com)

Haar 模型

# 1.读入包含人脸的图片
# 2.使用haar模型识别人脸
# 3.将识别结果用矩形框画出来import cv2
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inlineplt.rcParams['figure.dpi'] = 200img = cv2.imread('./images/others/Walid_Al-Awadi/Walid_Al-Awadi_0001.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))# 构造haar检测器
face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')# 转灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 检测结果
detections = face_detector.detectMultiScale(img_gray)# 打印
detections.shapefor (x,y,w,h) in detections:cv2.rectangle(img,(x,y),(w+x,y+h),(0,255,0),5)plt.imshow(img)

下面是调参:

#调参数#scaleFactor : 调整图片尺寸
#minNeighbors : 候选人脸数量
#minSize : 最小人脸尺寸
#maxSize : 最大人脸尺寸img = cv2.imread('./images/others/Walid_Al-Awadi/Walid_Al-Awadi_0001.jpg')
# 构造haar检测器
face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
# 转灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测结果
detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.1,minNeighbors=3,minSize=(46,46),maxSize=(100,100))for (x,y,w,h) in detections:cv2.rectangle(img,(x,y),(w+x,y+h),(0,255,0),5)plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

Hog

import cv2
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inlineplt.rcParams['figure.dpi'] = 200img = cv2.imread('./images/others/Walid_Al-Awadi/Walid_Al-Awadi_0001.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))import dlib# 构造HOG人脸检测器
hog_face_detector = dlib.get_frontal_face_detector()detections = hog_face_detector(img,1)detectionslen(detections) for face in detections:x = face.left()y = face.top()r = face.right()b = face.bottom()cv2.rectangle(img,(x,y),(r,b),(255,0,0),5)plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

CNN

import cv2
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inlineplt.rcParams['figure.dpi'] = 200img = cv2.imread('./images/others/Walid_Al-Awadi/Walid_Al-Awadi_0001.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))import dlib# 构建CNN人脸检测器
cnn_face_detector =  dlib.cnn_face_detection_model_v1("./mmod_human_face_detector.dat")detections = cnn_face_detector(img,1)for face in detections:x = face.rect.left()y = face.rect.top()r = face.rect.right()b = face.rect.bottom()# 置信度c = face.confidenceprint(c)cv2.rectangle(img,(x,y),(r,b),(255,0,0),5)plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

SSD

import cv2
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inlineplt.rcParams['figure.dpi'] = 200img = cv2.imread('./images/others/Walid_Al-Awadi/Walid_Al-Awadi_0001.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))face_detector = cv2.dnn.readNetFromCaffe('./deploy.prototxt.txt','./res10_300x300_ssd_iter_140000.caffemodel')img_height = img.shape[0]
img_width = img.shape[1]# 缩放至模型输入尺寸
img_resize = cv2.resize(img, (500,300))# 图像转为二进制
img_blob = cv2.dnn.blobFromImage(img_resize,1.0,(500,300),(104.0,177.0,123.0))face_detector.setInput(img_blob)detections = face_detector.forward()
detectionsnum_of_detections = detections.shape[2]
num_of_detectionsimg_copy = img.copy()
for index in range(num_of_detections):# 置信度detection_confidence = detections[0,0,index,2]if detection_confidence>0.85:locations = detections[0,0,index,3:7] * np.array([img_width,img_height,img_width,img_height])print(detection_confidence * 100)lx,ly,rx,ry = locations.astype('int')cv2.rectangle(img_copy,(lx,ly),(rx,ry),(0,255,0),5) plt.imshow(cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB))

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com