一般的な顔検出器、カメラを呼び出して顔を検出する
序文
主にいくつかの一般的な顔検出器を紹介し、opencv を組み合わせてリアルタイム顔検出用のカメラを呼び出します。
1.関連パッケージをインポートする
import cv2
import numpy as np
import matplotlib.pyplot as plt
import dlib
from mtcnn.mtcnn import MTCNN
2.ハール検出器
- Haar 特徴: エッジ特徴検出 + 線形特徴検出アルゴリズムは、実際の特徴と理想的な特徴の近さを計算して比較します。
- 黒ピクセルの平均を計算する
- 白ピクセルの平均を計算する
- 黒と白のピクセルの平均差が 1 に近いほど計算され、この特徴が見つかる可能性が高くなります。
- 重量ファイルはオンラインで見つけることができます
img = cv2.imread('./images/faces2.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')
# scaleFactor: 调整图片尺寸
# minNeighbors: 寻找人脸框的数量
# minSize: 最小人脸尺寸
# maxSize: 最大人脸尺寸
detections = face_detector.detectMultiScale(img_gray, scaleFactor=1.3, minNeighbors=7,
minSize=(10, 10), maxSize=(100, 100))
for (x, y, w, h) in detections:
# 绘制矩形框, 在 img 图上进行绘制
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 5)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
3. 豚探知機
img = cv2.imread('./images/faces2.jpg')
# 构造 hog 检测器
hog_face_detector = dlib.get_frontal_face_detector()
# 检测人脸
# scale: 类似 haar 的 scaleFactor
detections = hog_face_detector(img, 1)
# 进行迭代, 解析 detections
for face in detections:
x = face.left()
y = face.top()
r = face.right()
b = face.bottom()
cv2.rectangle(img, (x, y), (r, b), (0, 255, 0), 5)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
4.CNN検出器
img = cv2.imread('./images/faces2.jpg')
cnn_face_detector = dlib.cnn_face_detection_model_v1('./weights/mmod_human_face_detector.dat')
# 检测人脸
detections = cnn_face_detector(img, 1)
# 解析 detections
for face in detections:
x = face.rect.left()
y = face.rect.top()
r = face.rect.right()
b = face.rect.bottom()
# 置信度
c = face.confidence
cv2.rectangle(img, (x, y), (r, b), (0, 255, 0), 5)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
5.SSD検出器
img = cv2.imread('./images/faces2.jpg')
# 加载模型
face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
'./weights/res10_300x300_ssd_iter_140000.caffemodel')
# 原图尺寸
img_height = img.shape[0]
img_width = img.shape[1]
# 缩放至模型输入尺寸
img_resize = cv2.resize(img, (500, 300))
# 图像转为Blob
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()
# 检测人脸数量
num_of_face = detections.shape[2]
print(num_of_face) # 200
print(detections.shape[3]) # 7
# 迭代遍历人脸数量
for index in range(num_of_face):
# 置信度
detection_confience = detections[0, 0, index, 2]
# 挑选置信度
if detection_confience > 0.15:
# 位置
locations = detections[0, 0, index, 3:7] * np.array([img_width, img_height, img_width, img_height])
# 打印置信度
print(str(round(detection_confience * 100, 2)) + '%')
l, t, r, b = locations.astype('int')
# 绘制矩形框
cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 3)
# 绘制
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
6.MTCNN検出器
- MTCNN は顔を検出し、受信した画像は RGB 順序で表示されます
img = cv2.imread('./images/faces2.jpg')
# MTCNN 需要RGB顺序
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 加载模型
face_detector = MTCNN()
# 检测人脸
detections = face_detector.detect_faces(img_cvt)
# 解析
for face in detections:
(x, y, w, h) = face['box']
cv2.rectangle(img_cvt, (x, y), (x+w, y+h), (0, 255, 0), 3)
plt.imshow(img_cvt)
plt.show()
7. Opencv と検出器を組み合わせて顔を検出する
- 2種類の顔検出装置+カメラ画像の取得により顔検出が可能
- パラメータは自分で調整する必要がありますが、効果はより良くなります
7.1 豚検出器
cap = cv2.VideoCapture(0)
hog_face_detector = dlib.get_frontal_face_detector()
while True:
rec, frame = cap.read()
frame = cv2.flip(frame, 1)
detections = hog_face_detector(frame, 1)
for face in detections:
x = face.left()
y = face.top()
r = face.right()
b = face.bottom()
cv2.rectangle(frame, (x, y), (r, b), (0, 255, 0), 5)
cv2.imshow('face', frame)
if cv2.waitKey(10) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
7.2 ハール検出器
cap = cv2.VideoCapture(0)
face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')
while True:
rec, frame = cap.read()
frame = cv2.flip(frame, 1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 添加 minNeighbors 会降低误检率
detections = face_detector.detectMultiScale(gray, minNeighbors=7)
for (x, y, w, h) in detections:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
cv2.imshow('face', frame)
if cv2.waitKey(10) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()