欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

face_recognition 人脸识别和检测

程序员文章站 2022-07-12 20:58:43
...

1、安装

可以直接 pip install face_recognition
出现安装错误:pip报错 CMake must be installed to build the following extensions: dlib
先按照cmake再重新安装face_recognition即可: pip install cmake

2、检测图片人脸

import numpy as np
import cv2
import face_recognition
image = face_recognition.load_image_file("test3.jpeg")
#return (A,B,C,D)(top, right, bottom, left) (D,A,B,C)
face_locations = face_recognition.face_locations(image)
print(face_locations)
image1=image*1
image1[:,:,0]=image[:,:,2]
image1[:,:,2]=image[:,:,0]
for (A,B,C,D) in face_locations:
    cv2.rectangle(image1,(D,A),(B,C),(0,255,0),2)
cv2.imshow('image',image1)
cv2.waitKey(0)
cv2.destroyAllWindows()

face_recognition 人脸识别和检测

2、也支持直接摄像头检测人脸

import cv2

import face_recognition

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()

    # return (A,B,C,D)(top, right, bottom, left) (D,A,B,C)
    face_locations = face_recognition.face_locations(frame)

    for (A, B, C, D) in face_locations:
        cv2.rectangle(frame, (D, A), (B, C), (0, 255, 0), 2)
    cv2.imshow('image', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

3、面部点识别

# 自动识别人脸特征
from PIL import Image, ImageDraw
import face_recognition

# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("test3.jpeg")

#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
#打印发现的脸张数
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

# 让我们在图像中描绘出每个人脸特征!
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)

for face_landmarks in face_landmarks_list:

   #打印此图像中每个面部特征的位置
    facial_features = [
        'chin',
        'left_eyebrow',
        'right_eyebrow',
        'nose_bridge',
        'nose_tip',
        'left_eye',
        'right_eye',
        'top_lip',
        'bottom_lip'
    ]

    for facial_feature in facial_features:
        print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))



    for facial_feature in facial_features:
        d.line(face_landmarks[facial_feature], width=5)

pil_image.show()

face_recognition 人脸识别和检测

4、人脸识别

注意是运用把图片向量化进行比对相似性,参考:https://blog.csdn.net/qq_41251963/article/details/85473561#commentBox

1、人脸匹配函数——compare_faces
       compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

       比较脸部编码列表和候选编码,看看它们是否匹配,设置一个阈值,若两张人脸特征向量的距离,在阈值范围之内,则认为其 是同一个人

       参数:

       known_face_encodings:已知的人脸编码列表

       face_encoding_to_check:待进行对比的单张人脸编码数据

       tolerance=0.6:两张脸之间有多少距离才算匹配。该值越小对比越严格,0.6是典型的最佳值

       返回值:

       一个 True或者False值的列表,该表指示了known_face_encodings列表的每个成员的匹配结果
人脸编码函数——face_encodings
     
   2、  face_encodings(face_image, known_face_locations=None, num_jitters=1)

       给定一个图像,返回图像中每个人脸的128脸部编码(特征向量)。

       参数:

       face_image:输入的人脸图像

       known_face_locations:可选参数,如果你知道每个人脸所在的边界框

       num_jitters=1:在计算编码时要重新采样的次数。越高越准确,但速度越慢(100就会慢100倍)

       返回值:

       一个128维的脸部编码列表

# 导入库
import os
import face_recognition
# 制作所有可用图像的列表
images = os.listdir('images')
# 加载图像
image_to_be_matched = face_recognition.load_image_file('my_image.jpg')

# 将加载图像编码为特征向量

image_to_be_matched_encoded = face_recognition.face_encodings(

   image_to_be_matched)[0]

# 遍历每张图像
for image in images:
   # 加载图像
   current_image = face_recognition.load_image_file("images/" + image)
   # 将加载图像编码为特征向量
   current_image_encoded = face_recognition.face_encodings(current_image)[0]

   # 将你的图像和图像对比,看是否为同一人

   result = face_recognition.compare_faces(

       [image_to_be_matched_encoded], current_image_encoded)

   # 检查是否一致

   if result[0] == True:

       print ("Matched: " + image)

   else:

       print ("Not matched: " + image)