【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 inline
plt.rcParams['figure.dpi'] = 200
img = 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.shape
for (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 inline
plt.rcParams['figure.dpi'] = 200
img = 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)
detections
len(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 inline
plt.rcParams['figure.dpi'] = 200
img = 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.confidence
print(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 inline
plt.rcParams['figure.dpi'] = 200
img = 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()
detections
num_of_detections = detections.shape[2]
num_of_detections
img_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))
原文地址:https://blog.csdn.net/m0_74183164/article/details/143060476
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!