Spaces:
Sleeping
Sleeping
NTAMBARA Etienne
commited on
Commit
·
935b026
1
Parent(s):
dbd8adf
Changes Made Keys p3
Browse files
app.py
CHANGED
@@ -6,14 +6,14 @@ from PIL import Image
|
|
6 |
import pickle
|
7 |
import firebase_admin
|
8 |
from firebase_admin import credentials
|
|
|
9 |
from firebase_admin import storage
|
10 |
|
11 |
# Initialize Firebase
|
12 |
cred = credentials.Certificate("serviceAccountKey.json") # Update with your credentials path
|
13 |
-
firebase_admin.initialize_app(cred,{
|
14 |
-
'databaseURL': 'https://faceantendancerealtime-default-rtdb.firebaseio.com/',
|
15 |
-
'storageBucket': 'faceantendancerealtime.appspot.com'
|
16 |
-
|
17 |
})
|
18 |
bucket = storage.bucket()
|
19 |
|
@@ -31,36 +31,48 @@ def recognize_face(input_image):
|
|
31 |
# Convert PIL Image to numpy array
|
32 |
img = np.array(input_image)
|
33 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
34 |
-
|
35 |
# Detect faces and encode
|
36 |
face_locations = face_recognition.face_locations(img)
|
37 |
face_encodings = face_recognition.face_encodings(img, face_locations)
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
|
42 |
name = "Unknown"
|
|
|
43 |
|
44 |
face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
|
45 |
best_match_index = np.argmin(face_distances)
|
46 |
if matches[best_match_index]:
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Convert back to PIL Image
|
54 |
-
|
|
|
55 |
|
56 |
# Gradio interface
|
57 |
iface = gr.Interface(
|
58 |
fn=recognize_face,
|
59 |
inputs=gr.Image(type="pil"),
|
60 |
-
outputs=gr.Image(type="pil"),
|
61 |
title="Face Recognition Attendance System",
|
62 |
description="Upload an image to identify individuals."
|
63 |
)
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
-
iface.launch(inline=False
|
|
|
6 |
import pickle
|
7 |
import firebase_admin
|
8 |
from firebase_admin import credentials
|
9 |
+
from firebase_admin import db
|
10 |
from firebase_admin import storage
|
11 |
|
12 |
# Initialize Firebase
|
13 |
cred = credentials.Certificate("serviceAccountKey.json") # Update with your credentials path
|
14 |
+
firebase_app = firebase_admin.initialize_app(cred, {
|
15 |
+
'databaseURL': 'https://faceantendancerealtime-default-rtdb.firebaseio.com/',
|
16 |
+
'storageBucket': 'faceantendancerealtime.appspot.com'
|
|
|
17 |
})
|
18 |
bucket = storage.bucket()
|
19 |
|
|
|
31 |
# Convert PIL Image to numpy array
|
32 |
img = np.array(input_image)
|
33 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
|
|
34 |
# Detect faces and encode
|
35 |
face_locations = face_recognition.face_locations(img)
|
36 |
face_encodings = face_recognition.face_encodings(img, face_locations)
|
37 |
+
# Initialize the database reference
|
38 |
+
ref = db.reference('Students')
|
39 |
+
|
40 |
+
# Recognize faces and fetch data from the database
|
41 |
+
results = []
|
42 |
+
for face_encoding in face_encodings:
|
43 |
matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
|
44 |
name = "Unknown"
|
45 |
+
student_info = {}
|
46 |
|
47 |
face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
|
48 |
best_match_index = np.argmin(face_distances)
|
49 |
if matches[best_match_index]:
|
50 |
+
student_id = studentsIds[best_match_index]
|
51 |
+
student_info = ref.child(student_id).get()
|
52 |
|
53 |
+
if student_info:
|
54 |
+
name = student_info['name']
|
55 |
+
results.append(student_info)
|
56 |
+
else:
|
57 |
+
results.append({'name': 'Unknown'})
|
58 |
+
|
59 |
+
# Draw rectangles around the faces
|
60 |
+
for (top, right, bottom, left), name in zip(face_locations, [student_info.get('name', 'Unknown') for student_info in results]):
|
61 |
+
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
|
62 |
+
cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
|
63 |
|
64 |
# Convert back to PIL Image
|
65 |
+
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
66 |
+
return pil_img, results
|
67 |
|
68 |
# Gradio interface
|
69 |
iface = gr.Interface(
|
70 |
fn=recognize_face,
|
71 |
inputs=gr.Image(type="pil"),
|
72 |
+
outputs=[gr.Image(type="pil"), gr.JSON(label="Student Information")],
|
73 |
title="Face Recognition Attendance System",
|
74 |
description="Upload an image to identify individuals."
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
+
iface.launch(debug=True,inline=False)
|