Python人脸识别考勤打卡系统
Python人臉識別考勤打卡系統
如需安裝運行環境或遠程調試,可加QQ905733049,?或QQ2945218359由專業技術人員遠程協助!
運行結果如下:
?主要代碼:
import random import cv2 import numpy import datetime import os import time import cv2 as cv import numpy as np import threading from PIL import Image, ImageFont, ImageDrawdatabase_file_path = './resources/data.db' picture_dir_path = "./resources/pictures" trainer_file_path = './resources/Trainer/trainer.yml' font_file_path = './resources/simsun.ttc' zsc_circle_file_path = './resources/zsc.jpg' zsc_rectangle_file_path = './resources/zsc.png' haarcascade_frontalface_file_path = './resources/haarcascade_frontalface_default.xml' capture_opt = 0 # 攝像頭參數,0,1為本機攝像頭# 繼承wx庫里面的Frame類來使用 class MainFrame(wx.Frame):def __init__(self):# 初始化窗體數據wx.Frame.__init__(self, None, -1, '人臉識別考勤系統', pos=(100, 100), size=(1337, 600))panel = wx.Panel(self, -1)sizer = wx.BoxSizer(wx.HORIZONTAL)sizer1 = wx.BoxSizer(wx.VERTICAL)sizer2 = wx.BoxSizer(wx.VERTICAL)font = wx.Font(15, wx.ROMAN, wx.NORMAL, wx.BOLD)# 設置窗口以及托盤圖標icon = wx.Icon()icon.CopyFromBitmap(wx.Bitmap(wx.Image((zsc_circle_file_path), wx.BITMAP_TYPE_JPEG)))self.SetIcon(icon)# 設置左邊背景學院logoimage = wx.Image(zsc_rectangle_file_path, wx.BITMAP_TYPE_PNG).ConvertToBitmap()self.background = wx.StaticBitmap(panel, -1, bitmap=image, style=wx.ALIGN_CENTER)sizer1.Add(self.background, proportion=10, flag=wx.ALIGN_CENTER, border=10)# 設置采集人臉按鈕self.command1 = wx.Button(panel, -1, '采集人臉')self.command1.SetFont(font)self.command1.SetBackgroundColour('#3299CC')sizer1.Add(self.command1, proportion=5, flag=wx.ALL | wx.EXPAND, border=10)# 設置訓練數據按鈕self.command2 = wx.Button(panel, -1, '訓練數據')self.command2.SetFont(font)self.command2.SetBackgroundColour('#DBDB70')sizer1.Add(self.command2, proportion=5, flag=wx.ALL | wx.EXPAND, border=10)# 設置人臉識別按鈕self.command3 = wx.Button(panel, -1, '識別打卡')self.command3.SetFont(font)self.command3.SetBackgroundColour('#32CC32')sizer1.Add(self.command3, proportion=5, flag=wx.ALL | wx.EXPAND, border=10)# 設置退出系統按鈕self.command4 = wx.Button(panel, -1, '關閉攝像頭')self.command4.SetFont(font)self.command4.SetBackgroundColour((random.randint(1, 255), random.randint(0, 255), random.randint(0, 255)))sizer1.Add(self.command4, proportion=5, flag=wx.ALL | wx.EXPAND, border=10)# 設置消息提示文本self.text5 = wx.StaticText(panel, -1, '\n\n', style=wx.ALIGN_CENTER)self.text5.SetFont(font)self.text5.SetForegroundColour('Red')sizer1.Add(self.text5, proportion=15, flag=wx.ALL | wx.EXPAND, border=10)# 設置個人信息文本self.text6 = wx.StaticText(panel, -1, '姓名:')self.text7 = wx.StaticText(panel, -1, '學號:')self.text8 = wx.StaticText(panel, -1, '學院:')sizer1.Add(self.text6, proportion=3, flag=wx.LEFT, border=0)sizer1.Add(self.text7, proportion=3, flag=wx.LEFT, border=0)sizer1.Add(self.text8, proportion=3, flag=wx.LEFT, border=0)# 把分布局全部加入整體頂級布局sizer.Add(sizer1, flag=wx.EXPAND | wx.ALL, border=20)# 設置右上邊消息提示文本sizer3 = wx.BoxSizer(wx.HORIZONTAL)font = wx.Font(12, wx.ROMAN, wx.NORMAL, wx.BOLD)self.text9 = wx.StaticText(panel, -1, '', style=wx.ALIGN_LEFT)self.text9.SetFont(font)self.text9.SetForegroundColour('brown')self.text9.SetLabel(u''+'您好,歡迎使用人臉考勤系統!')self.text10 = wx.StaticText(panel, -1, '', style=wx.ALIGN_RIGHT)self.text10.SetFont(font)self.text10.SetForegroundColour('Blue')self.data_num = 0self.updateSumData()sizer3.Add(self.text9, proportion=1, flag= wx.ALL|wx.EXPAND, border=10)sizer3.Add(self.text10, proportion=1, flag= wx.ALL|wx.EXPAND, border=10)sizer2.Add(sizer3, proportion=1, flag=wx.EXPAND | wx.ALL, border=0)# 封面圖片self.image_cover = wx.Image(zsc_circle_file_path, wx.BITMAP_TYPE_ANY).Scale(575, 460)self.bmp = wx.StaticBitmap(panel, -1, wx.Bitmap(self.image_cover))sizer2.Add(self.bmp, proportion=1, flag=wx.ALL|wx.EXPAND ,border=0)# 加入頂級布局sizer.Add(sizer2, flag=wx.EXPAND | wx.ALL, border=10)# 實例化數據庫操作對象self.mySqlDao = MySQLDao(self)self.grid = MyGrid(panel, self.mySqlDao)self.grid.updateGrid()# 打卡記錄表sizer.Add(self.grid, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)panel.SetSizer(sizer)# 四個按鈕對應的事件self.command1.Bind(wx.EVT_BUTTON, self.command1_event)self.command4.Bind(wx.EVT_BUTTON, self.command4_event)self.command3.Bind(wx.EVT_BUTTON, self.command3_event)self.command2.Bind(wx.EVT_BUTTON, self.command2_event)# 關閉事件self.Bind(wx.EVT_CLOSE,self.onClose)# 窗口居中,顯示self.Center()self.Show()# 控制攝像頭的開啟與關閉self.recognition = Falseself.collected = False'''主窗體關閉事件'''def onClose(self,event):self.command4_event(event)# 等待子線程完成 再關閉,防止不正常退出time.sleep(1)self.Destroy()'''采集數據按鈕的響應事件'''def command1_event(self, event):self.text6.SetLabel('姓名:')self.text7.SetLabel('學號:')self.text8.SetLabel('學院:')self.text5.SetLabel(u'\n溫馨提示:\n' + '?正在進學生信息錄入...')self.text5.Update()collectFrame = CollectFrame(self,self.mySqlDao)collectFrame.Show()'''訓練數據按鈕的響應事件'''def command2_event(self, event):self.trainData()'''識別打卡按鈕的響應事件'''def command3_event(self, event):self.text5.SetLabel(u'')self.recognition = Falset1 = threading.Thread(target=self.recognitionFace)t1.start()'''關閉攝像頭按鈕的響應事件'''def command4_event(self, event):if self.collected == False:self.collected = Trueif self.recognition == False:self.recognition = Truedef updateSumData(self):self.data_num = 0for list in os.listdir(picture_dir_path):if len(os.listdir(picture_dir_path + '/' + list)) >= 200:self.data_num += 1self.text10.SetLabel(u'當前已采集人臉數據的人數:' + str(self.data_num))self.text10.Update()'''@Author:Himit_ZH@Function:處理收集人臉每一幀生成圖片存入對應文件夾'''def collect(self,face_id):self.text5.SetLabel(u'\n溫馨提示:\n' + '請看向攝像頭\n準備采集200張人臉圖片...')count = 0 # 統計照片數量path = picture_dir_path+"/Stu_" + str(face_id) # 人臉圖片數據的儲存路徑# 讀取視頻cap = cv.VideoCapture(capture_opt)print(cap.isOpened())if cap.isOpened() == False:self.text5.SetLabel(u'\n錯誤提示:\n' + '×采集人臉數據失敗!\n原因:未能成功打開攝像頭')return# 加載特征數據face_detector = cv.CascadeClassifier(haarcascade_frontalface_file_path)if not os.path.exists(path): # 如果沒有對應文件夾,自動生成os.makedirs(path)while self.collected == False:flag, frame = cap.read()# print('flag:',flag,'frame.shape:',frame.shape)if not flag:break# 將圖片灰度gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)faces = face_detector.detectMultiScale(gray, 1.1, 3)if len(faces) > 1: # 一幀出現兩張照片丟棄,原因:有人亂入,也有可能人臉識別出現差錯continue# 框選人臉,for循環保證一個能檢測的實時動態視頻流for x, y, w, h in faces:cv.rectangle(frame, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)count += 1cv.imwrite(path + '/' + str(count) + '.png', gray[y:y + h, x:x + w])# # 顯示圖片# cv.imshow('Camera', frame)# 將一幀幀圖片顯示在UI中image1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)image2 = cv2.resize(image1, (575, 460))height, width = image2.shape[:2]pic = wx.Bitmap.FromBuffer(width, height, image2)# 顯示圖片在panel上self.bmp.SetBitmap(pic)self.bmp.Update()self.text9.SetLabel(u'溫馨提示:\n' + '已采集'+ str(count)+'張人臉照片')if count >= 200: #默認采集200張照片break# 關閉資源if count >=200:self.text5.SetLabel(u'\n溫馨提示:\n' + '?已成功采集到人臉數據!')self.updateSumData()else:self.text5.SetLabel(u'\n錯誤提示:\n' + '×采集人臉數據失敗!\n未能收集到200張人臉數據!')# 刪除該文件夾下的所有數據ls = os.listdir(path)for file_path in ls:f_path = os.path.join(path, file_path)os.remove(f_path)os.rmdir(path)cv.destroyAllWindows()cap.release()self.bmp.SetBitmap(wx.Bitmap(self.image_cover))'''@Author:Himit_ZH@Function:遍歷指定文件夾里面的人臉數據,根據文件夾名字,訓練對應數據'''def trainData(self):self.text5.SetLabel(u'\n溫馨提示:\n' + '?正在整合訓練的人臉數據\n請稍后...')# 圖片路徑path = picture_dir_pathfacesSamples = []imageFiles = []ids = []for root, dirs, files in os.walk(path):# root 表示當前正在訪問的文件夾路徑# dirs 表示該文件夾下的子目錄名list# files 表示該文件夾下的文件list# 遍歷文件for file in files:imageFiles.append(os.path.join(root, file))# 檢測人臉的模型數據face_detector = cv2.CascadeClassifier(haarcascade_frontalface_file_path)# 遍歷列表中的圖片stu_map = self.mySqlDao.getIdfromSql()for imagefile in imageFiles: # 獲得所有文件名字imagefile = imagefile.replace('\\', '/')sno = imagefile.split('/')[3].split('_')[1]if stu_map.get(sno):PIL_img = Image.open(imagefile).convert('L') # 打開圖片并且轉為灰度圖片# 將圖像轉換為數組img_numpy = np.array(PIL_img, 'uint8')faces = face_detector.detectMultiScale(img_numpy)for x, y, w, h in faces:facesSamples.append(img_numpy[y:y + h, x:x + w])ids.append(int(stu_map.get(sno)))if __name__ == '__main__':app = wx.App()app.locale = wx.Locale(wx.LANGUAGE_CHINESE_SIMPLIFIED)frame = MainFrame()app.MainLoop()?運行結果如下:
?
Python, C++, PHP語言學習參考實例連接:
C++學習參考實例:
C++實現圖形界面五子棋游戲源碼:
https://blog.csdn.net/alicema1111/article/details/90035420
C++實現圖形界面五子棋游戲源碼2:
https://blog.csdn.net/alicema1111/article/details/106479579
C++ OpenCV相片視頻人臉識別統計人數:
https://blog.csdn.net/alicema1111/article/details/105833928
VS2017+PCL開發環境配置:
https://blog.csdn.net/alicema1111/article/details/106877145
VS2017+Qt+PCL點云開發環境配置:
https://blog.csdn.net/alicema1111/article/details/105433636
C++ OpenCV汽車檢測障礙物與測距:
https://blog.csdn.net/alicema1111/article/details/105833449
Windows VS2017安裝配置PCL點云庫:
https://blog.csdn.net/alicema1111/article/details/105111110
VS+VTK+Dicom(dcm)+CT影像切片窗體界面顯示源碼
https://blog.csdn.net/alicema1111/article/details/106994839
Python學習參考實例:
Python相片更換背景顏色qt窗體程序:
https://blog.csdn.net/alicema1111/article/details/106919140
OpenCV汽車識別檢測數量統計:
https://blog.csdn.net/alicema1111/article/details/106597260
OpenCV視頻識別檢測人數跟蹤統計:
https://blog.csdn.net/alicema1111/article/details/106113042
OpenCV米粒檢測數量統計:
https://blog.csdn.net/alicema1111/article/details/106089697
opencv人臉識別與變形哈哈鏡:
https://blog.csdn.net/alicema1111/article/details/105833123
OpenCV人臉檢測打卡系統:
https://blog.csdn.net/alicema1111/article/details/105315066
Python+OpenCV攝像頭人臉識別:
https://blog.csdn.net/alicema1111/article/details/105107286
Python+Opencv識別視頻統計人數:
https://blog.csdn.net/alicema1111/article/details/103804032
Python+OpenCV圖像人臉識別人數統計:
https://blog.csdn.net/alicema1111/article/details/105378639
python人臉頭發身體部位識別人數統計:
https://blog.csdn.net/alicema1111/article/details/116424942
VS+QT+VTK三維網格圖像顯示GUI
https://blog.csdn.net/alicema1111/article/details/117060734
PHP網頁框架:
PHP Laravel框架安裝與配置后臺管理前臺頁面顯示:
https://blog.csdn.net/alicema1111/article/details/106686523
總結
以上是生活随笔為你收集整理的Python人脸识别考勤打卡系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino测量误差数据的处理——莱特
- 下一篇: kettle下载windows共享文件夹