其他分享
首页 > 其他分享> > face_recognition安装及各种应用

face_recognition安装及各种应用

作者:互联网

1.安装

首先,必须提前安装cmake、numpy、dlib,其中,由于博主所用的python版本是3.6.4(为了防止不兼容,所以用之前的版本),只能安装19.7.0及之前版本的dlib,所以直接pip install dlib会报错,需要pip install dlib==19.7.0

安装完预备库之后就可以直接pip install face_recognition

2.应用

(1)提取人脸

import face_recognition
from PIL import Image
image = face_recognition.load_image_file("1.jpg")
face_locations = face_recognition.face_locations(image) # top, right, bottom, left
#以下展示提取的人脸
for face_location in face_locations:
    # Print the location of each face in this image
    top, right, bottom, left = face_location
    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()

(2)查找面部特征轮廓线

import face_recognition
from PIL import Image,ImageDraw
image = face_recognition.load_image_file("1.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
#以下为展示轮廓线
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:
        d.line(face_landmarks[facial_feature], width=5)
del d
pil_image.show()

(3)比较人脸

import face_recognition
known_image = face_recognition.load_image_file("known_person.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

(4)辨别人脸

#可以设置多张已知人脸
import face_recognition import cv2,time # 人物名称的集合 known_face_names = ["first","second"] face_locations = [] face_encodings = [] demo_names = [] process_this_demo = True # 本地图像一 first_image = face_recognition.load_image_file("first_person.jpg") first_encoding = face_recognition.face_encodings(first_image)[0] # 本地图像二 second_image = face_recognition.load_image_file("second_person.jpg") second_encoding = face_recognition.face_encodings(second_image)[0] known_face_encodings = [first_encoding,second_encoding] # demo path = "unknown.jpg" demo = cv2.imread(path) demo_image = face_recognition.load_image_file(path) demo_encodings = face_recognition.face_encodings(demo_image) small_demo = cv2.resize(demo, (0, 0), fx=0.25, fy=0.25) rgb_small_demo = small_demo[:, :, ::-1] demo_face_locations = face_recognition.face_locations(rgb_small_demo) for demo_encoding in demo_encodings: # 默认为unknown matches = face_recognition.compare_faces(known_face_encodings, demo_encoding) name = "unknown" if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] demo_names.append(name) # 将捕捉到的人脸显示出来 for (top, right, bottom, left), name in zip(demo_face_locations, demo_names): # Scale back up face locations since the demo we detected in was scaled to 1/4 size top *= 4 right *= 4 bottom *= 4 left *= 4 # 矩形框 cv2.rectangle(demo, (left, top), (right, bottom), (0, 0, 255), 2) #加上标签 cv2.rectangle(demo, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(demo, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # Display cv2.imshow('face_recognition', demo) cv2.waitKey(0) cv2.destroyAllWindows()

(5)摄像头实时辨别人脸

import face_recognition
import cv2,time

video_capture = cv2.VideoCapture(0)
# 本地图像一
first_image = face_recognition.load_image_file("1.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0]

# 本地图像二
second_image = face_recognition.load_image_file("3.jpg")
second_face_encoding = face_recognition.face_encodings(second_image)[0]

# 本地图片三
third_image = face_recognition.load_image_file("5.jpg")
third_face_encoding = face_recognition.face_encodings(third_image)[0]

# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [
    first_face_encoding,
    second_face_encoding,
    third_face_encoding
]
# 人物名称的集合
known_face_names = [
    "first",
    "second",
    "third"
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
    # 读取摄像头画面
    ret, frame = video_capture.read()
    # 改变摄像头图像的大小,图像小,所做的计算就少
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
    rgb_small_frame = small_frame[:, :, ::-1]
    # Only process every other frame of video to save time
    if process_this_frame:
        # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        face_names = []
        for face_encoding in face_encodings:
            # 默认为unknown
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]
            face_names.append(name)
    process_this_frame = not process_this_frame
    # 将捕捉到的人脸显示出来
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4
        # 矩形框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        #加上标签
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
    # Display
    cv2.imshow('monitor', frame)
    # 按Q退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

 

标签:demo,image,cv2,face,encodings,安装,recognition
来源: https://www.cnblogs.com/chengjunkai/p/15852432.html