-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
106 lines (75 loc) · 2.79 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import tkinter as tk
from tkinter import ttk
import cv2
import mysql.connector
from PIL import Image, ImageTk
import time
def connectdb():
return mysql.connector.connect(
host="localhost",
user="root",
password="12345",
database="attendance_db"
)
def getdata():
conn = connectdb()
cursor = conn.cursor()
cursor.execute("SELECT * FROM attendance")
rows = cursor.fetchall()
conn.close()
return rows
def saveattend(name):
conn = connectdb()
cursor = conn.cursor()
cursor.execute("INSERT INTO attendance (name) VALUES (%s)", (name,))
conn.commit()
conn.close()
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
last_seen = {}
delay = 10
def detect_face(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
return faces
root = tk.Tk()
root.title("Face Detection and Attendance Viewer")
root.geometry("1200x700")
root.configure(bg="#f0f0f0")
face_frame = tk.Frame(root, width=600, height=700)
face_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
viewer_frame = tk.Frame(root, width=600, height=700)
viewer_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
tree = ttk.Treeview(viewer_frame, columns=("ID", "Name", "Timestamp"), show='headings')
tree.heading("ID", text="ID")
tree.heading("Name", text="Name")
tree.heading("Timestamp", text="Timestamp")
tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
def load_data():
for row in getdata():
tree.insert("", tk.END, values=row)
cap = cv2.VideoCapture(0)
def update_frame():
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detect_face(frame)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image=image)
video_label.config(image=photo)
video_label.image = photo
root.after(10, update_frame)
video_label = tk.Label(face_frame)
video_label.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
status_label = tk.Label(face_frame, text="Status: Not Recognizing", font=("Arial", 14), bg="#f0f0f0")
status_label.pack(pady=10)
start_button = tk.Button(face_frame, text="Start Recognition", command=lambda: status_label.config(text="Status: Recognizing Faces"))
start_button.pack(pady=20)
view_button = tk.Button(viewer_frame, text="Refresh Data", command=load_data)
view_button.pack(pady=10)
load_data()
update_frame()
root.mainloop()
cap.release()