Vikas01 commited on
Commit
7199111
1 Parent(s): 4b4dfc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -140
app.py CHANGED
@@ -1,37 +1,18 @@
1
- from flask import Flask, jsonify, render_template, request, send_file
2
-
3
- app = Flask(__name__)
4
-
5
-
6
- @app.route("/")
7
- def index():
8
- #return 'hello'
9
- return render_template("index.html")
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
- if __name__ == "__main__":
19
- app.run(host="0.0.0.0", port=7860)
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
 
 
 
 
 
 
33
 
 
 
 
34
 
 
 
35
 
36
 
37
 
@@ -39,24 +20,15 @@ if __name__ == "__main__":
39
 
40
 
41
 
 
42
 
43
- # # from PIL import Image
44
- # from flask import *
45
- # from flask_socketio import SocketIO,emit
46
 
47
- # import face_recognition
48
- # import cv2
49
- # import numpy as np
50
- # import csv
51
- # from datetime import datetime
52
 
53
- # from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
54
- # import pylab # this allows you to control figure size
55
- # pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
56
 
57
- # import io
58
- # import streamlit as st
59
- # # bytes_data=None
60
  ####################################################
61
  # app = Flask(__name__)
62
 
@@ -67,118 +39,122 @@ if __name__ == "__main__":
67
  # def test_connect():
68
  # print("Connected")
69
  # emit("my response", {"data": "Connected"})
70
- #########################################################
71
- # @app.route('/at')
72
- # def attend():
73
- # # Face recognition variables
74
- # known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
75
- # known_face_encodings = []
76
 
77
- # # Load known face encodings
78
- # sir_image = face_recognition.load_image_file("photos/sir.jpeg")
79
- # sir_encoding = face_recognition.face_encodings(sir_image)[0]
80
 
81
- # vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
82
- # vikas_encoding = face_recognition.face_encodings(vikas_image)[0]
83
 
84
- # lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
85
- # lalit_encoding = face_recognition.face_encodings(lalit_image)[0]
86
 
87
- # jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
88
- # jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]
89
 
90
- # maam_image = face_recognition.load_image_file("photos/maam.png")
91
- # maam_encoding = face_recognition.face_encodings(maam_image)[0]
92
 
93
- # known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]
94
 
95
- # students = known_faces_names.copy()
96
 
97
- # face_locations = []
98
- # face_encodings = []
99
- # face_names = []
100
 
101
- # now = datetime.now()
102
- # current_date = now.strftime("%Y-%m-%d")
103
- # csv_file = open(f"{current_date}.csv", "a+", newline="")
104
 
105
- # csv_writer = csv.writer(csv_file)
106
- # def run_face_recognition():
107
- # video_capture = cv2.VideoCapture(0)
108
- # s = True
109
-
110
- # existing_names = set(row[0] for row in csv.reader(csv_file)) # Collect existing names from the CSV file
111
-
112
-
113
- # while s:
114
- # _, frame = video_capture.read()
115
- # small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
116
- # rgb_small_frame = small_frame[:, :, ::-1]
117
-
118
- # face_locations = face_recognition.face_locations(rgb_small_frame)
119
- # face_encodings = face_recognition.face_encodings(small_frame, face_locations)
120
- # face_names = []
121
-
122
- # for face_encoding in face_encodings:
123
- # matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
124
- # name = ""
125
- # face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
126
- # best_match_index = np.argmin(face_distance)
127
- # if matches[best_match_index]:
128
- # name = known_faces_names[best_match_index]
129
-
130
- # face_names.append(name)
131
-
132
-
133
- # for name in face_names:
134
- # if name in known_faces_names and name in students and name not in existing_names:
135
- # students.remove(name)
136
- # print(students)
137
- # print(f"Attendance recorded for {name}")
138
- # current_time = now.strftime("%H-%M-%S")
139
- # csv_writer.writerow([name, current_time, "Present"])
140
- # existing_names.add(name) # Add the name to the set of existing names
141
-
142
- # s = False # Set s to False to exit the loop after recording attendance
143
- # break # Break the loop once attendance has been recorded for a name
144
-
145
- # cv2.imshow("Attendance System", frame)
146
- # if cv2.waitKey(1) & 0xFF == ord('q'):
147
- # break
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  # video_capture.release()
150
  # cv2.destroyAllWindows()
151
- # csv_file.close()
152
-
153
- # # Call the function to run face recognition
154
- # run_face_recognition()
155
-
156
- # return redirect(url_for('show_table'))
157
-
158
- # @app.route('/table')
159
- # def show_table():
160
- # # Get the current date
161
- # current_date = datetime.now().strftime("%Y-%m-%d")
162
- # # Read the CSV file to get attendance data
163
- # attendance=[]
164
- # try:
165
- # with open(f"{current_date}.csv", newline="") as csv_file:
166
- # csv_reader = csv.reader(csv_file)
167
- # attendance = list(csv_reader)
168
- # except FileNotFoundError:
169
- # pass
170
- # # Render the table.html template and pass the attendance data
171
- # return render_template('attendance.html', attendance=attendance)
172
 
173
- # @app.route("/")
174
- # def home():
175
- # return render_template('index.html')
176
 
