File size: 9,295 Bytes
7199111
 
8ecf185
7199111
 
 
 
 
8ecf185
3fac891
1419912
3fac891
 
8ecf185
088b445
 
 
8ecf185
3fac891
3657998
088b445
 
 
 
 
3fac891
3657998
088b445
8ecf185
088b445
 
 
 
 
290eb90
088b445
 
 
 
 
 
 
8ff9a4c
c458411
 
 
 
3657998
c458411
 
 
3657998
c458411
 
3657998
c458411
 
3657998
c458411
 
17aa4ec
c458411
 
bbcce29
c458411
bbcce29
c458411
8510d34
c458411
 
 
bbcce29
c458411
 
 
9eb9696
c458411
cad92ea
c458411
 
 
 
b881cfe
c458411
b881cfe
 
c458411
 
 
 
b881cfe
c458411
 
 
b881cfe
c458411
 
 
 
 
 
 
b881cfe
c458411
b881cfe
 
c458411
 
 
 
 
 
 
 
b881cfe
c458411
 
b881cfe
c458411
 
 
b881cfe
c458411
 
 
b881cfe
c458411
 
b881cfe
c458411
088b445
 
 
98b16e3
0304d96
088b445
 
 
 
 
0304d96
088b445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7199111
088b445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7199111
 
 
 
 
 
 
 
 
 
 
 
 
 
bbcce29
7199111
 
 
8ecf185
9d9428d
bbcce29
 
7199111
 
9eb9696
bbcce29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from flask import *
from PIL import Image

import face_recognition
import cv2
import numpy as np
import csv
from datetime import datetime

############################################
import matplotlib.pyplot as plt
import pylab # this allows you to control figure size 
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook

# import io
# import streamlit as st
# bytes_data=None

##################################################3

import gradio as gr




app = Flask(__name__)

# flag1 = True

# @app.route('/at')
# def testme():
#     global flag1
#     # return "i am in testme"
#     while flag1 is True:
        
#         img_file_buffer=st.camera_input("Take a picture")
#         if img_file_buffer is not None:
#             test_image = Image.open(img_file_buffer)
#             st.image(test_image, use_column_width=True)
#         if bytes_data is None:
#             flag1 = False
#             st.stop()

# def attend():
#     # Face recognition variables
#     known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
#     known_face_encodings = []

#     # Load known face encodings
#     sir_image = face_recognition.load_image_file("photos/sir.jpeg")
#     sir_encoding = face_recognition.face_encodings(sir_image)[0]

#     vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
#     vikas_encoding = face_recognition.face_encodings(vikas_image)[0]

#     lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
#     lalit_encoding = face_recognition.face_encodings(lalit_image)[0]

#     jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
#     jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]

#     maam_image = face_recognition.load_image_file("photos/maam.png")
#     maam_encoding = face_recognition.face_encodings(maam_image)[0]

#     known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]

#     students = known_faces_names.copy()

#     face_locations = []
#     face_encodings = []
#     face_names = []

#     now = datetime.now()
#     current_date = now.strftime("%Y-%m-%d")
#     csv_file = open(f"{current_date}.csv", "a+", newline="")
    
#     csv_writer = csv.writer(csv_file)

#     # Function to run face recognition
#     def run_face_recognition():
#         video_capture = cv2.VideoCapture(0)
#         s = True

#         existing_names = set(row[0] for row in csv.reader(csv_file))  # Collect existing names from the CSV file   
        

#         while s:
#             _, frame = video_capture.read()
#             small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
#             rgb_small_frame = small_frame[:, :, ::-1]
            
#             face_locations = face_recognition.face_locations(rgb_small_frame)
#             face_encodings = face_recognition.face_encodings(small_frame, face_locations)
#             face_names = []

#             for face_encoding in face_encodings:
#                 matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
#                 name = ""
#                 face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
#                 best_match_index = np.argmin(face_distance)
#                 if matches[best_match_index]:
#                     name = known_faces_names[best_match_index]

#                 face_names.append(name)

               
#                 for name in face_names:
#                     if name in known_faces_names and name in students and name not in existing_names:
#                         students.remove(name)
#                         print(students)
#                         print(f"Attendance recorded for {name}")
#                         current_time = now.strftime("%H-%M-%S")
#                         csv_writer.writerow([name, current_time, "Present"])
#                         existing_names.add(name)  # Add the name to the set of existing names
                        
