Vikas01 commited on
Commit
b881cfe
·
1 Parent(s): 46ddd82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -26
app.py CHANGED
@@ -17,10 +17,6 @@ import streamlit as st
17
  import streamlit_webrtc as webrtc
18
 
19
 
20
-
21
-
22
-
23
-
24
  app = Flask(__name__)
25
 
26
 
@@ -77,28 +73,58 @@ def attend():
77
 
78
  csv_writer = csv.writer(csv_file)
79
 
80
- @app.route('/at')
81
- def cam():
82
- return render_template('attendance.html')
83
-
84
- def main():
85
- webrtc_ctx = webrtc.client(ctx=st)
86
- webrtc_ctx.create_stream()
87
-
88
- while True:
89
- if webrtc_ctx.video_receiver:
90
- frame = webrtc_ctx.video_receiver.get_frame()
91
- if frame is not None:
92
- image = frame.to_ndarray(format="bgr24")
93
- # Process the image with OpenCV as needed
94
-
95
- # Display the processed image
96
- st.image(image, channels="BGR")
97
- else:
98
- st.write("Waiting for video...")
99
- st.sleep(0.1)
100
-
101
- return render_template('attendance.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
 
104
 
 
17
  import streamlit_webrtc as webrtc
18
 
19
 
 
 
 
 
20
  app = Flask(__name__)
21
 
22
 
 
73
 
74
  csv_writer = csv.writer(csv_file)
75
 
76
+ # Function to run face recognition
77
+ def run_face_recognition():
78
+ video_capture = cv2.VideoCapture(0)
79
+ s = True
80
+
81
+ existing_names = set(row[0] for row in csv.reader(csv_file)) # Collect existing names from the CSV file
82
+
83
+
84
+ while s:
85
+ _, frame = video_capture.read()
86
+ small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
87
+ rgb_small_frame = small_frame[:, :, ::-1]
88
+
89
+ face_locations = face_recognition.face_locations(rgb_small_frame)
90
+ face_encodings = face_recognition.face_encodings(small_frame, face_locations)
91
+ face_names = []
92
+
93
+ for face_encoding in face_encodings:
94
+ matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
95
+ name = ""
96
+ face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
97
+ best_match_index = np.argmin(face_distance)
98
+ if matches[best_match_index]:
99
+ name = known_faces_names[best_match_index]
100
+
101
+ face_names.append(name)
102
+
103
+
104
+ for name in face_names:
105
+ if name in known_faces_names and name in students and name not in existing_names:
106
+ students.remove(name)
107
+ print(students)
108
+ print(f"Attendance recorded for {name}")
109
+ current_time = now.strftime("%H-%M-%S")
110
+ csv_writer.writerow([name, current_time, "Present"])
111
+ existing_names.add(name) # Add the name to the set of existing names
112
+
113
+ s = False # Set s to False to exit the loop after recording attendance
114
+ break # Break the loop once attendance has been recorded for a name
115
+
116
+ cv2.imshow("Attendance System", frame)
117
+ if cv2.waitKey(1) & 0xFF == ord('q'):
118
+ break
119
+
120
+ video_capture.release()
121
+ cv2.destroyAllWindows()
122
+ csv_file.close()
123
+
124
+ # Call the function to run face recognition
125
+ run_face_recognition()
126
+
127
+ return redirect(url_for('show_table'))
128
 
129
 
130