【人臉識別】基於opencv&&face_recognition庫進行本地人臉識別並截圖保存本地

一、前言

本篇文章適合人臉識別初學者。小總結篇。
環境:

  • 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、把需要匹配的圖片放在這裏
在這裏插入圖片描述
2、因爲這個庫對亞洲人和小孩的識別度不高,所以可以通過改變tolerance=0.4它的參數值來提高準確度。在這裏插入圖片描述
3、截圖部分
把路徑改一下,然後就可以在文件夾裏看見截圖了,但是這個還需要改善。
在這裏插入圖片描述

五、運行結果

哦這個照片…因爲我放的是obama照片,所以不一樣哈,所以是unknown.大家放自己的照片會有名字的~
在這裏插入圖片描述
截圖:
在這裏插入圖片描述
這個庫還蠻好用的,行了去看論文了,這個還是不可以滿足我的需求T_T
希望有幫助到需要的人8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章