#                         s = False  # Set s to False to exit the loop after recording attendance
#                         break  # Break the loop once attendance has been recorded for a name

#             cv2.imshow("Attendance System", frame)
#             if cv2.waitKey(1) & 0xFF == ord('q'):
#                 break

#         video_capture.release()
#         cv2.destroyAllWindows()
#         csv_file.close()

#     # Call the function to run face recognition
#     run_face_recognition()

#     return redirect(url_for('show_table'))
##########################################################################
def snap(image,video):
    return [image,video]


@app.route('/at')
def attend():
    # Face recognition variables
    known_faces_names = ["Sarwan Sir", "Vikas","Lalit","Jasmeen","Anita Ma'am"]
    known_face_encodings = []

    # Load known face encodings
    sir_image = face_recognition.load_image_file("photos/sir.jpeg")
    sir_encoding = face_recognition.face_encodings(sir_image)[0]

    vikas_image = face_recognition.load_image_file("photos/vikas.jpg")
    vikas_encoding = face_recognition.face_encodings(vikas_image)[0]

    lalit_image = face_recognition.load_image_file("photos/lalit.jpg")
    lalit_encoding = face_recognition.face_encodings(lalit_image)[0]

    jasmine_image = face_recognition.load_image_file("photos/jasmine.jpg")
    jasmine_encoding = face_recognition.face_encodings(jasmine_image)[0]

    maam_image = face_recognition.load_image_file("photos/maam.png")
    maam_encoding = face_recognition.face_encodings(maam_image)[0]

    known_face_encodings = [sir_encoding, vikas_encoding,lalit_encoding,jasmine_encoding,maam_encoding]

    students = known_faces_names.copy()

    face_locations = []
    face_encodings = []
    face_names = []

    now = datetime.now()
    current_date = now.strftime("%Y-%m-%d")
    csv_file = open(f"{current_date}.csv", "a+", newline="")
    
    csv_writer = csv.writer(csv_file)
    

    # Function to run face recognition
    def run_face_recognition():
        video_capture = cv2.VideoCapture(0)
        s = True

        existing_names = set(row[0] for row in csv.reader(csv_file))  # Collect existing names from the CSV file   
        

        while s:
            _, frame = video_capture.read()
            small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
            rgb_small_frame = small_frame[:, :, ::-1]
            
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(small_frame, face_locations)
            face_names = []

            for face_encoding in face_encodings:
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = ""
                face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distance)
                if matches[best_match_index]:
                    name = known_faces_names[best_match_index]

                face_names.append(name)

               
                for name in face_names:
                    if name in known_faces_names and name in students and name not in existing_names:
                        students.remove(name)
                        print(students)
                        print(f"Attendance recorded for {name}")
                        current_time = now.strftime("%H-%M-%S")
                        csv_writer.writerow([name, current_time, "Present"])
                        existing_names.add(name)  # Add the name to the set of existing names
                        
                        s = False  # Set s to False to exit the loop after recording attendance
                        break  # Break the loop once attendance has been recorded for a name

            cv2.imshow("Attendance System", frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        video_capture.release()
        cv2.destroyAllWindows()
        csv_file.close()

    # Call the function to run face recognition
    run_face_recognition()

    return redirect(url_for('show_table'))

def gradio_interface():
    demo = gr.Interface(
        snap,
        [gr.Image(source="webcam", tool=None), gr.Video(source="webcam")],
        ["image", "video"],
    )
    return demo


@app.route('/gradio')
def gradio():
    interface = gradio_interface()
    return interface.launch()
    
###########################################################################
@app.route('/table')
def show_table():
    # Get the current date
    current_date = datetime.now().strftime("%Y-%m-%d")
    # Read the CSV file to get attendance data
    attendance=[]
    try:
        with open(f"{current_date}.csv", newline="") as csv_file:
            csv_reader = csv.reader(csv_file)
            attendance = list(csv_reader)
    except FileNotFoundError:
        pass
    # Render the table.html template and pass the attendance data
    return render_template('attendance.html', attendance=attendance)

@app.route("/")
def home():
    return render_template('index.html')

   


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)