-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
90 lines (77 loc) · 2.77 KB
/
detector.py
File metadata and controls
90 lines (77 loc) · 2.77 KB
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
import cv2
import mysql.connector
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
# connect to db
def connectdb():
return mysql.connector.connect(host="localhost",user="root",password="12345",database="attendance_db")
#get next id
def nextid():
conn = connectdb()
cursor = conn.cursor()
cursor.execute("SELECT MAX(id) FROM attendance")
result = cursor.fetchone()
conn.close()
if result[0] is not None:
return (result[0] + 1)
else:
return 1
# save attendance
def saveattend(personid):
conn = connectdb()
cursor = conn.cursor()
cursor.execute("INSERT INTO attendance (name) VALUES (%s)", (f"Person {personid}",))
conn.commit()
conn.close()
#load facedetection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# find faces
def detectface(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
#show video feed
def updateframe():
ret, frame = cap.read()
if ret:
global lastframe
lastframe = frame
detectedfaces = detectface(frame)
#draw rectangle
for (x, y, w, h) in detectedfaces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
#show in tkinter
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, updateframe) #keep updating frames
#code for button
def markattend():
faces = detectface(lastframe)
if len(faces) > 0:
personid = nextid()
saveattend(personid)
status_label.config(text=f"Status: Attendance recorded for Person {personid}")
messagebox.showinfo("Success", f"Attendance recorded for Person {personid}")
else:
messagebox.showwarning("Warning", "No faces detected. Attendance not recorded.")
status_label.config(text=f"No faces detected. Attendance not recorded.")
# tkinter window
root = tk.Tk()
root.title("Attendance System")
root.geometry("900x700")
root.configure(bg="#f0f0f0")
video_label = tk.Label(root)
video_label.pack(pady=20)
status_label = tk.Label(root, text="Status: Waiting for action", font=("Arial", 14), bg="#f0f0f0")
status_label.pack(pady=10)
start_button = tk.Button(root, text="Start Recognition", command=markattend, font=("Arial", 14), bg="#4CAF50", fg="white")
start_button.pack(pady=20)
#srtart capturing
cap = cv2.VideoCapture(0)
last_frame = None
updateframe()
root.mainloop()
cap.release()