最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python+OpenCV人臉識別考勤系統(tǒng)實現的詳細代碼

 更新時間:2023年05月22日 08:55:12   作者:A等天晴  
作為一個基于人臉識別算法的考勤系統(tǒng)的設計與實現教程,以下內容將提供詳細的步驟和代碼示例。本教程將使用 Python 語言和 OpenCV 庫進行實現,需要的朋友可以參考下

一、環(huán)境配置

  • 安裝 Python

請確保您已經安裝了 Python 3.x??梢栽赑ython 官網下載并安裝。

  • 安裝所需庫

在命令提示符或終端中運行以下命令來安裝所需的庫:

pip install opencv-python
pip install opencv-contrib-python
pip install numpy
pip install face-recognition

二、創(chuàng)建數據集

  • 創(chuàng)建文件夾結構

在項目目錄下創(chuàng)建如下文件夾結構:

attendance-system/
├── dataset/
│   ├── person1/
│   ├── person2/
│   └── ...
└── src/

將每個人的照片放入對應的文件夾中,例如:

attendance-system/
├── dataset/
│   ├── person1/
│   │   ├── 01.jpg
│   │   ├── 02.jpg
│   │   └── ...
│   ├── person2/
│   │   ├── 01.jpg
│   │   ├── 02.jpg
│   │   └── ...
│   └── ...
└── src/

三、實現人臉識別算法

在 src 文件夾下創(chuàng)建一個名為 face_recognition.py 的文件,并添加以下代碼:

import os
import cv2
import face_recognition
import numpy as np

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder, filename))
        if img is not None:
            images.append(img)
    return images

def create_known_face_encodings(root_folder):
    known_face_encodings = []
    known_face_names = []
    for person_name in os.listdir(root_folder):
        person_folder = os.path.join(root_folder, person_name)
        images = load_images_from_folder(person_folder)
        for image in images:
            face_encoding = face_recognition.face_encodings(image)[0]
            known_face_encodings.append(face_encoding)
            known_face_names.append(person_name)
    return known_face_encodings, known_face_names

def recognize_faces_in_video(known_face_encodings, known_face_names):
    video_capture = cv2.VideoCapture(0)
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True

    while True:
        ret, frame = video_capture.read()
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        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)
                name = "Unknown"

                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]

                face_names.append(name)

        process_this_frame = not process_this_frame

        for (top, right, bottom, left), name in zip(face_locations, face_names):
            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, 0.8, (255, 255, 255), 1)

        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    dataset_folder = "../dataset/"
    known_face_encodings, known_face_names = create_known_face_encodings(dataset_folder)
    recognize_faces_in_video(known_face_encodings, known_face_names)

四、實現考勤系統(tǒng)

在 src 文件夾下創(chuàng)建一個名為 attendance.py 的文件,并添加以下代碼:

import os
import datetime
import csv
from face_recognition import create_known_face_encodings, recognize_faces_in_video

def save_attendance(name):
    attendance_file = "../attendance/attendance.csv"
    now = datetime.datetime.now()
    date_string = now.strftime("%Y-%m-%d")
    time_string = now.strftime("%H:%M:%S")
    if not os.path.exists(attendance_file):
        with open(attendance_file, "w", newline="") as csvfile:
            csv_writer = csv.writer(csvfile)
            csv_writer.writerow(["Name", "Date", "Time"])
    with open(attendance_file, "r+", newline="") as csvfile:
        csv_reader = csv.reader(csvfile)
        rows = [row for row in csv_reader]
        for row in rows:
            if row[0] == name and row[1] == date_string:
                return
        csv_writer = csv.writer(csvfile)
        csv_writer.writerow([name, date_string, time_string])

def custom_recognize_faces_in_video(known_face_encodings, known_face_names):
    video_capture = cv2.VideoCapture(0)
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    while True:
        ret, frame = video_capture.read()
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        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)
                name = "Unknown"
                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]
                    save_attendance(name)
                face_names.append(name)
        process_this_frame = not process_this_frame
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            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, 0.8, (255, 255, 255), 1)

        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    dataset_folder = "../dataset/"
    known_face_encodings, known_face_names = create_known_face_encodings(dataset_folder)
    custom_recognize_faces_in_video(known_face_encodings, known_face_names)

