Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,29 +12,34 @@ import matplotlib.pyplot as plt
|
|
12 |
import pylab # this allows you to control figure size
|
13 |
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
|
14 |
|
15 |
-
import io
|
16 |
-
import streamlit as st
|
17 |
-
bytes_data=None
|
18 |
|
19 |
##################################################3
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
app = Flask(__name__)
|
22 |
|
23 |
-
flag1 = True
|
24 |
|
25 |
-
@app.route('/at')
|
26 |
-
def testme():
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
# def attend():
|
40 |
# # Face recognition variables
|
@@ -123,11 +128,116 @@ def testme():
|
|
123 |
# run_face_recognition()
|
124 |
|
125 |
# return redirect(url_for('show_table'))
|
|
|
|
|
|
|
126 |
|
127 |
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
@app.route('/table')
|
132 |
def show_table():
|
133 |
# Get the current date
|
|
|
12 |
import pylab # this allows you to control figure size
|
13 |
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
|
14 |
|
15 |
+
# import io
|
16 |
+
# import streamlit as st
|
17 |
+
# bytes_data=None
|
18 |
|
19 |
##################################################3
|
20 |
|
21 |
+
import gradio as gr
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
app = Flask(__name__)
|
27 |
|
28 |
+
# flag1 = True
|
29 |
|
30 |
+
# @app.route('/at')
|
31 |
+
# def testme():
|
32 |
+
# global flag1
|
33 |
+
# # return "i am in testme"
|
34 |
+
# while flag1 is True:
|
35 |
|
36 |
+
# img_file_buffer=st.camera_input("Take a picture")
|
37 |
+
# if img_file_buffer is not None:
|
38 |
+
# test_image = Image.open(img_file_buffer)
|
39 |
+
# st.image(test_image, use_column_width=True)
|
40 |
+
# if bytes_data is None:
|
41 |
+
# flag1 = False
|
42 |
+
# st.stop()
|
43 |
|
44 |
# def attend():
|
45 |
# # Face recognition variables
|
|
|
128 |
# run_face_recognition()
|
129 |
|
130 |
# return redirect(url_for('show_table'))
|
131 |
+
##########################################################################
|
132 |
+
def snap(image,video):
|
133 |
+
return [image,video]
|
134 |
|
135 |
|
136 |
+
@app.route('/at')
|
137 |
+
def attend():
|
138 |
+
# Face recognition variables
|
139 |
+
known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
|
140 |
+
known_face_encodings = []
|
141 |
|
142 |
+
# Load known face encodings
|
143 |
+
sir_image = face_recognition.load_image_file("photos/sir.jpeg")
|
144 |
+
sir_encoding = face_recognition.face_encodings(sir_image)[0]
|
145 |
+
|
146 |
+
vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
|
147 |
+
vikas_encoding = face_recognition.face_encodings(vikas_image)[0]
|
148 |
+
|
149 |
+
lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
|
150 |
+
lalit_encoding = face_recognition.face_encodings(lalit_image)[0]
|
151 |
+
|
152 |
+
jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
|
153 |
+
jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]
|
154 |
+
|
155 |
+
maam_image = face_recognition.load_image_file("photos/maam.png")
|
156 |
+
maam_encoding = face_recognition.face_encodings(maam_image)[0]
|
157 |
+
|
158 |
+
known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]
|
159 |
+
|
160 |
+
students = known_faces_names.copy()
|
161 |
+
|
162 |
+
face_locations = []
|
163 |
+
face_encodings = []
|
164 |
+
face_names = []
|
165 |
+
|
166 |
+
now = datetime.now()
|
167 |
+
current_date = now.strftime("%Y-%m-%d")
|
168 |
+
csv_file = open(f"{current_date}.csv", "a+", newline="")
|
169 |
+
|
170 |
+
csv_writer = csv.writer(csv_file)
|
171 |
+
|
172 |
|
173 |
+
# Function to run face recognition
|
174 |
+
def run_face_recognition():
|
175 |
+
video_capture = cv2.VideoCapture(0)
|
176 |
+
s = True
|
177 |
+
|
178 |
+
existing_names = set(row[0] for row in csv.reader(csv_file)) # Collect existing names from the CSV file
|
179 |
+
|
180 |
+
|
181 |
+
while s:
|
182 |
+
_, frame = video_capture.read()
|
183 |
+
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
|
184 |
+
rgb_small_frame = small_frame[:, :, ::-1]
|
185 |
+
|
186 |
+
face_locations = face_recognition.face_locations(rgb_small_frame)
|
187 |
+
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
|
188 |
+
face_names = []
|
189 |
+
|
190 |
+
for face_encoding in face_encodings:
|
191 |
+
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
|
192 |
+
name = ""
|
193 |
+
face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
|
194 |
+
best_match_index = np.argmin(face_distance)
|
195 |
+
if matches[best_match_index]:
|
196 |
+
name = known_faces_names[best_match_index]
|
197 |
+
|
198 |
+
face_names.append(name)
|
199 |
+
|
200 |
+
|
201 |
+
for name in face_names:
|
202 |
+
if name in known_faces_names and name in students and name not in existing_names:
|
203 |
+
students.remove(name)
|
204 |
+
print(students)
|
205 |
+
print(f"Attendance recorded for {name}")
|
206 |
+
current_time = now.strftime("%H-%M-%S")
|
207 |
+
csv_writer.writerow([name, current_time, "Present"])
|
208 |
+
existing_names.add(name) # Add the name to the set of existing names
|
209 |
+
|
210 |
+
s = False # Set s to False to exit the loop after recording attendance
|
211 |
+
break # Break the loop once attendance has been recorded for a name
|
212 |
+
|
213 |
+
cv2.imshow("Attendance System", frame)
|
214 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
215 |
+
break
|
216 |
+
|
217 |
+
video_capture.release()
|
218 |
+
cv2.destroyAllWindows()
|
219 |
+
csv_file.close()
|
220 |
+
|
221 |
+
# Call the function to run face recognition
|
222 |
+
run_face_recognition()
|
223 |
+
|
224 |
+
return redirect(url_for('show_table'))
|
225 |
+
|
226 |
+
def gradio_interface():
|
227 |
+
demo = gr.Interface(
|
228 |
+
snap,
|
229 |
+
[gr.Image(source="webcam", tool=None), gr.Video(source="webcam")],
|
230 |
+
["image", "video"],
|
231 |
+
)
|
232 |
+
return demo
|
233 |
+
|
234 |
+
|
235 |
+
@app.route('/gradio')
|
236 |
+
def gradio():
|
237 |
+
interface = gradio_interface()
|
238 |
+
return interface.launch()
|
239 |
+
|
240 |
+
###########################################################################
|
241 |
@app.route('/table')
|
242 |
def show_table():
|
243 |
# Get the current date
|