Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,9 @@ import numpy as np
|
|
| 2 |
import cv2
|
| 3 |
import face_recognition
|
| 4 |
import sqlite3
|
| 5 |
-
import
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
# Step 1: Initialize Face Recognition
|
| 9 |
# Load known faces and names
|
|
@@ -11,10 +12,13 @@ known_face_encodings = []
|
|
| 11 |
known_face_names = []
|
| 12 |
|
| 13 |
# Example: Add known faces
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Step 2: Set Up Database
|
| 20 |
connection = sqlite3.connect("attendance.db", check_same_thread=False)
|
|
@@ -34,9 +38,9 @@ def log_attendance(name):
|
|
| 34 |
connection.commit()
|
| 35 |
|
| 36 |
# Step 3: Detect and Recognize Faces
|
| 37 |
-
def detect_and_mark_attendance(
|
| 38 |
-
|
| 39 |
-
rgb_image = cv2.cvtColor(
|
| 40 |
face_locations = face_recognition.face_locations(rgb_image)
|
| 41 |
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
| 42 |
|
|
@@ -50,10 +54,10 @@ def detect_and_mark_attendance(image):
|
|
| 50 |
log_attendance(name) # Log to database
|
| 51 |
|
| 52 |
# Draw a rectangle around the face
|
| 53 |
-
cv2.rectangle(
|
| 54 |
-
cv2.putText(
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
# Step 4: Fetch Attendance Logs
|
| 59 |
def fetch_attendance():
|
|
@@ -61,21 +65,26 @@ def fetch_attendance():
|
|
| 61 |
rows = cursor.fetchall()
|
| 62 |
return rows
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
inputs="image",
|
| 68 |
-
outputs="image",
|
| 69 |
-
title="Face Recognition and Attendance",
|
| 70 |
-
description="Upload an image to detect and mark attendance. Attendance will be saved to the database."
|
| 71 |
-
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
inputs=None,
|
| 76 |
-
outputs="dataframe",
|
| 77 |
-
label="Attendance Logs"
|
| 78 |
-
)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import face_recognition
|
| 4 |
import sqlite3
|
| 5 |
+
import streamlit as st
|
| 6 |
from datetime import datetime
|
| 7 |
+
from PIL import Image
|
| 8 |
|
| 9 |
# Step 1: Initialize Face Recognition
|
| 10 |
# Load known faces and names
|
|
|
|
| 12 |
known_face_names = []
|
| 13 |
|
| 14 |
# Example: Add known faces
|
| 15 |
+
try:
|
| 16 |
+
image_person1 = face_recognition.load_image_file("person1.jpg")
|
| 17 |
+
encoding_person1 = face_recognition.face_encodings(image_person1)[0]
|
| 18 |
+
known_face_encodings.append(encoding_person1)
|
| 19 |
+
known_face_names.append("Person 1")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
st.error(f"Error loading known faces: {e}")
|
| 22 |
|
| 23 |
# Step 2: Set Up Database
|
| 24 |
connection = sqlite3.connect("attendance.db", check_same_thread=False)
|
|
|
|
| 38 |
connection.commit()
|
| 39 |
|
| 40 |
# Step 3: Detect and Recognize Faces
|
| 41 |
+
def detect_and_mark_attendance(uploaded_image):
|
| 42 |
+
image = np.array(uploaded_image)
|
| 43 |
+
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 44 |
face_locations = face_recognition.face_locations(rgb_image)
|
| 45 |
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
| 46 |
|
|
|
|
| 54 |
log_attendance(name) # Log to database
|
| 55 |
|
| 56 |
# Draw a rectangle around the face
|
| 57 |
+
cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 3)
|
| 58 |
+
cv2.putText(image, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 59 |
|
| 60 |
+
return image
|
| 61 |
|
| 62 |
# Step 4: Fetch Attendance Logs
|
| 63 |
def fetch_attendance():
|
|
|
|
| 65 |
rows = cursor.fetchall()
|
| 66 |
return rows
|
| 67 |
|
| 68 |
+
# Streamlit Interface
|
| 69 |
+
st.title("Face Recognition and Attendance System")
|
| 70 |
+
st.write("Upload an image to detect faces and mark attendance.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Image upload
|
| 73 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
if uploaded_image is not None:
|
| 76 |
+
image = Image.open(uploaded_image)
|
| 77 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 78 |
+
|
| 79 |
+
# Process image
|
| 80 |
+
image_np = np.array(image)
|
| 81 |
+
processed_image = detect_and_mark_attendance(image_np)
|
| 82 |
+
st.image(processed_image, caption="Processed Image with Attendance Marked", use_column_width=True)
|
| 83 |
|
| 84 |
+
# Display attendance logs
|
| 85 |
+
st.subheader("Attendance Logs")
|
| 86 |
+
attendance_data = fetch_attendance()
|
| 87 |
+
if attendance_data:
|
| 88 |
+
st.table(attendance_data)
|
| 89 |
+
else:
|
| 90 |
+
st.write("No attendance records found.")
|