177
 
178
 
179
 
180
- # if __name__ == '__main__':
181
- # # Start Flask application
182
- # socket.run(app,host="0.0.0.0", port=5000)
183
 
184
 
 
1
+ from flask import *
2
+ from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ import face_recognition
5
+ import cv2
6
+ import numpy as np
7
+ import csv
8
+ from datetime import datetime
9
 
10
+ from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
11
+ import pylab # this allows you to control figure size
12
+ pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
13
 
14
+ import io
15
+ import streamlit as st
16
 
17
 
18
 
 
20
 
21
 
22
 
23
+ app = Flask(__name__)
24
 
 
 
 
25
 
26
+ # @app.route("/")
27
+ # def index():
28
+ # #return 'hello'
29
+ # return render_template("index.html")
 
30
 
 
 
 
31
 
 
 
 
32
  ####################################################
33
  # app = Flask(__name__)
34
 
 
39
  # def test_connect():
40
  # print("Connected")
41
  # emit("my response", {"data": "Connected"})
42
+ ########################################################
43
+ @app.route('/at')
44
+ def attend():
45
+ # Face recognition variables
46
+ known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
47
+ known_face_encodings = []
48
 
49
+ # Load known face encodings
50
+ sir_image = face_recognition.load_image_file("photos/sir.jpeg")
51
+ sir_encoding = face_recognition.face_encodings(sir_image)[0]
52
 
53
+ vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
54
+ vikas_encoding = face_recognition.face_encodings(vikas_image)[0]
55
 
56
+ lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
57
+ lalit_encoding = face_recognition.face_encodings(lalit_image)[0]
58
 
59
+ jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
60
+ jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]
61
 
62
+ maam_image = face_recognition.load_image_file("photos/maam.png")
63
+ maam_encoding = face_recognition.face_encodings(maam_image)[0]
64
 
65
+ known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]
66
 
67
+ students = known_faces_names.copy()
68
 
69
+ face_locations = []
70
+ face_encodings = []
71
+ face_names = []
72
 
73
+ now = datetime.now()
74
+ current_date = now.strftime("%Y-%m-%d")
75
+ csv_file = open(f"{current_date}.csv", "a+", newline="")
76
 
77
+ csv_writer = csv.writer(csv_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ bytes_data=None
80
+ def run_face_recognition():
81
+ img_file_buffer=st.camera_input("Take a picture")
82
+ if img_file_buffer is not None:
83
+
84
+ s = True
85
+
86
+ existing_names = set(row[0] for row in csv.reader(csv_file)) # Collect existing names from the CSV file
87
+
88
+
89
+ while s:
90
+ _, frame = img_file_buffer.read()
91
+ small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
92
+ rgb_small_frame = small_frame[:, :, ::-1]
93
+
94
+ face_locations = face_recognition.face_locations(rgb_small_frame)
95
+ face_encodings = face_recognition.face_encodings(small_frame, face_locations)
96
+ face_names = []
97
+
98
+ for face_encoding in face_encodings:
99
+ matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
100
+ name = ""
101
+ face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
102
+ best_match_index = np.argmin(face_distance)
103
+ if matches[best_match_index]:
104
+ name = known_faces_names[best_match_index]
105
+
106
+ face_names.append(name)
107
+
108
+
109
+ for name in face_names:
110
+ if name in known_faces_names and name in students and name not in existing_names:
111
+ students.remove(name)
112
+ print(students)
113
+ print(f"Attendance recorded for {name}")
114
+ current_time = now.strftime("%H-%M-%S")
115
+ csv_writer.writerow([name, current_time, "Present"])
116
+ existing_names.add(name) # Add the name to the set of existing names
117
+
118
+ s = False # Set s to False to exit the loop after recording attendance
119
+ break # Break the loop once attendance has been recorded for a name
120
+
121
+ cv2.imshow("Attendance System", frame)
122
+ if cv2.waitKey(1) & 0xFF == ord('q'):
123
+ break
124
+ if bytes_data is None:
125
+ st.stop()
126
  # video_capture.release()
127
  # cv2.destroyAllWindows()
128
+ csv_file.close()
129
+
130
+ # Call the function to run face recognition
131
+ run_face_recognition()
132
+
133
+ return redirect(url_for('show_table'))
134
+
135
+ @app.route('/table')
136
+ def show_table():
137
+ # Get the current date
138
+ current_date = datetime.now().strftime("%Y-%m-%d")
139
+ # Read the CSV file to get attendance data
140
+ attendance=[]
141
+ try:
142
+ with open(f"{current_date}.csv", newline="") as csv_file:
143
+ csv_reader = csv.reader(csv_file)
144
+ attendance = list(csv_reader)
145
+ except FileNotFoundError:
146
+ pass
147
+ # Render the table.html template and pass the attendance data
148
+ return render_template('attendance.html', attendance=attendance)
149
 
150
+ @app.route("/")
151
+ def home():
152
+ return render_template('index.html')
153
 
154
 
155
 
156
 
157
+ if __name__ == "__main__":
158
+ app.run(host="0.0.0.0", port=7860)
 
159
 
160