目录

1.装包

2.实例分析

1.人脸定位(opencv)

 2.人脸定位(face_recognition)

3.特征点检测

4.人脸对齐 


1.装包

本人是在anaconda下进行python包的安装

pip install opencv-python

接着安装Dlib,发现仅仅是pip的话无法下载。然后采用下载whl文件的办法,pip install whl文件下载成功;

最后安装face_recognition

pip install face_recognition

环境配置完成

2.实例分析

1.人脸定位(opencv)

import cv2

def detect(filename):
    face_cascade = cv2.CascadeClassifier('D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')

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

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

    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)


    cv2.imshow('Person Detected!', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    detect('test7.jpg')

 

 2.人脸定位(face_recognition)

import face_recognition
import cv2

image = face_recognition.load_image_file("test7.jpg")
face_locations_noCNN=face_recognition.face_locations(image)
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)
org = cv2.imread("test7.jpg")
img = cv2.imread("test7.jpg")
cv2.imshow("test7.jpg",img)
for i in range(0,face_num2):
    top = face_locations_noCNN[i][0]
    right = face_locations_noCNN[i][1]
    bottom = face_locations_noCNN[i][2]
    left = face_locations_noCNN[i][3]

    start = (left, top)
    end = (right, bottom)

    color = (0,255,255)
    thickness = 2
    cv2.rectangle(org, start, end, color, thickness)

cv2.imshow("no cnn ",org)

cv2.waitKey(0)
cv2.destroyAllWindows()

我们可以发现,使用face_recognition准确度明显提升

3.特征点检测

#coding=utf-8

import cv2
import dlib

path = "11.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

4.人脸对齐 

import cv2
import dlib
import sys
import numpy as np
import os

predicter_path =  'D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat'
face_file_path =  '3.jpg'
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predicter_path)
bgr_img = cv2.imread(face_file_path)
if bgr_img is None:
   print("Sorry, we could not load '{}' as an image".format(face_file_path))
   exit()
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
dets = detector(rgb_img, 1)
num_faces = len(dets)
if num_faces == 0:
   print("Sorry, there were no faces found in '{}'".format(face_file_path))
   exit()
faces = dlib.full_object_detections()
for det in dets:
   faces.append(sp(rgb_img, det))
images = dlib.get_face_chips(rgb_img, faces, size=320)
image_cnt = 0
for image in images:
    image_cnt += 1
    cv_rgb_image = np.array(image).astype(np.uint8)
    cv_bgr_image = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR)
    cv2.imshow('%s'%(image_cnt), cv_bgr_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