OpenCV+Dlib+FR人脸识别和对齐

人脸定位(仅Opencv)

安装:pip install opencv-python
代码:

import cv2


def detect(filename):
    face_cascade = cv2.CascadeClassifier('C:/Users/13311/Anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
    #括号内路径为:'Python路径下/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('1.jpg')

输出结果:
Opencv
存在未识别到人脸和识别错误。

人脸定位(face_recognition)

需要python3.7+opencv+face_recognition
且需要先安装cmake,boost和dlib,然后才可以安装face_recognition

cmake安装:pip install cmake
如下图则安装成功
安装成功

boost安装:pip install boost

dlib安装:pip install dlib
如存在问题,则手动安装。
自行下载相应版本的dlib(该链接为python3.7版本)
链接:https://pan.baidu.com/s/17lVu2egTUGOmJbH_YO6IdQ
提取码:ur13
下载后在命令行该whl文件路径下输入命令python -m pip install xx.whl

face_recognition安装:pip install face_recognition

代码:

import face_recognition
import cv2

image = face_recognition.load_image_file("1.jpg")
face_locations_noCNN = face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因为返回值的顺序是这样子的,因此在后面的for循环里面赋值要注意按这个顺序来


print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2 = len(face_locations_noCNN)
print(face_num2)       # The number of faces
# 到这里为止,可以观察两种情况的坐标和人脸数,一般来说,坐标会不一样,但是检测出来的人脸数应该是一样的
# 也就是说face_num1 = face_num2; face_locations_useCNN 和 face_locations_noCNN 不一样


org = cv2.imread("1.jpg")
img = cv2.imread("1.jpg")
cv2.imshow("1.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
控制台输出:
控制台
人脸识别更为准确,该例中未出现错误。

特征点检测

需要下载包shape_predictor_68_face_landmarks.dat
安装包放置于‘Python路径下/Lib/site-packages/’

代码:

import cv2
import dlib

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

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"C:\Users\13311\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat\shape_predictor_68_face_landmarks.dat")
# 括号内路径为:'Python路径下/Lib/site-packages/shape_predictor_68_face_landmarks.dat/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()

输出结果:
特征点识别

Opencv:
https://docs.opencv.org/3.3.0/d9/de5/tutorial_py_table_of_contents_objdetect.html
mooc第四周:
https://mooc.study.163.com/course/2001281004?tid=2403042001&trace_c_p_k2=a30ffd1da6024a348e70354906a3b5af#/info
旷视科技(国际顶级的人脸识别团队之一):
https://www.megvii.com/technologies/face_recognition
商汤科技(国际顶级人脸识别之一):
https://www.sensetime.com/cn/technology-detail?categoryId=30
爱奇艺:探寻人工智能:
https://so.iqiyi.com/so/q_%E6%8E%A2%E5%AF%BB%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD?source=input&sr=16028073540123868&ssrt=20210513114701184&ssra=73756ec02a6e6e284f1aae5360b422cd

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值