LovnishVerma commited on
Commit
e34404d
·
verified ·
1 Parent(s): 7de09b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -50
app.py CHANGED
@@ -103,7 +103,7 @@ def process_frame(frame):
103
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
104
  cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
105
 
106
- return frame
107
 
108
  # Attendance recording
109
  def record_attendance(name, roll_no, emotion):
@@ -116,60 +116,88 @@ def record_attendance(name, roll_no, emotion):
116
  conn.commit()
117
  conn.close()
118
 
119
- # User Interface
120
- st.title("Student Registration and Attendance")
121
-
122
- # Choose input method for the image (webcam or file upload)
123
- capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
124
-
125
- if capture_mode == "Use Webcam":
126
- picture = st.camera_input("Take a picture") # Capture image using webcam
127
- elif capture_mode == "Upload File":
128
- picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
129
-
130
- # Input fields for student details
131
- name = st.text_input("Enter your name")
132
- roll_no = st.text_input("Enter your roll number")
133
-
134
- # Handle image upload or webcam capture
135
- if st.button("Register"):
136
- if not name or not roll_no:
137
- st.error("Please fill in both name and roll number.")
138
- elif not picture:
139
- st.error("Please upload or capture an image.")
140
- else:
141
- try:
142
- # Open the image based on capture mode
143
- if capture_mode == "Use Webcam" and picture:
144
- image = Image.open(picture)
145
- elif capture_mode == "Upload File" and picture:
146
- image = Image.open(picture)
147
-
148
- # Save the image locally and upload it to Hugging Face
149
- image_path = save_image_to_hugging_face(image, name, roll_no)
150
-
151
- # Save user data to the database
152
- save_to_database(name, roll_no, image_path)
153
-
154
- # Detect faces and emotions
155
- cap = cv2.VideoCapture(0)
156
- while True:
157
- ret, frame = cap.read()
158
- if not ret:
159
- break
160
-
161
- frame = process_frame(frame)
162
- st.image(frame, channels="BGR", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  record_attendance(name, roll_no, emotion)
 
164
  break # Stop after capturing one frame
165
 
166
- cap.release()
167
 
168
- except Exception as e:
169
- st.error(f"An error occurred: {e}")
170
 
171
- # Display registered students
172
- if st.checkbox("Show registered students"):
173
  conn = sqlite3.connect(DATABASE)
174
  cursor = conn.cursor()
175
  cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
 
103
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
104
  cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
105
 
106
+ return frame, faces # Return the frame and the detected faces
107
 
108
  # Attendance recording
109
  def record_attendance(name, roll_no, emotion):
 
116
  conn.commit()
117
  conn.close()
118
 
119
+ # User Interface - Streamlit tabs
120
+ st.title("Student Registration and Attendance System")
121
+
122
+ tabs = st.selectbox("Choose Functionality", ["Register Student", "Record Attendance", "View Attendance"])
123
+
124
+ if tabs == "Register Student":
125
+ st.subheader("Student Registration")
126
+
127
+ # Choose input method for the image (webcam or file upload)
128
+ capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
129
+
130
+ if capture_mode == "Use Webcam":
131
+ picture = st.camera_input("Take a picture") # Capture image using webcam
132
+ elif capture_mode == "Upload File":
133
+ picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
134
+
135
+ # Input fields for student details
136
+ name = st.text_input("Enter your name")
137
+ roll_no = st.text_input("Enter your roll number")
138
+
139
+ # Handle image upload or webcam capture
140
+ if st.button("Register"):
141
+ if not name or not roll_no:
142
+ st.error("Please fill in both name and roll number.")
143
+ elif not picture:
144
+ st.error("Please upload or capture an image.")
145
+ else:
146
+ try:
147
+ # Open the image based on capture mode
148
+ if capture_mode == "Use Webcam" and picture:
149
+ image = Image.open(picture)
150
+ elif capture_mode == "Upload File" and picture:
151
+ image = Image.open(picture)
152
+
153
+ # Save the image locally and upload it to Hugging Face
154
+ image_path = save_image_to_hugging_face(image, name, roll_no)
155
+
156
+ # Save user data to the database
157
+ save_to_database(name, roll_no, image_path)
158
+
159
+ st.success("Registration Successful!")
160
+
161
+ except Exception as e:
162
+ st.error(f"An error occurred: {e}")
163
+
164
+ elif tabs == "Record Attendance":
165
+ st.subheader("Record Attendance")
166
+
167
+ # Initialize the webcam for face detection and emotion recognition
168
+ cap = cv2.VideoCapture(0)
169
+
170
+ while True:
171
+ ret, frame = cap.read()
172
+ if not ret:
173
+ break
174
+
175
+ # Process the frame to detect faces and emotions
176
+ frame, faces = process_frame(frame)
177
+
178
+ # Show the image with detected faces and emotions
179
+ st.image(frame, channels="BGR", use_column_width=True)
180
+
181
+ # When a face is detected, assume it's the person for attendance
182
+ if len(faces) > 0:
183
+ # Assume the first face detected corresponds to the student
184
+ x, y, w, h = faces[0]
185
+ # Extract the emotion from the frame (for simplicity, use the first detected face)
186
+ name = "John Doe" # Replace with the actual name after face recognition
187
+ roll_no = "12345" # Replace with the actual roll number
188
+ emotion = emotion_labels[np.argmax(model.predict(cv2.resize(frame[y:y+h, x:x+w], (48, 48)).reshape(1, 48, 48, 3)))] # Get emotion of detected face
189
+
190
+ # Record attendance based on the detected information
191
+ if st.button("Record Attendance"):
192
  record_attendance(name, roll_no, emotion)
193
+ st.success(f"Attendance recorded for {name} ({roll_no}) with emotion: {emotion}")
194
  break # Stop after capturing one frame
195
 
196
+ cap.release()
197
 
198
+ elif tabs == "View Attendance":
199
+ st.subheader("View Attendance Records")
200
 
 
 
201
  conn = sqlite3.connect(DATABASE)
202
  cursor = conn.cursor()
203
  cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")