基于Dlib的人臉識別客戶端
需要源碼的朋友可私信我!?。?!
客戶端界面
代碼思路
1、開發(fā)環(huán)境
- 開發(fā)平臺:win10
- 開發(fā)軟件:PyCharm
- 界面開發(fā):PyQt5
2、文檔說明
- face_lib文件夾
- align_dlib.py文件:主要進行人臉對齊。
- face_recg.py文件: 進行人臉識別,其中閾值為0.4,可根據(jù)相應(yīng)情況進行修改。
- my.api.py文件: 自己寫的各種函數(shù)方法。
- udp_recv.py文件:包含進行udp協(xié)議傳輸視頻的類。
- faces文件夾 每一個文件夾名字必須是英文字母,代表一個類,其每一個類別中可以有多張圖片,但數(shù)量過多,識別過慢。圖片必須是96*96大小的經(jīng)過對齊的jpg格式圖片。
- model文件夾 存放你訓(xùn)練的模型。
- gui.py文件 一些界面相關(guān)的函數(shù)。
- inference.py文件 神經(jīng)網(wǎng)絡(luò)函數(shù)。
- main.py文件
使用說明
- 點擊“打開本地攝像頭”按鈕,在“攝像頭采集信息”欄目中即會顯示攝像頭采集到的信息。
- 點擊“打開網(wǎng)絡(luò)攝像頭”按鈕,在“攝像頭采集信息”欄目中即會顯示通過WIFI傳輸?shù)降囊曨l信息。傳輸協(xié)議是UDP,目的是顯示樹莓派采集到的視頻。
- 在打開攝像頭后,點擊獲取人臉,左邊會顯示對齊后的人臉圖片,大小為96*96.第一次使用時,數(shù)據(jù)集為空,在獲得人臉圖片后,點擊新建人臉數(shù)據(jù)按鈕,輸入姓名,此時人臉圖片保存在faces文件夾下。
- 當(dāng)人臉圖片上出現(xiàn)圖片時,才可點擊“人臉識別”按鈕,點擊后會在人臉圖片下方顯示預(yù)測的姓名和歐式距離。歐式距離,值越小,表示越相似。當(dāng)距離大于一定閾值(默認為0.4),其姓名會顯示為unknown。
- 點擊“報錯”功能按鈕,把誤識的人臉存入對應(yīng)的人臉數(shù)據(jù)集中。例如:數(shù)據(jù)集中有5種人臉,分別標(biāo)號為1、2、3、4、5,采集人臉其實為5號,卻被誤識為1、2、3、4或者unknown,這時點擊“報錯”按鈕,把采集人臉存為5號人臉文件夾中即可。
- 閾值是影響人臉識別的關(guān)鍵因素,默認為0.4,可自行調(diào)整。
- 有2種模型可供下載,在model文件夾中可得下載鏈接
- 此種模型在微軟MS-Celeb-1M數(shù)據(jù)集上訓(xùn)練,在LFW上測試的準(zhǔn)確率可達87%以上。
- 此種模型在模型1的基礎(chǔ)上,在LFW上繼續(xù)訓(xùn)練,在LFW上測試的準(zhǔn)確率可達91%以上。
- 可以自己訓(xùn)練模型
部分源碼
main.py主函數(shù)文件:
import sys
import time
import cv2
import os
import numpy as np
from PyQt5 import QtWidgets, QtCore, QtGui
from face_lib import align_dlib, face_recg
from face_lib.udp_recv import UdpGetVideo
# import helpers
from gui import Ui_widget
class MyDesignerShow(QtWidgets.QWidget, Ui_widget):
_signal = QtCore.pyqtSignal(int)
def __init__(self):
super(MyDesignerShow, self).__init__()
self.timer_camera = QtCore.QTimer() # 本地攝像頭定時器
self.timer_udp_video = QtCore.QTimer() # UDP獲取視頻定時器
self.cap = cv2.VideoCapture() # 獲得攝像頭對象
self.CAM_NUM = 0 # 獲取攝像頭編號
self.time = time # 獲取時間對象
self.PREDICTOR_PATH = './face_lib/shape_predictor_68_face_landmarks.dat' # 關(guān)鍵點提取模型路徑
self.my_align = align_dlib.AlignDlib(self.PREDICTOR_PATH) # 獲取人臉對齊對象
self.pix = QtGui.QPixmap() # 獲取QPixmap對象
self.pic_show = None
self.face_photo = None # 人臉圖片
self.face_recog = face_recg.Recognize() # 獲取人臉識別對象
self.setupUi(self) # 加載窗體
self.btn_close.clicked.connect(self.close) # 關(guān)閉程序
self.btn_local_camera.clicked.connect(self.get_local_camera) # 打開本地相機
self.btn_web_camera.clicked.connect(self.get_udp_video) # 打開UDP視頻數(shù)據(jù)
self.btn_get_face.clicked.connect(self.get_face) # 得到人臉圖像
self.btn_debug.clicked.connect(self.debug) # 報錯
self.btn_new_face.clicked.connect(self.new_face) # 新建人臉數(shù)據(jù)
self.btn_face_recognize.clicked.connect(self.face_recognize) # 人臉識別
self.timer_camera.timeout.connect(self.show_local_camera) # 計時結(jié)束調(diào)用show_camera()方法
self.timer_udp_video.timeout.connect(self.show_udp_video) # 計時結(jié)束調(diào)用show_udp_video()方法
# 獲取本地攝像頭視頻
def get_local_camera(self):
if self.timer_udp_video.isActive(): # 查詢網(wǎng)絡(luò)攝像頭是否打開
QtWidgets.QMessageBox.warning(self, u"Warning", u"請先關(guān)閉網(wǎng)絡(luò)攝像頭", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
elif not self.timer_camera.isActive():
flag = self.cap.open(self.CAM_NUM)
if not flag:
QtWidgets.QMessageBox.warning(self, u"Warning", u"請檢測相機與電腦是否連接正確", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera.start(30) # 30ms刷新一次
self.btn_local_camera.setText(u'關(guān)閉本地攝像頭')
else:
self.timer_camera.stop() # 定時器關(guān)閉
self.cap.release() # 攝像頭釋放
self.label_camera.clear() # 視頻顯示區(qū)域清屏
self.graphicsView.show()
self.btn_local_camera.setText(u'打開本地攝像頭')
def show_local_camera(self):
flag, image = self.cap.read()
self.pic_show = cv2.resize(image, (640, 480))
self.pic_show = cv2.cvtColor(self.pic_show, cv2.COLOR_BGR2RGB)
showimage = QtGui.QImage(self.pic_show.data, self.pic_show.shape[1], self.pic_show.shape[0], QtGui.QImage.Format_RGB888)
self.graphicsView.close()
self.label_camera.setPixmap(self.pix.fromImage(showimage))
def get_udp_video(self):
if self.timer_camera.isActive(): # 查詢本地攝像頭
QtWidgets.QMessageBox.warning(self, u"Warning", u"請先關(guān)閉本地攝像頭", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
elif not self.timer_udp_video.isActive():
self.time.sleep(1)
self.udp_video = UdpGetVideo() # 獲取udp視頻對象
self.timer_udp_video.start(30) # 10ms刷新一次
self.btn_web_camera.setText(u'關(guān)閉網(wǎng)絡(luò)攝像頭')
else:
self.timer_udp_video.stop() # 定時器關(guān)閉
self.udp_video.close() # udp視頻接受關(guān)閉
self.label_camera.clear() # 視頻顯示區(qū)域清屏
self.graphicsView.show()
self.btn_web_camera.setText(u'打開網(wǎng)絡(luò)攝像頭')
def show_udp_video(self):
image = self.udp_video.receive()
# 從內(nèi)存緩存區(qū)中讀取圖像
decimg = cv2.imdecode(image, 1)
self.pic_show = cv2.resize(decimg, (640, 480))
self.pic_show = cv2.cvtColor(self.pic_show, cv2.COLOR_BGR2RGB)
showimage = QtGui.QImage(self.pic_show.data, self.pic_show.shape[1], self.pic_show.shape[0], QtGui.QImage.Format_RGB888)
self.graphicsView.close()
self.label_camera.setPixmap(self.pix.fromImage(showimage))
def get_face(self):
flag_cam = True
if not self.timer_camera.isActive() and not self.timer_udp_video.isActive(): # 查詢攝像頭
QtWidgets.QMessageBox.warning(self, u"Warning", u"請先打開攝像頭", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
flag_cam = False
if flag_cam:
pic = self.pic_show
if pic is not None:
# 使用dlib自帶的frontal_face_detector作為我們的特征提取器
face_align = self.my_align.align(96, pic)
if face_align is None:
QtWidgets.QMessageBox.warning(self, u"Warning", u"沒有檢測到人臉", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
face_align = cv2.cvtColor(face_align, cv2.COLOR_RGB2BGR) # 轉(zhuǎn)為BGR圖片
self.face_photo = face_align
face_align = cv2.cvtColor(face_align, cv2.COLOR_BGR2RGB) # 轉(zhuǎn)為RGB圖片
showimage = QtGui.QImage(face_align.data, face_align.shape[1], face_align.shape[0],
QtGui.QImage.Format_RGB888)
self.label_face.setPixmap(QtGui.QPixmap.fromImage(showimage))
else:
QtWidgets.QMessageBox.warning(self, u"Warning", u"沒有檢測到圖片", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
def face_recognize(self):
if self.face_photo is None:
QtWidgets.QMessageBox.warning(self, u"Warning", u"請先獲取人臉圖片", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.face_recog.reload_data() # 重載人臉數(shù)據(jù)集
names = self.face_recog.names
if len(names) < 1:
QtWidgets.QMessageBox.warning(self, u"Warning", u"數(shù)據(jù)集為空!", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
image_data = np.array(self.face_photo)
image_data = image_data.astype('float32') / 255.0
face_like = self.face_recog.whose_face(image_data) # 識別人臉
for i in range(len(names)):
print('姓名:%s 歐式距離: %s' % (names[i], face_like[i]))
self.textEdit.append("姓名:" + str(names[i]) + " 歐式距離: " + str(face_like[i]))
face_id, distance = self.face_recog.get_face_id(face_like)
if face_id is not None:
self.label_name.setText(str(names[face_id]))
else:
self.label_name.setText('unknown')
self.label_look.setText(str(distance))
def new_face(self):
text, ok = QtWidgets.QInputDialog.getText(self, '英文字符!', '請輸入你的英文名字:')
if ok:
print(text)
if self.face_photo is not None:
# 創(chuàng)建文件夾
paths = './faces/' + text + '/'
if not os.path.exists(paths):
os.makedirs(paths)
# 保存圖片
s_time = time.ctime().replace(' ', '_').replace(':', '_')
cv2.imwrite(str(paths) + str(s_time) + '.jpg', self.face_photo)
self.textEdit.append("人臉已存放在 " + paths + ' 文件夾中!!')
else:
QtWidgets.QMessageBox.warning(self, u"Warning", u"數(shù)據(jù)集中已有相同人名!",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
QtWidgets.QMessageBox.warning(self, u"Warning", u"輸入錯誤", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
def debug(self):
self.face_recog.reload_data() # 重載人臉數(shù)據(jù)集
num = self.face_recog.max_num
file_names = self.face_recog.names
print(file_names)
if num > 0:
result, ok = QtWidgets.QInputDialog.getItem(self, u"人臉數(shù)據(jù)校驗", u"把人臉數(shù)據(jù)存入對應(yīng)的文件夾中,可增加人臉識別的準(zhǔn)確性。確定把圖片存放在以下文件夾中嗎?",
file_names, 1, False)
if ok:
if self.face_photo is not None:
# 保存圖片
s_time = time.ctime().replace(' ', '_').replace(':', '_')
cv2.imwrite('./faces/' + result + '/' + str(s_time) + '.jpg', self.face_photo)
self.textEdit.append("已保存在./faces/" + result + '文件夾下!!')
else:
QtWidgets.QMessageBox.warning(self, u"Warning", u"數(shù)據(jù)集為空,請新建人臉數(shù)據(jù)!", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
def closeEvent(self, event):
ok = QtWidgets.QPushButton()
cacel = QtWidgets.QPushButton()
msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, u"關(guān)閉", u"是否關(guān)閉!")
msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
msg.addButton(cacel, QtWidgets.QMessageBox.RejectRole)
ok.setText(u'確定')
cacel.setText(u'取消')
if msg.exec_() == QtWidgets.QMessageBox.RejectRole:
event.ignore()
else:
if self.cap.isOpened():
self.cap.release()
if self.timer_camera.isActive():
self.timer_camera.stop()
if self.timer_udp_video.isActive():
self.timer_udp_video.stop()
event.accept()
if __name__ == "__main__":
if not os.path.exists("./faces"):
os.makedirs("./faces")
app = QtWidgets.QApplication(sys.argv)
myshow = MyDesignerShow() # 創(chuàng)建實例
myshow.show() # 使用Qidget的show()方法
sys.exit(app.exec_())
UI界面文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_widget(object):
def setupUi(self, widget):
widget.setObjectName("widget")
widget.resize(1024, 768)
widget.setMinimumSize(QtCore.QSize(1024, 768))
widget.setMaximumSize(QtCore.QSize(1024, 768))
self.label_camera = QtWidgets.QLabel(widget)
self.label_camera.setEnabled(True)
self.label_camera.setGeometry(QtCore.QRect(360, 40, 640, 480))
self.label_camera.setMinimumSize(QtCore.QSize(640, 480))
self.label_camera.setMaximumSize(QtCore.QSize(640, 480))
self.label_camera.setText("")
self.label_camera.setObjectName("label_camera")
self.textEdit = QtWidgets.QTextEdit(widget)
self.textEdit.setGeometry(QtCore.QRect(360, 550, 641, 181))
self.textEdit.setObjectName("textEdit")
self.graphicsView = QtWidgets.QGraphicsView(widget)
self.graphicsView.setGeometry(QtCore.QRect(360, 40, 640, 480))
self.graphicsView.setMinimumSize(QtCore.QSize(640, 480))
self.graphicsView.setMaximumSize(QtCore.QSize(640, 480))
self.graphicsView.setObjectName("graphicsView")
self.label_2 = QtWidgets.QLabel(widget)
self.label_2.setGeometry(QtCore.QRect(640, 20, 91, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(widget)
self.label_3.setGeometry(QtCore.QRect(630, 530, 111, 20))
self.label_3.setObjectName("label_3")
self.layoutWidget = QtWidgets.QWidget(widget)
self.layoutWidget.setGeometry(QtCore.QRect(40, 430, 281, 101))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.label_name = QtWidgets.QLabel(self.layoutWidget)
self.label_name.setText("")
self.label_name.setObjectName("label_name")
self.gridLayout.addWidget(self.label_name, 0, 1, 1, 1)
self.label_look = QtWidgets.QLabel(self.layoutWidget)
self.label_look.setText("")
self.label_look.setObjectName("label_look")
self.gridLayout.addWidget(self.label_look, 2, 1, 1, 1)
self.label_5 = QtWidgets.QLabel(self.layoutWidget)
self.label_5.setObjectName("label_5")
self.gridLayout.addWidget(self.label_5, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_face = QtWidgets.QLabel(widget)
self.label_face.setEnabled(True)
self.label_face.setGeometry(QtCore.QRect(50, 210, 96, 96))
self.label_face.setMinimumSize(QtCore.QSize(96, 96))
self.label_face.setMaximumSize(QtCore.QSize(96, 96))
self.label_face.setObjectName("label_face")
self.textEdit_2 = QtWidgets.QTextEdit(widget)
self.textEdit_2.setGeometry(QtCore.QRect(40, 550, 281, 181))
self.textEdit_2.setObjectName("textEdit_2")
self.layoutWidget1 = QtWidgets.QWidget(widget)
self.layoutWidget1.setGeometry(QtCore.QRect(40, 70, 281, 121))
self.layoutWidget1.setObjectName("layoutWidget1")
self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget1)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.btn_local_camera = QtWidgets.QPushButton(self.layoutWidget1)
self.btn_local_camera.setObjectName("btn_local_camera")
self.gridLayout_2.addWidget(self.btn_local_camera, 0, 0, 1, 1)
self.btn_web_camera = QtWidgets.QPushButton(self.layoutWidget1)
self.btn_web_camera.setObjectName("btn_web_camera")
self.gridLayout_2.addWidget(self.btn_web_camera, 1, 0, 1, 1)
self.btn_close = QtWidgets.QPushButton(self.layoutWidget1)
self.btn_close.setObjectName("btn_close")
self.gridLayout_2.addWidget(self.btn_close, 2, 0, 1, 1)
self.btn_get_face = QtWidgets.QPushButton(widget)
self.btn_get_face.setEnabled(True)
self.btn_get_face.setGeometry(QtCore.QRect(230, 220, 91, 41))
self.btn_get_face.setMinimumSize(QtCore.QSize(0, 0))
self.btn_get_face.setMaximumSize(QtCore.QSize(100, 60))
self.btn_get_face.setObjectName("btn_get_face")
self.btn_face_recognize = QtWidgets.QPushButton(widget)
self.btn_face_recognize.setGeometry(QtCore.QRect(230, 280, 91, 41))
self.btn_face_recognize.setMinimumSize(QtCore.QSize(30, 30))
self.btn_face_recognize.setMaximumSize(QtCore.QSize(16777215, 100))
self.btn_face_recognize.setObjectName("btn_face_recognize")
self.btn_new_face = QtWidgets.QPushButton(widget)
self.btn_new_face.setGeometry(QtCore.QRect(50, 350, 80, 31))
self.btn_new_face.setMaximumSize(QtCore.QSize(100, 100))
self.btn_new_face.setObjectName("btn_new_face")
self.btn_debug = QtWidgets.QPushButton(widget)
self.btn_debug.setGeometry(QtCore.QRect(230, 350, 91, 31))
self.btn_debug.setMaximumSize(QtCore.QSize(100, 100))
self.btn_debug.setObjectName("btn_debug")
self.retranslateUi(widget)
QtCore.QMetaObject.connectSlotsByName(widget)
def retranslateUi(self, widget):
_translate = QtCore.QCoreApplication.translate
widget.setWindowTitle(_translate("widget", "Form"))
self.label_2.setText(_translate("widget", "攝像頭采集信息"))
self.label_3.setText(_translate("widget", "調(diào)試信息顯示窗口"))
self.label_5.setText(_translate("widget", " 歐式距離:"))
self.label.setText(_translate("widget", " 預(yù)測姓名:"))
self.label_face.setText(_translate("widget", "<html><head/><body><p align="center"><span style=" font-size:14pt; vertical-align:sub;">人臉圖片</span></p></body></html>"))
self.textEdit_2.setHtml(_translate("widget", "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">n"
"<html><head><meta name="qrichtext" content="1" /><style type="text/css">n"
"p, li { white-space: pre-wrap; }n"
"</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt; font-weight:600;">說明:</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">1.初次使用軟件,請確保model文件夾存有訓(xùn)練好的權(quán)重</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">2.初次使用請新建人臉數(shù)據(jù),名字必須為英文字符</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">3.歐式距離越大,代表越不相似,越小代表越相似</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">4.每次點擊</span><span style=" font-size:8pt; font-weight:600; text-decoration: underline;">人臉識別</span><span style=" font-size:8pt;">前都必須先</span><span style=" font-size:8pt; font-weight:600; text-decoration: underline;">獲取人臉</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt; font-weight:600;">步驟:</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">1.打開本地攝像頭或者網(wǎng)絡(luò)攝像頭</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">2.點擊獲取人臉</span></p>n"
"<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">3.點擊人臉識別</span></p></body></html>"))
self.btn_local_camera.setText(_translate("widget", "打開本地攝像頭"))
self.btn_web_camera.setText(_translate("widget", "打開網(wǎng)絡(luò)攝像頭"))
self.btn_close.setText(_translate("widget", "關(guān)閉程序"))
self.btn_get_face.setText(_translate("widget", "獲取人臉"))
self.btn_face_recognize.setText(_translate("widget", "人臉識別"))
self.btn_new_face.setText(_translate("widget", "新建人臉數(shù)據(jù)"))
self.btn_debug.setText(_translate("widget", "報錯"))
博客主頁:https://blog.csdn.net/weixin_51141489,需要源碼或相關(guān)資料實物的友友請關(guān)注、點贊,私信吧!
閱讀全文