1
+ # Generalised the code for import of names and face_encodings
2
+ # Unknown faces are now saved in a folder named unknown_faces in the project directory
3
+ # Fixed the issue where time of the recognition was same for all the faces recognised
4
+ # Follow the dictionary format in store1.csv ie "name" "image_path" if you import your own file
5
+ import cv2
6
+ import csv
7
+ import face_recognition
8
+ import numpy as np
9
+ import datetime
10
+ import os
11
+
12
+ # Create a folder for unknown faces if it doesn't exist already
13
+ unknown_faces_folder = "unknown_faces"
14
+ os .makedirs (unknown_faces_folder , exist_ok = True )
15
+
16
+ video_capture = cv2 .VideoCapture (0 )
17
+
18
+ # Specify the folder location and csv file location
19
+ database_folder = "paste your database path or the file name if it exists in this projects directory"
20
+ csv_file = "paste your csv file path or the file name if it exists in this projects directory"
21
+
22
+ # Store the faceencodings and names of the people in the database
23
+ known_face_encodings = []
24
+ known_face_names = []
25
+
26
+ # Read the CSV file and load face encodings
27
+ with open (csv_file , newline = '' ) as csvfile :
28
+ reader = csv .DictReader (csvfile )
29
+ for row in reader :
30
+ image_path = os .path .join (database_folder , row ['image_path' ])
31
+ image = face_recognition .load_image_file (image_path )
32
+ face_encoding = face_recognition .face_encodings (image )[0 ]
33
+ known_face_encodings .append (face_encoding )
34
+ known_face_names .append (row ['name' ])
35
+
36
+ # Creating a copy of the names list
37
+ students = known_face_names .copy ()
38
+
39
+ # Get the current date for the CSV filename
40
+ current_date = datetime .datetime .now ().strftime ("%Y-%m-%d" )
41
+
42
+ # Open the CSV file for writing, or create it
43
+ f = open (f"{ current_date } .csv" , "w+" , newline = "" )
44
+ lnwriter = csv .writer (f )
45
+
46
+ # Initialize a counter for unknown faces
47
+ unknown_face_counter = 1
48
+
49
+ #Check if the unknown faces in the webcam feed are already in the unknown folder
50
+ def is_face_unique (face_encoding , folder_path ):
51
+ for filename in os .listdir (folder_path ):
52
+ if filename .endswith (".jpg" ):
53
+ saved_image = face_recognition .load_image_file (os .path .join (folder_path , filename ))
54
+ saved_face_encodings = face_recognition .face_encodings (saved_image )
55
+ if saved_face_encodings :
56
+ saved_face_encoding = saved_face_encodings [0 ]
57
+ matches = face_recognition .compare_faces ([saved_face_encoding ], face_encoding )
58
+ if matches [0 ]:
59
+ return False
60
+ return True
61
+
62
+ # Explained in the last version
63
+ while True :
64
+ ret , frame = video_capture .read ()
65
+ rgb_frame = frame [:, :, ::- 1 ]
66
+
67
+ face_locations = face_recognition .face_locations (rgb_frame )
68
+ face_encodings = face_recognition .face_encodings (rgb_frame , face_locations )
69
+
70
+ for (top , right , bottom , left ), face_encoding in zip (face_locations , face_encodings ):
71
+ matches = face_recognition .compare_faces (known_face_encodings , face_encoding )
72
+
73
+ name = "Unknown"
74
+
75
+ face_distance = face_recognition .face_distance (known_face_encodings , face_encoding )
76
+ best_match_index = np .argmin (face_distance )
77
+ if matches [best_match_index ]:
78
+ name = known_face_names [best_match_index ]
79
+
80
+ cv2 .rectangle (frame , (left , top ), (right , bottom ), (0 , 0 , 255 ), 2 )
81
+
82
+ if name == "Unknown" :
83
+ # Add the unknown faces not already in the unknown folder to the unknown folder
84
+ if is_face_unique (face_encoding , unknown_faces_folder ):
85
+ face_image = frame [top :bottom , left :right ]
86
+ face_filename = f"{ unknown_faces_folder } /unknown{ unknown_face_counter } .jpg"
87
+ cv2 .imwrite (face_filename , face_image )
88
+ unknown_face_counter += 1
89
+
90
+ cv2 .rectangle (frame , (left , bottom - 35 ), (right , bottom ), (0 , 0 , 255 ), cv2 .FILLED )
91
+ font = cv2 .FONT_HERSHEY_DUPLEX
92
+ cv2 .putText (frame , name , (left + 6 , bottom - 6 ), font , 0.6 , (255 , 255 , 255 ), 1 )
93
+
94
+ if name in students :
95
+ students .remove (name )
96
+ current_time = datetime .datetime .now ().strftime ("%H:%M:%S" )
97
+ lnwriter .writerow ([name , current_time ])
98
+
99
+ cv2 .imshow ('Video' , frame )
100
+
101
+ if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
102
+ break
103
+
104
+ video_capture .release ()
105
+ cv2 .destroyAllWindows ()
106
+ f .close ()
0 commit comments