一、前言

本篇文章适合人脸识别初学者。小总结篇。
环境:

  • Python 3.3+ or Python 2.7
  • macOS or Linux (Windows这个库说是不支持的,但是应该也有办法)

下面是这个库的github地址 face_recognition 基于opencv的人脸实时识别&&face_recognition库进行本地人脸识别
对视频中的人脸抓取并匹配照片

安装 face_recognition

pip install face_recognition

二、需求

我们要做的需求就是,要求能够实时进行人脸识别,然后并截图到本地(这里我本来是想做根据人的识别结果进行截图的,但是没整成功,后面再研究一下,或者大家有思路也留言学习下hh)

三、代码

# -*- coding: utf-8 -*-
import face_recognition
import cv2
import numpy as np

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
cap = cv2.VideoCapture(0)
i=0
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("dataset/me.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("dataset/catch.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "trump",
    "wuyuhui"
]

# Initialize some variables  初始化
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video  抓取一帧视频
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing  将视频帧的大小调整为1/4以加快人脸识别处理
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # 将图像从BGR颜色(OpenCV使用)转换为RGB颜色(人脸识别使用)
    rgb_small_frame = small_frame[:, :, ::-1]



    # 仅每隔一帧处理一次视频以节省时间
    if process_this_frame:
        # 查找当前视频帧中的所有面和面编码
        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:
            # 查看该面是否与已知面匹配
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.4)
            name = "Unknown"


            # # 如果在已知的面编码中找到匹配项,请使用第一个。
            # if True in matches:
            #     first_match_index = matches.index(True)
            #     name = known_face_names[first_match_index]

            # 或者,使用与新面的距离最小的已知面
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            print face_distances
            # if face_distances[best_match_index]<=0.45:
            if matches[best_match_index]:
                name = known_face_names[best_match_index]
            face_names.append(name)

            #抓拍
            if False in matches:
                ret, frame = cap.read()
                cv2.imshow('capture', frame)
                cv2.imwrite(r"/Users/sue/desktop/picture/p" + str(i) + ".jpg", frame)
                i = i + 1

    process_this_frame = not process_this_frame


    # Display the results
    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

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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 the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

四、修改部分

1、把需要匹配的图片放在这里

opencv人物抠图算法 opencv截取人脸_opencv人物抠图算法


2、因为这个库对亚洲人和小孩的识别度不高,所以可以通过改变tolerance=0.4它的参数值来提高准确度。

opencv人物抠图算法 opencv截取人脸_opencv人物抠图算法_02


3、截图部分

把路径改一下,然后就可以在文件夹里看见截图了,但是这个还需要改善。

opencv人物抠图算法 opencv截取人脸_Python_03

五、运行结果

哦这个照片…因为我放的是obama照片,所以不一样哈,所以是unknown.大家放自己的照片会有名字的~

opencv人物抠图算法 opencv截取人脸_Python_04


截图:

opencv人物抠图算法 opencv截取人脸_opencv人物抠图算法_05


这个库还蛮好用的,行了去看论文了,这个还是不可以满足我的需求T_T

希望有帮助到需要的人8