Python cv-profileface和dlib-mmod_human_face_detector两种方法实现侧脸识别,并用dlib-mmod_human_face_detector实现分割

效果

上一篇文章无法做到侧脸的识别

Python-dlib实现人脸提取和分割-CSDN博客

cv-profileface可以识别侧脸

# 法1 opencv profileface 准确率不高 参考https://github.com/jb892/FaceDetection-HaarCascade
import cv2
import matplotlib.pyplot as plt
face_detector = cv2.CascadeClassifier('haarcascade_profileface.xml')
img = cv2.imread('2.jpg')
wid = 10
pts = 400
img=cv2.resize(img,(pts,pts))
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
face1 = face_detector.detectMultiScale(img1, scaleFactor=1.15,minNeighbors=5,minSize=(34, 35), flags=cv2.CASCADE_SCALE_IMAGE)
print(face1)
for (x, y, w, h) in face1: # 在img1上绘制一个矩形框
    cv2.rectangle(img1, (x-wid, y-wid), (x+w + wid, y+h + wid), (255, 0, 0), 2)
plt.imshow(img1)

dlib-mmod_human_face_detector 侧脸识别准确率更高

# 法2 dlib mmod_human_face_detector
#参考https://blog.csdn.net/hongbin_xu/article/details/78359520
#参考https://blog.csdn.net/LJX_ahut/article/details/124969818
import dlib
import matplotlib.pyplot as plt
# step 1. make sure you have downloaded the correct model file
face_detector_model_path = './mmod_human_face_detector.dat'
# step 2. load this model and create a cnn face detector
face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path)  # dlib.cnn_face_detection_model_v1
# step 3. read an image using dlib or cv2
# note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2
image_path = '1.jpg'
img = dlib.load_rgb_image(image_path)
pts = 300
wid = 10
img = cv2.resize(img,(pts,pts))
# img = cv2.imread(image_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# step 4. predict and detect
detections = face_detector(img, 1)  # dlib.mmod_rectangles

# step 5. get just one of the rectangle instead all of them ,the type is mmod_rectangle
detection = detections[0]  # dlib.mmod_rectangle
# the mmod_rectangle contains two parts : confidence and rect
print(detection.confidence, detection.rect)

# step 6.get face coordinates for just one as sample
l, t, r, b = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()
cv2.rectangle(img, (l-wid, t-wid), (r+wid, b+wid), (255, 0, 0), 2)
plt.imshow(img)

分割

# In[]
import os
import warnings
import cv2
import matplotlib.pyplot as plt
import dlib
 
from PIL import Image
warnings.filterwarnings("error", category=UserWarning)
PATH1 = "./data/"
outpts = 120
wid = 10
 
def is_read_successfully(file):
    try:
        imgFile = Image.open(file)
        return True
    except Exception:
        return False
 
def clipface(parent,file):
    in_path=os.path.join(parent, file)
    img = dlib.load_rgb_image(in_path)
    # plt.imshow(img);
    face_detector = dlib.cnn_face_detection_model_v1('./mmod_human_face_detector.dat')  # dlib.cnn_face_detection_model_v1
    detections = face_detector(img)
    # 人脸数
    if len(detections)> 0:
        detection = detections[0]  # dlib.mmod_rectangle
        # print(detection.confidence, detection.rect)
        l, t, r, b = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()
        #获取宽度和高度
        height=len(img)
        width=len(img[0])
        img1=img[t-wid:b+wid,l-wid:r+wid]
        height=len(img1)
        width=len(img1[0])
        print('图片大小%d X %d'%(width,height))
        plt.imshow(img1)
        dir=os.path.join('face/',parent)
        if not os.path.exists(dir):
            os.makedirs(dir)
        cv2.imwrite(os.path.join(dir,file), cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))

if __name__=="__main__":
    #子文件夹
    for childlist in os.listdir(PATH1):
        #子文件夹路径
        childPATH = PATH1 + '/'+ str(childlist)
        for cpath, dirs, files in os.walk(childPATH):
            if childlist== '14' :
                sum=0
                print (childlist)
                for file in files:
                    in_file = os.path.join(cpath, file)
                    if not is_read_successfully(in_file):
                        print('cant open'+ in_file)
                    else:
                        sum+=1
                        print(str(sum) + 'read ' + in_file)
                        clipface(cpath,file)
# %%

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值