嗨,我正在研究面部识别。
为了提高性能,我想使用面部对齐。
当我使用例如由Adrian描述的 HOG 面部标识符时,我得到一个对齐的图像。
from imutils.face_utils import rect_to_bb
from dlib import get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor('/home/base/Documents/facial_landmarks/shape_predictor_5_face_landmarks.dat')
fa = face_utils.facealigner.FaceAligner(shape_predictor, desiredFaceWidth=112, desiredLeftEye=(0.3, 0.3))
img=cv2.imread(pathtoimage)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 2)
for rect in rects:
(x, y, w, h) = rect_to_bb(rect)
faceAligned = fa.align(img, gray, rect)
但是,我必须在嵌入式硬件上工作,而且 HOG 面部识别速度还不够快。最好的工作是 cv2 lbpcascader。
使用 cv2 我也得到了找到的脸的框,但使用它不起作用。
faces_detected = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=4)
在使用 HOG 的其他示例中,坐标是从 HOG-rect 中提取的:
(x, y, w, h) = rect_to_bb(rect)
然后与
aligned_face = fa.align(img, gray, dlib.rectangle(left = x, top=y, right=w, bottom=h))
这个想法是将 x,y,w,h 与 cv2 值交换。不幸的是,这不起作用,因为上面的两行会导致完全错误的对齐。在第一个代码示例中,包含但未使用 rect_to_bb 函数。
我检查了这些值,它们以某种方式关闭:
- 224x224 图像
- 156 70 219 219 cv2 值(当然略有不同)
- 165 101 193 193 rect_to_bb 的 rect 值
- [(165, 101) (358, 294)] 矩形值
我检查了 rect_to_bb 函数,但这似乎很简单:
def rect_to_bb(rect):
# take a bounding predicted by dlib and convert it
# to the format (x, y, w, h) as we would normally do
# with OpenCV
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
# return a tuple of (x, y, w, h)
return (x, y, w, h)