睿智的目標檢測14——face_recognition人臉識別庫的安裝與簡要使用(Windows)

睿智的目標檢測14——face_recognition人臉識別庫的安裝與簡要使用

學習前言

我死了我死了我死了!
在這裏插入圖片描述

什麼是face_recognition

face_recognition是一個基於python的開源的人臉識別庫,據說識別準確率達到了99.38%,效果比較好!代碼網址爲:https://github.com/ageitgey/face_recognition

安裝步驟(Windows)

1、下載dlib的whl

點擊鏈接:https://pypi.org/simple/dlib/
下載“dlib-19.7.0-cp36-cp36m-win_amd64.whl“安裝包。
face_recognition在Windows上使用貌似需要dlib-19.6以上的版本,因此建議大家安裝python3.6。
下載完後呢,進入目標文件夾,進行whl的安裝。
在這裏插入圖片描述

2、安裝face_recognition

利用如下代碼進行face_recognition的安裝。

pip install face_recognition

之後就能完成安裝了!
在這裏插入圖片描述

簡要使用方法

擺放格式如下,face_dataset用於放人臉:
在這裏插入圖片描述
face_recognition的使用demo

import face_recognition
import numpy as np
import cv2
import os

class face_rec():
    def __init__(self):
        #-----------------------------------------------#
        #   對數據庫中的人臉進行編碼
        #   known_face_encodings中存儲的是編碼後的人臉
        #   known_face_names爲人臉的名字
        #-----------------------------------------------#
        face_list = os.listdir("face_dataset")
        self.known_face_encodings=[]
        self.known_face_names=[]
        for face in face_list:
            name = face.split(".")[0]
            image = face_recognition.load_image_file("./face_dataset/"+face)
            face_encoding = face_recognition.face_encodings(image)[0]
            self.known_face_encodings.append(face_encoding)
            self.known_face_names.append(name)

    def recognize(self,draw):
        #-----------------------------------------------#
        #   人臉識別
        #   先定位,再進行數據庫匹配
        #-----------------------------------------------#
        height,width,_ = np.shape(draw)
        draw_rgb = cv2.cvtColor(draw,cv2.COLOR_BGR2RGB)

        # 根據上述參數進行人臉檢測
        # face_locations的格式爲(top, right, bottom, left)
        face_locations = face_recognition.face_locations(draw_rgb, number_of_times_to_upsample=1, model="hog")
        if(len(face_locations) == 0):
            return draw

        # 批量編碼
        face_encodings = face_recognition.face_encodings(draw_rgb, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # 取出一張臉並與數據庫中所有的人臉進行對比,計算得分
            matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding, tolerance = 0.4)
            name = "Unknown"
            # 找出距離最近的人臉
            face_distances = face_recognition.face_distance(self.known_face_encodings, face_encoding)
            # 取出這個最近人臉的評分
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = self.known_face_names[best_match_index]
            face_names.append(name)

        #-----------------------------------------------#
        #   畫框~!~
        #-----------------------------------------------#
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            print(top, right, bottom, left)
            if top < 0:
                top = 0
            if left < 0:
                left = 0
            if right > width:
                right = width
            if bottom > height:
                bottom = height
            cv2.rectangle(draw, (left, top), (right, bottom), (0, 0, 255), 2)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(draw, name, (left , bottom - 15), font, 0.75, (255, 255, 255), 2) 
        return draw

if __name__ == "__main__":

    dududu = face_rec()
    video_capture = cv2.VideoCapture(0)

    while True:
        ret, draw = video_capture.read()
        dududu.recognize(draw) 
        cv2.imshow('Video', draw)
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break

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