【摸魚神器】基於python的BOSS識別系統

專注的阿熊發表於2022-05-05

import os

import cv2

import sys

import time

import numpy as np

import face_recognition

from PyQt5.QtGui import QPixmap

from PyQt5.QtWidgets import QApplication,QLabel

# 全域性變數

path = './data'  # 人像儲存路徑

showflag = True  # 是否實時顯示影像

# 利用 PyQt5 開啟全屏視窗,實現視窗替換效果

def lock_screen(image_path='lock.jpg'):

     app = QApplication(sys.argv)

     pixmap = QPixmap(image_path)

     screen = QLabel()

     screen.setPixmap(pixmap)

     screen.showFullScreen()

     sys.exit(app.exec_())

# 讀取本地肖像庫,建立識別白名單

def load_portrait(path=path):

     '''

     path :肖像庫路徑

     '''

     # 訊息提示

     print('>>> 本地影像庫讀取中,請稍後 ',end='')

     for i in range(5):

         print('.', end='')

         time.sleep(0.3)

     # 建立白名單

     white_map = {}

     for name in os.listdir(path):

         filepath = '%s/%s'%(path, name)

         avg_coding = np.zeros(128)

         n = 0

         for file in os.listdir(filepath):

             if '.jpg' in file:

                 image = face_recognition.load_image_file('%s/%s'%(filepath, file))

                 encoding = face_recognition.face_encodings(image)[0]

                 avg_coding += encoding

                 n += 1

         avg_coding /= n

         white_map[name] = avg_coding

         print('>>"%s" 人臉資料載入完成! '%name)

     return white_map

# 人臉識別,判斷當前畫面的人像是否與白名單中的匹配

def recognize(frame, white_map):

     '''

     frame: 捕獲的攝像頭幀

     white_map: 人像庫白名單

     '''

     # 根據白名單,提取肖像編碼

     known_face_encodings = list(white_map.values())

     known_face_names = list(white_map.keys())

     # 影像預處理(包括大小調整、格式轉換)

     frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # 調整影像大小,以減小計算需求

     frame = frame[:, :, ::-1] # BGR->RGB

     # 計算人臉的編碼值

     face_locations = face_recognition.face_locations(frame)

     face_encodings =外匯跟單gendan5.com face_recognition.face_encodings(frame, face_locations)

     # 計算的編碼與白名單比較,獲得其匹配的名字

     face_names = []

     for face_encoding in face_encodings:

         # 預設為 " 未知 "

         name = ' 未知 '

         # 匹配

         matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

         if True in matches:

             index = matches.index(True)

             name = known_face_names[index]

         face_names.append(name)

     return face_names, face_locations

if __name__ == '__main__':

     # 載入白名單

     white_map = load_portrait(path)

     # 開啟攝像頭

     video_capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)

     # 採集影像

     flag = 0

     while True:

         flag %= 3

         _, frame = video_capture.read()

         if flag == 0: # 3 幀處理因此(提高處理速度,防止影片卡頓)

             face_names, face_locations = recognize(frame, white_map)

             if ' 未知 ' in face_names: # 如果有白名單之外的人

                 lock_screen()

                 break

         flag += 1

         if showflag:

             # 將人臉框出

             for (top, right, bottom, left), name in zip(face_locations, face_names):

                 # 改變座標位置(因為處理時原圖被縮小了 4*4

                 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)

             # 顯示影像

             cv2.imshow('monitor', frame)

         # Q 退出

         if cv2.waitKey(1) & 0xFF == ord('q'):

             break

     video_capture.release()

     cv2.destroyAllWindows()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2890835/,如需轉載,請註明出處,否則將追究法律責任。

相關文章