五、運行考勤系統(tǒng)

運行 attendance.py 文件,系統(tǒng)將開始識別并記錄考勤信息。考勤記錄將保存在 attendance.csv 文件中。

python src/attendance.py

現在,您的基于人臉識別的考勤系統(tǒng)已經實現。請注意,這是一個基本示例,您可能需要根據實際需求對其進行優(yōu)化和擴展。例如,您可以考慮添加更多的人臉識別算法、考勤規(guī)則等。

到此這篇關于python+OpenCV人臉識別考勤系統(tǒng)實現的詳細代碼的文章就介紹到這了,更多相關python OpenCV人臉識別考勤系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • peewee創(chuàng)建連接前的前置操作wireshark抓包實現

    peewee創(chuàng)建連接前的前置操作wireshark抓包實現

    這篇文章主要為大家介紹了peewee創(chuàng)建連接前的前置操作wireshark?抓包實現示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • python多線程抽象編程模型詳解

    python多線程抽象編程模型詳解

    這篇文章主要為大家詳細介紹了python多線程抽象編程模型,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 使用sklearn之LabelEncoder將Label標準化的方法

    使用sklearn之LabelEncoder將Label標準化的方法

    今天小編就為大家分享一篇使用sklearn之LabelEncoder將Label標準化的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題

    解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題

    今天小編就為大家分享一篇解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 11行Python代碼實現解密摩斯密碼

    11行Python代碼實現解密摩斯密碼

    摩爾斯電碼是一種時通時斷的信號代碼,通過不同的排列順序來表達不同的英文字母、數字和標點符號。本文將通過Python代碼來實現解密摩斯密碼,感興趣的可以學習一下
    2022-04-04
  • PyQt5每天必學之QSplitter實現窗口分隔

    PyQt5每天必學之QSplitter實現窗口分隔

    這篇文章主要介紹了PyQt5每天必學之窗口分隔,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 利用 Monkey 命令操作屏幕快速滑動

    利用 Monkey 命令操作屏幕快速滑動

    Monkey測試是Android平臺自動化測試的一種手段,通過Monkey程序模擬用戶觸摸屏幕、滑動Trackball、按鍵等操作來對設備上的程序進行壓力測試,檢測程序多久的時間會發(fā)生異常
    2016-12-12
  • Python使用PyShark分析網絡流量的腳本分享

    Python使用PyShark分析網絡流量的腳本分享

    作為一名網絡安全工程師,是常常需要自己編寫一些實用的腳本的,下面是一個使用 PyShark 庫編寫的網絡流量分析腳本,感興趣的小伙伴可以了解下
    2025-08-08
  • Python技法之如何用re模塊實現簡易tokenizer

    Python技法之如何用re模塊實現簡易tokenizer

    當我們在Python中開始新的東西時,我通常首先看一些模塊或庫來使用,下面這篇文章主要給大家介紹了關于Python技法之如何用re模塊實現簡易tokenizer的相關資料,需要的朋友可以參考下
    2022-05-05
  • django中資源文件夾的引入及配置方法

    django中資源文件夾的引入及配置方法

    這篇文章主要介紹了django中資源文件夾的引入,主要包括靜態(tài)資源文件夾的引入及媒體資源文件夾的引入,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08

最新評論

镇宁| 台中县| 达尔| 调兵山市| 北票市| 贺兰县| 常山县| 宝坻区| 芮城县| 鄄城县| 新昌县| 邹平县| 江北区| 通州区| 鹿邑县| 张家界市| 湟中县| 永新县| 宜黄县| 昂仁县| 突泉县| 鄂托克旗| 台江县| 南江县| 南涧| 敦煌市| 凤凰县| 贡山| 陆川县| 闻喜县| 湘西| 兖州市| 临海市| 曲阜市| 东平县| 大足县| 镇雄县| 祁连县| 大安市| 灯塔市| 读书|