Vikas01 commited on
Commit
bbcce29
·
1 Parent(s): 9844a47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -53
app.py CHANGED
@@ -1,72 +1,130 @@
 
 
1
  import face_recognition
2
  import cv2
3
  import numpy as np
4
  import csv
5
  from datetime import datetime
6
 
7
- video_capture = cv2.VideoCapture(1)
 
 
8
 
9
- sir_image = face_recognition.load_image_file("photos/sir.jpeg")
10
- sir_encoding = face_recognition.face_encodings(sir_image)[0]
 
11
 
12
- vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
13
- vikas_encoding = face_recognition.face_encodings(vikas_image)[0]
14
 
15
- known_face_encoding = [sir_encoding, vikas_encoding]
16
- known_faces_names = ["Sarwan Sir", "Vikas"]
 
 
 
17
 
18
- students = known_faces_names.copy()
 
 
19
 
20
- now = datetime.now()
21
- current_date = now.strftime("%Y-%m-%d")
22
 
23
- # Create and open the CSV file
24
- f = open(current_date + '.csv', 'w+', newline='')
25
- lnwriter = csv.writer(f)
26
 
27
- # Initialize variables
28
- face_locations = []
29
- face_encodings = []
30
- face_names = []
31
- s = True
32
 
33
- while True:
34
- ret, frame = video_capture.read()
35
-
36
- if not ret:
37
- continue
 
38
 
39
- rgb_frame = frame[:, :, ::-1]
 
 
 
 
 
 
40
 
41
- if s:
42
- face_locations = face_recognition.face_locations(rgb_frame)
43
- face_encodings = face_recognition.face_encodings(frame, face_locations)
44
- face_names = []
 
 
45
 
46
- for face_encoding in face_encodings:
47
- matches = face_recognition.compare_faces(known_face_encoding, face_encoding)
48
- name = ""
49
- face_distance = face_recognition.face_distance(known_face_encoding, face_encoding)
50
- best_match_index = np.argmin(face_distance)
51
-
52
- if matches[best_match_index]:
53
- name = known_faces_names[best_match_index]
54
-
55
- face_names.append(name)
56
 
57
- if name in known_faces_names:
58
- if name in students:
59
- students.remove(name)
60
- print(students)
61
- current_time = now.strftime("%H-%M-%S")
62
- lnwriter.writerow([name, current_time, "Present"])
63
-
64
- cv2.imshow("attendance system", frame)
65
-
66
- if cv2.waitKey(1) & 0xFF == ord('q'):
67
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # Release the video capture and close the CSV file
70
- video_capture.release()
71
- cv2.destroyAllWindows()
72
- f.close()
 
1
+ from PIL import Image
2
+ from flask import *
3
  import face_recognition
4
  import cv2
5
  import numpy as np
6
  import csv
7
  from datetime import datetime
8
 
9
+ from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
10
+ import pylab # this allows you to control figure size
11
+ pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
12
 
13
+ import io
14
+ import streamlit as st
15
+ bytes_data=None
16
 
17
+ app = Flask(__name__)
 
18
 
19
+ @app.route('/at')
20
+ def attend():
21
+ # Face recognition variables
22
+ known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
23
+ known_face_encodings = []
24
 
25
+ # Load known face encodings
26
+ sir_image = face_recognition.load_image_file("photos/sir.jpeg")
27
+ sir_encoding = face_recognition.face_encodings(sir_image)[0]
28
 
29
+ vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
30
+ vikas_encoding = face_recognition.face_encodings(vikas_image)[0]
31
 
32
+ lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
33
+ lalit_encoding = face_recognition.face_encodings(lalit_image)[0]
 
34
 
35
+ jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
36
+ jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]
 
 
 
37
 
38
+ maam_image = face_recognition.load_image_file("photos/maam.png")
39
+ maam_encoding = face_recognition.face_encodings(maam_image)[0]
40
+
41
+ known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]
42
+
43
+ students = known_faces_names.copy()
44
 
45
+ face_locations = []
46
+ face_encodings = []
47
+ face_names = []
48
+
49
+ now = datetime.now()
50
+ current_date = now.strftime("%Y-%m-%d")
51
+ csv_file = open(f"{current_date}.csv", "a+", newline="")
52
 
53
+ csv_writer = csv.writer(csv_file)
54
+ def run_face_recognition():
55
+ video_capture = cv2.VideoCapture(0)
56
+ s = True
57
+
58
+ existing_names = set(row[0] for row in csv.reader(csv_file)) # Collect existing names from the CSV file
59
 
60
+
61
+ while s:
62
+ _, frame = video_capture.read()
63
+ small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
64
+ rgb_small_frame = small_frame[:, :, ::-1]
 
 
 
 
 
65
 
66
+ face_locations = face_recognition.face_locations(rgb_small_frame)
67
+ face_encodings = face_recognition.face_encodings(small_frame, face_locations)
68
+ face_names = []
69
+
70
+ for face_encoding in face_encodings:
71
+ matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
72
+ name = ""
73
+ face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
74
+ best_match_index = np.argmin(face_distance)
75
+ if matches[best_match_index]:
76
+ name = known_faces_names[best_match_index]
77
+
78
+ face_names.append(name)
79
+
80
+
81
+ for name in face_names:
82
+ if name in known_faces_names and name in students and name not in existing_names:
83
+ students.remove(name)
84
+ print(students)
85
+ print(f"Attendance recorded for {name}")
86
+ current_time = now.strftime("%H-%M-%S")
87
+ csv_writer.writerow([name, current_time, "Present"])
88
+ existing_names.add(name) # Add the name to the set of existing names
89
+
90
+ s = False # Set s to False to exit the loop after recording attendance
91
+ break # Break the loop once attendance has been recorded for a name
92
+
93
+ cv2.imshow("Attendance System", frame)
94
+ if cv2.waitKey(1) & 0xFF == ord('q'):
95
+ break
96
+
97
+ video_capture.release()
98
+ cv2.destroyAllWindows()
99
+ csv_file.close()
100
+
101
+ # Call the function to run face recognition
102
+ run_face_recognition()
103
+
104
+ return redirect(url_for('show_table'))
105
+
106
+ @app.route('/table')
107
+ def show_table():
108
+ # Get the current date
109
+ current_date = datetime.now().strftime("%Y-%m-%d")
110
+ # Read the CSV file to get attendance data
111
+ attendance=[]
112
+ try:
113
+ with open(f"{current_date}.csv", newline="") as csv_file:
114
+ csv_reader = csv.reader(csv_file)
115
+ attendance = list(csv_reader)
116
+ except FileNotFoundError:
117
+ pass
118
+ # Render the table.html template and pass the attendance data
119
+ return render_template('attendance.html', attendance=attendance)
120
+
121
+ @app.route('/')
122
+ def index():
123
+ return render_template('index.html')
124
+
125
+
126
+ if __name__ == '__main__':
127
+ # Start Flask application
128
+ app.run(debug=True)
129
 
130
+