-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace_Recognition.py
84 lines (68 loc) · 3.02 KB
/
Face_Recognition.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
# pip install cmake
# pip install face_recognition
# pip install opencv-python
# pip install numpy
import face_recognition
import cv2
import numpy as np
import csv
from datetime import datetime
import pyttsx3
# Load known faces
ashwin_image = face_recognition.load_image_file("faces/ashwin.jpg")
ashwin_encoding = face_recognition.face_encodings(ashwin_image)[0]
rishi_image = face_recognition.load_image_file("faces/rishi.jpg")
rishi_encoding = face_recognition.face_encodings(rishi_image)[0]
known_face_encodings = [ashwin_encoding, rishi_encoding]
known_face_names = ["Ashwin", "Rishi"]
# List of expected students
students = known_face_names.copy()
# Initialize video capture from webcam
video_capture = cv2.VideoCapture(0)
# Get the current date
current_date = datetime.now().strftime("%Y-%m-%d")
# Open CSV file for writing attendance
csv_filename = f"{current_date}.csv"
with open(csv_filename, "w", newline="") as csvfile:
csv_writer = csv.writer(csvfile)
# Initialize font
font = cv2.FONT_HERSHEY_SIMPLEX
# Initialize the speech engine
engine = pyttsx3.init()
while True:
_, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
# Recognize faces
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
for face_encoding in face_encodings:
if len(face_encoding) > 0: # Ensure there's at least one face encoding
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distance)
if matches[best_match_index]:
name = known_face_names[best_match_index]
# Add the text if a person is present
if name in known_face_names:
bottomLeftCornerOfText = (10, 50)
fontScale = 1
FontColor = (255, 255, 255)
thickness = 2
lineType = 2
cv2.putText(frame, f"{name} present", bottomLeftCornerOfText, font, fontScale, FontColor, thickness, lineType)
if name in students:
students.remove(name)
current_time = datetime.now().strftime("%H:%M:%S")
csv_writer.writerow([name, current_time])
engine.say("Thank you")
engine.runAndWait()
else:
engine.say("Please try again")
engine.runAndWait()
cv2.imshow("Attendance", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Release video capture and close windows
video_capture.release()
cv2.destroyAllWindows()