简体   繁体   English

使用 opencv 检测没有眉毛和下巴的人脸

[英]Detect faces without eyebrows and jaw using opencv

I am trying to detect faces (specifically, faces with opened eyes) using OpenCV haar cascade classifier.我正在尝试使用 OpenCV haar 级联分类器检测面部(特别是睁开眼睛的面部)。 However, I had a problem detecting the faces that do not have eyebrows and/or jaw, as shown in the following image.但是,我在检测没有眉毛和/或下巴的面部时遇到了问题,如下图所示。 I had tried many haar cascade for face detection such as haarcascade_frontalface_default.xml , haarcascade_frontalface_alt_tree.xml , etc. But all of these did not work.我尝试了许多用于人脸检测的 haar 级联,例如haarcascade_frontalface_default.xmlhaarcascade_frontalface_alt_tree.xml等。但所有这些都不起作用。

在此处输入图像描述 在此处输入图像描述

Here is my code:这是我的代码:

import cv2
import os
import glob

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')

count = 0
path = "./test/*.png"
for index, filename in enumerate(glob.glob(path)):
    img = cv2.imread(filename)
    basename = os.path.splitext(os.path.basename(filename))[0]

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        # cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)

        if len(eyes) >= 2:
            count = count + 1
            output_dir = './test/output'
            cv2.imwrite(f'{output_dir}/{basename}.png', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Thank you in advance!先感谢您!

Use facial landmarks with dlib , this method may work for you, see these two links:dlib中使用面部标记,此方法可能对您有用,请参阅以下两个链接:

Also, see this link:另外,请参阅此链接:

If you have tensorflow installed you can use a neural net to detect faces which give much better accuracy than the simple haar classifier.如果您安装了 tensorflow,您可以使用神经网络来检测人脸,这比简单的 haar 分类器提供更好的准确度。

Here's an example using the MTCNN detector which uses tensorflow as the backend.这是一个使用 MTCNN 检测器的示例,该检测器使用 tensorflow 作为后端。

from mtcnn.mtcnn import MTCNN
from PIL import Image
import numpy as np

img = Image.open('6qNFu.png') # load the image
img = np.asarray(img, dtype='uint8') # convert to numpy array
img = img[:,:,0:3] # drop the alpha channel

detector = MTCNN() # initialize MTCNN detector
print(detector.detect_faces(img)) # use MTCNN detector to return bounding box and face metrics

Using the bounding box you can extract the face from the image.使用边界框,您可以从图像中提取人脸。 Note: if the face is truncated like in the instances above, it might return a negative coordinate which is an extrapolation of where it thinks the face might be.注意:如果面部像上面的例子一样被截断,它可能会返回一个负坐标,这是它认为面部可能在哪里的外推。

Here is a the documentation on MTCNN library: https://pypi.org/project/mtcnn/ It also tells you how to install it.这是关于 MTCNN 库的文档: https://pypi.org/project/mtcnn/它还告诉您如何安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用OpenCV从“ Waldos Waldo”图片中检测面部 - Detect faces from “Where's Waldo” picture using OpenCV 使用Python中的OpenCV从图像中裁剪面 - Cropping faces from an image using OpenCV in Python 使用 Opencv Python 从相机中检测人脸 - Detecting faces from camera using Opencv Python 使用OpenCV检测手 - Detect Hand using OpenCV 如何用像素检测每个30个细胞的坐标? (不使用opencv,理想情况下不使用任何库) - How to detect the coordinates of each 30 cells with pixels? (without using opencv, ideally without using any library) 使用脚本从给定目录路径加载和检测人脸时,Python“OpenCV 错误:断言失败(size.width>0 && size.height>0)” - Python "OpenCV Error: Assertion failed (size.width>0 && size.height>0)" when using script to load and detect faces from a given directory path 用 python 和 opencv 计算人脸 - count faces with python and opencv 检测视频中的人脸,裁剪它们并以相同的顺序保存所有帧 - opencv python - Detect Faces in video, crop them and save all frames in the same order - opencv python 使用opencv从图片中裁剪多个面并将它们存储在一个文件夹中 - Crop multiple faces from a picture using opencv and store them in a folder 如何使用openCV检测脸颊? - How to detect cheeks using openCV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM