Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# Try to import FaceNet with error handling
|
| 10 |
try:
|
|
@@ -68,15 +68,14 @@ class FaceRecognitionSystem:
|
|
| 68 |
try:
|
| 69 |
# Convert image to PIL format
|
| 70 |
if isinstance(image, str):
|
|
|
|
| 71 |
pil_image = Image.open(image).convert('RGB')
|
| 72 |
elif isinstance(image, np.ndarray):
|
|
|
|
| 73 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 74 |
else:
|
| 75 |
-
# Handle file
|
| 76 |
-
|
| 77 |
-
pil_image = Image.open(io.BytesIO(image.read())).convert('RGB')
|
| 78 |
-
else:
|
| 79 |
-
return None, "Unsupported image format"
|
| 80 |
|
| 81 |
# Detect faces and extract embeddings
|
| 82 |
faces = self.mtcnn(pil_image)
|
|
@@ -183,26 +182,30 @@ class FaceRecognitionSystem:
|
|
| 183 |
else:
|
| 184 |
result_message = f"⚠️ {name} already marked today (ID: {student_id})"
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
if isinstance(image,
|
|
|
|
|
|
|
| 188 |
display_image = image.copy()
|
| 189 |
else:
|
| 190 |
-
|
|
|
|
| 191 |
if len(display_image.shape) == 3 and display_image.shape[2] == 3:
|
| 192 |
display_image = cv2.cvtColor(display_image, cv2.COLOR_RGB2BGR)
|
| 193 |
|
| 194 |
# Add text to image
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
return display_image, result_message
|
| 204 |
-
else:
|
| 205 |
-
return image, "❌ No matching student found"
|
| 206 |
|
| 207 |
def get_all_students(self):
|
| 208 |
"""Get all registered students"""
|
|
@@ -273,31 +276,26 @@ with gr.Blocks(title="Face Recognition Attendance System") as demo:
|
|
| 273 |
with gr.Column():
|
| 274 |
name_input = gr.Textbox(
|
| 275 |
label="Full Name",
|
| 276 |
-
placeholder="Enter student's full name"
|
| 277 |
-
info="This name will be used for attendance records"
|
| 278 |
)
|
| 279 |
student_id_input = gr.Textbox(
|
| 280 |
label="Student ID",
|
| 281 |
-
placeholder="Enter unique student ID"
|
| 282 |
-
info="Must be unique for each student"
|
| 283 |
)
|
| 284 |
class_input = gr.Textbox(
|
| 285 |
label="Class",
|
| 286 |
-
placeholder="Enter class name"
|
| 287 |
-
info="e.g., Computer Science, Mathematics, etc."
|
| 288 |
)
|
| 289 |
image_input = gr.Image(
|
| 290 |
label="Upload Face Image",
|
| 291 |
-
type="filepath"
|
| 292 |
-
info="Upload a clear frontal face photo for registration"
|
| 293 |
)
|
| 294 |
-
register_btn = gr.Button("
|
| 295 |
|
| 296 |
with gr.Column():
|
| 297 |
register_output = gr.Textbox(
|
| 298 |
label="Registration Status",
|
| 299 |
-
lines=5
|
| 300 |
-
info="Status of student registration and face embedding extraction"
|
| 301 |
)
|
| 302 |
|
| 303 |
register_btn.click(
|
|
@@ -312,20 +310,17 @@ with gr.Blocks(title="Face Recognition Attendance System") as demo:
|
|
| 312 |
with gr.Column():
|
| 313 |
attendance_image = gr.Image(
|
| 314 |
label="Upload Photo for Attendance",
|
| 315 |
-
type="filepath"
|
| 316 |
-
info="Upload a photo containing faces to mark attendance automatically"
|
| 317 |
)
|
| 318 |
-
recognize_btn = gr.Button("
|
| 319 |
|
| 320 |
with gr.Column():
|
| 321 |
processed_image = gr.Image(
|
| 322 |
-
label="Processed Image"
|
| 323 |
-
info="Image with recognition results"
|
| 324 |
)
|
| 325 |
recognition_output = gr.Textbox(
|
| 326 |
label="Recognition Results",
|
| 327 |
-
lines=4
|
| 328 |
-
info="Attendance marking status and recognition details"
|
| 329 |
)
|
| 330 |
|
| 331 |
recognize_btn.click(
|
|
@@ -339,7 +334,7 @@ with gr.Blocks(title="Face Recognition Attendance System") as demo:
|
|
| 339 |
with gr.Row():
|
| 340 |
with gr.Column():
|
| 341 |
gr.Markdown("#### Registered Students")
|
| 342 |
-
students_btn = gr.Button("
|
| 343 |
students_output = gr.Markdown()
|
| 344 |
|
| 345 |
with gr.Column():
|
|
@@ -347,9 +342,9 @@ with gr.Blocks(title="Face Recognition Attendance System") as demo:
|
|
| 347 |
time_range = gr.Radio(
|
| 348 |
choices=["Today", "All Time"],
|
| 349 |
value="Today",
|
| 350 |
-
label="Select Time Range
|
| 351 |
)
|
| 352 |
-
attendance_btn = gr.Button("
|
| 353 |
attendance_output = gr.Markdown()
|
| 354 |
|
| 355 |
students_btn.click(view_students_interface, outputs=students_output)
|
|
@@ -365,58 +360,21 @@ with gr.Blocks(title="Face Recognition Attendance System") as demo:
|
|
| 365 |
|
| 366 |
This system uses state-of-the-art facial recognition technology to automate attendance marking.
|
| 367 |
|
| 368 |
-
###
|
| 369 |
- **Face Registration**: Register students with their facial data
|
| 370 |
- **Automatic Recognition**: Recognize students from photos and mark attendance
|
| 371 |
- **Attendance Management**: View and manage attendance records
|
| 372 |
-
- **SQLite Database**: Secure storage of student data and face embeddings
|
| 373 |
-
|
| 374 |
-
### 🔧 Technology Stack:
|
| 375 |
-
- **FaceNet**: For face detection and recognition
|
| 376 |
-
- **MTCNN**: Multi-task Cascaded CNN for face detection
|
| 377 |
-
- **Gradio**: For user-friendly web interface
|
| 378 |
-
- **SQLite**: For data persistence
|
| 379 |
-
|
| 380 |
-
### 📝 How to Use:
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
- Click 'Register Student with Face'
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
- View results in the processed image and status message
|
| 393 |
-
|
| 394 |
-
3. **View Records**:
|
| 395 |
-
- View all registered students
|
| 396 |
-
- Check attendance records (today or all time)
|
| 397 |
-
|
| 398 |
-
### 💡 Tips for Best Results:
|
| 399 |
-
- Use clear, well-lit face images for registration
|
| 400 |
-
- Ensure faces are clearly visible in attendance photos
|
| 401 |
-
- Frontal faces work better than profiles
|
| 402 |
-
- Good lighting improves recognition accuracy
|
| 403 |
-
|
| 404 |
-
### 🔒 Privacy & Security:
|
| 405 |
-
- Face embeddings are stored as numerical vectors
|
| 406 |
-
- No raw face images are stored in the database
|
| 407 |
-
- All data is processed locally
|
| 408 |
""")
|
| 409 |
-
|
| 410 |
-
# System status
|
| 411 |
-
status_msg = "✅ **System Status: OPERATIONAL**" if FACE_NET_AVAILABLE else "⚠️ **System Status: LIMITED FUNCTIONALITY**"
|
| 412 |
-
gr.Markdown(f"\n### System Status\n{status_msg}")
|
| 413 |
-
|
| 414 |
-
if not FACE_NET_AVAILABLE:
|
| 415 |
-
gr.Markdown("""
|
| 416 |
-
⚠️ **Note**: Face recognition features are currently unavailable.
|
| 417 |
-
The system is running in limited functionality mode.
|
| 418 |
-
Basic student registration and attendance tracking are still available.
|
| 419 |
-
""")
|
| 420 |
|
| 421 |
if __name__ == "__main__":
|
| 422 |
-
demo.launch(
|
|
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# Try to import FaceNet with error handling
|
| 10 |
try:
|
|
|
|
| 68 |
try:
|
| 69 |
# Convert image to PIL format
|
| 70 |
if isinstance(image, str):
|
| 71 |
+
# File path
|
| 72 |
pil_image = Image.open(image).convert('RGB')
|
| 73 |
elif isinstance(image, np.ndarray):
|
| 74 |
+
# numpy array
|
| 75 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 76 |
else:
|
| 77 |
+
# Handle Gradio file object
|
| 78 |
+
pil_image = Image.open(image).convert('RGB')
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Detect faces and extract embeddings
|
| 81 |
faces = self.mtcnn(pil_image)
|
|
|
|
| 182 |
else:
|
| 183 |
result_message = f"⚠️ {name} already marked today (ID: {student_id})"
|
| 184 |
|
| 185 |
+
# Convert image for display
|
| 186 |
+
if isinstance(image, str):
|
| 187 |
+
display_image = cv2.imread(image)
|
| 188 |
+
elif isinstance(image, np.ndarray):
|
| 189 |
display_image = image.copy()
|
| 190 |
else:
|
| 191 |
+
# Handle Gradio file object
|
| 192 |
+
display_image = np.array(Image.open(image))
|
| 193 |
if len(display_image.shape) == 3 and display_image.shape[2] == 3:
|
| 194 |
display_image = cv2.cvtColor(display_image, cv2.COLOR_RGB2BGR)
|
| 195 |
|
| 196 |
# Add text to image
|
| 197 |
+
if display_image is not None:
|
| 198 |
+
cv2.putText(display_image, f"Recognized: {name}", (10, 30),
|
| 199 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 200 |
+
cv2.putText(display_image, f"ID: {student_id}", (10, 60),
|
| 201 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 202 |
+
cv2.putText(display_image, f"Class: {class_name}", (10, 90),
|
| 203 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 204 |
+
|
| 205 |
+
display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB)
|
| 206 |
+
return display_image, result_message
|
| 207 |
|
| 208 |
+
return image, "❌ No matching student found"
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
def get_all_students(self):
|
| 211 |
"""Get all registered students"""
|
|
|
|
| 276 |
with gr.Column():
|
| 277 |
name_input = gr.Textbox(
|
| 278 |
label="Full Name",
|
| 279 |
+
placeholder="Enter student's full name"
|
|
|
|
| 280 |
)
|
| 281 |
student_id_input = gr.Textbox(
|
| 282 |
label="Student ID",
|
| 283 |
+
placeholder="Enter unique student ID"
|
|
|
|
| 284 |
)
|
| 285 |
class_input = gr.Textbox(
|
| 286 |
label="Class",
|
| 287 |
+
placeholder="Enter class name"
|
|
|
|
| 288 |
)
|
| 289 |
image_input = gr.Image(
|
| 290 |
label="Upload Face Image",
|
| 291 |
+
type="filepath"
|
|
|
|
| 292 |
)
|
| 293 |
+
register_btn = gr.Button("Register Student with Face", variant="primary")
|
| 294 |
|
| 295 |
with gr.Column():
|
| 296 |
register_output = gr.Textbox(
|
| 297 |
label="Registration Status",
|
| 298 |
+
lines=5
|
|
|
|
| 299 |
)
|
| 300 |
|
| 301 |
register_btn.click(
|
|
|
|
| 310 |
with gr.Column():
|
| 311 |
attendance_image = gr.Image(
|
| 312 |
label="Upload Photo for Attendance",
|
| 313 |
+
type="filepath"
|
|
|
|
| 314 |
)
|
| 315 |
+
recognize_btn = gr.Button("Recognize Faces & Mark Attendance", variant="primary")
|
| 316 |
|
| 317 |
with gr.Column():
|
| 318 |
processed_image = gr.Image(
|
| 319 |
+
label="Processed Image"
|
|
|
|
| 320 |
)
|
| 321 |
recognition_output = gr.Textbox(
|
| 322 |
label="Recognition Results",
|
| 323 |
+
lines=4
|
|
|
|
| 324 |
)
|
| 325 |
|
| 326 |
recognize_btn.click(
|
|
|
|
| 334 |
with gr.Row():
|
| 335 |
with gr.Column():
|
| 336 |
gr.Markdown("#### Registered Students")
|
| 337 |
+
students_btn = gr.Button("View All Students", variant="secondary")
|
| 338 |
students_output = gr.Markdown()
|
| 339 |
|
| 340 |
with gr.Column():
|
|
|
|
| 342 |
time_range = gr.Radio(
|
| 343 |
choices=["Today", "All Time"],
|
| 344 |
value="Today",
|
| 345 |
+
label="Select Time Range"
|
| 346 |
)
|
| 347 |
+
attendance_btn = gr.Button("View Attendance Records", variant="secondary")
|
| 348 |
attendance_output = gr.Markdown()
|
| 349 |
|
| 350 |
students_btn.click(view_students_interface, outputs=students_output)
|
|
|
|
| 360 |
|
| 361 |
This system uses state-of-the-art facial recognition technology to automate attendance marking.
|
| 362 |
|
| 363 |
+
### Features:
|
| 364 |
- **Face Registration**: Register students with their facial data
|
| 365 |
- **Automatic Recognition**: Recognize students from photos and mark attendance
|
| 366 |
- **Attendance Management**: View and manage attendance records
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
### How to Use:
|
| 369 |
+
1. **Register Students**: Go to 'Register Student' tab, enter details and upload face photo
|
| 370 |
+
2. **Mark Attendance**: Go to 'Mark Attendance' tab, upload photo with faces
|
| 371 |
+
3. **View Records**: Check registered students and attendance records
|
|
|
|
| 372 |
|
| 373 |
+
### Technology:
|
| 374 |
+
- FaceNet for face recognition
|
| 375 |
+
- Gradio for web interface
|
| 376 |
+
- SQLite for data storage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
if __name__ == "__main__":
|
| 380 |
+
demo.launch()
|