Spaces:
Sleeping
Sleeping
NTAMBARA Etienne
commited on
Commit
·
355a562
1
Parent(s):
457306d
Changes Made Keys p3
Browse files
app.py
CHANGED
@@ -2,81 +2,63 @@ import gradio as gr
|
|
2 |
import face_recognition
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
-
import os
|
6 |
from PIL import Image
|
7 |
import pickle
|
8 |
-
import io
|
9 |
import firebase_admin
|
10 |
from firebase_admin import credentials
|
11 |
-
from firebase_admin import db
|
12 |
from firebase_admin import storage
|
13 |
-
from datetime import datetime
|
14 |
|
15 |
# Initialize Firebase
|
16 |
-
cred = credentials.Certificate("serviceAccountKey.json")
|
17 |
firebase_admin.initialize_app(cred, {
|
18 |
-
'databaseURL': 'https://faceantendancerealtime-default-rtdb.firebaseio.com/',
|
19 |
'storageBucket': 'faceantendancerealtime.appspot.com'
|
20 |
})
|
21 |
bucket = storage.bucket()
|
22 |
|
23 |
-
#
|
24 |
-
def
|
25 |
-
# Code to download the 'EncodeFile.p' from Firebase Storage
|
26 |
-
# Assume 'EncodeFile.p' is already uploaded to Firebase Storage
|
27 |
blob = bucket.blob('EncodeFile.p')
|
28 |
-
blob.download_to_filename('
|
29 |
with open('EncodeFile.p', 'rb') as file:
|
30 |
-
|
31 |
-
return encodeListKnownWithIds
|
32 |
|
33 |
-
encodeListKnownWithIds =
|
34 |
encodeListKnown, studentsIds = encodeListKnownWithIds
|
35 |
|
36 |
def recognize_face(input_image):
|
37 |
-
# Convert
|
38 |
img = np.array(input_image)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# Find faces in the image
|
47 |
-
face_locations = face_recognition.face_locations(img_small)
|
48 |
-
face_encodings = face_recognition.face_encodings(img_small, face_locations)
|
49 |
-
|
50 |
-
# Convert the coordinates to full scale since the image was scaled to 1/4 size
|
51 |
-
face_locations = [(top*4, right*4, bottom*4, left*4) for top, right, bottom, left in face_locations]
|
52 |
-
|
53 |
# Recognize faces
|
54 |
-
for
|
55 |
matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
|
56 |
name = "Unknown"
|
57 |
-
|
58 |
-
# Use the known face with the smallest distance to the new face
|
59 |
face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
|
60 |
best_match_index = np.argmin(face_distances)
|
61 |
if matches[best_match_index]:
|
62 |
name = studentsIds[best_match_index]
|
63 |
-
|
64 |
-
# Draw
|
65 |
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
|
66 |
cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
|
67 |
-
|
68 |
-
# Convert the image back to PIL format
|
69 |
-
return Image.fromarray(img)
|
70 |
|
71 |
-
#
|
|
|
|
|
|
|
72 |
iface = gr.Interface(
|
73 |
fn=recognize_face,
|
74 |
-
inputs=gr.
|
75 |
-
outputs=gr.
|
76 |
title="Face Recognition Attendance System",
|
77 |
-
description="Upload an image to identify
|
78 |
)
|
79 |
|
80 |
-
# Run the Gradio app
|
81 |
if __name__ == "__main__":
|
82 |
iface.launch(inline=False)
|
|
|
2 |
import face_recognition
|
3 |
import cv2
|
4 |
import numpy as np
|
|
|
5 |
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 |
'storageBucket': 'faceantendancerealtime.appspot.com'
|
15 |
})
|
16 |
bucket = storage.bucket()
|
17 |
|
18 |
+
# Function to download face encodings from Firebase Storage
|
19 |
+
def download_encodings():
|
|
|
|
|
20 |
blob = bucket.blob('EncodeFile.p')
|
21 |
+
blob.download_to_filename('EncodeFile.p')
|
22 |
with open('EncodeFile.p', 'rb') as file:
|
23 |
+
return pickle.load(file)
|
|
|
24 |
|
25 |
+
encodeListKnownWithIds = download_encodings()
|
26 |
encodeListKnown, studentsIds = encodeListKnownWithIds
|
27 |
|
28 |
def recognize_face(input_image):
|
29 |
+
# Convert PIL Image to numpy array
|
30 |
img = np.array(input_image)
|
31 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
32 |
+
|
33 |
+
# Detect faces and encode
|
34 |
+
face_locations = face_recognition.face_locations(img)
|
35 |
+
face_encodings = face_recognition.face_encodings(img, face_locations)
|
36 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Recognize faces
|
38 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
39 |
matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
|
40 |
name = "Unknown"
|
41 |
+
|
|
|
42 |
face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
|
43 |
best_match_index = np.argmin(face_distances)
|
44 |
if matches[best_match_index]:
|
45 |
name = studentsIds[best_match_index]
|
46 |
+
|
47 |
+
# Draw rectangle and label
|
48 |
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
|
49 |
cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
|
|
|
|
|
|
|
50 |
|
51 |
+
# Convert back to PIL Image
|
52 |
+
return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
53 |
+
|
54 |
+
# Gradio interface
|
55 |
iface = gr.Interface(
|
56 |
fn=recognize_face,
|
57 |
+
inputs=gr.Image(type="pil"),
|
58 |
+
outputs=gr.Image(type="pil"),
|
59 |
title="Face Recognition Attendance System",
|
60 |
+
description="Upload an image to identify individuals."
|
61 |
)
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
iface.launch(inline=False)
|