NTAMBARA Etienne commited on
Commit
74b2d91
·
1 Parent(s): 036e11b

Add initial project files

Browse files
Files changed (3) hide show
  1. .gitignore +45 -0
  2. app.py +81 -0
  3. requirements.txt.txt +6 -0
.gitignore ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Firebase credentials
2
+ serviceAccountKey.json
3
+
4
+ # Encoded face data
5
+ EncodeFile.p
6
+
7
+ # Temporary files
8
+ *.pyc
9
+ __pycache__/
10
+ .tmp/
11
+
12
+ # Dataset directories (if they contain sensitive or large data)
13
+ Resources/
14
+ Images/
15
+
16
+ # Operating system files
17
+ .DS_Store
18
+ Thumbs.db
19
+
20
+ # Editor/IDE files
21
+ .vscode/
22
+ .idea/
23
+
24
+ # Python virtual environment
25
+ venv/
26
+ env/
27
+
28
+ # Jupyter notebook checkpoints
29
+ .ipynb_checkpoints/
30
+
31
+ # Gradio cached examples
32
+ gradio_cached_examples/
33
+
34
+ # Files and directories generated during execution (replace with your specific paths if they differ)
35
+ output/
36
+ *.log
37
+ captures/
38
+
39
+ # If using SQLite or other local DBs
40
+ *.db
41
+ *.sqlite
42
+ *.sqlite3
43
+
44
+ # Any additional directories you have that contain generated images or mode images
45
+ Modes/
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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 io
8
+ import firebase_admin
9
+ from firebase_admin import credentials
10
+ from firebase_admin import db
11
+ from firebase_admin import storage
12
+ from datetime import datetime
13
+
14
+ # Initialize Firebase
15
+ cred = credentials.Certificate("path/to/serviceAccountKey.json") # Update the path to your Firebase credentials
16
+ firebase_admin.initialize_app(cred, {
17
+ 'databaseURL': 'https://faceantendancerealtime-default-rtdb.firebaseio.com/',
18
+ 'storageBucket': 'faceantendancerealtime.appspot.com'
19
+ })
20
+ bucket = storage.bucket()
21
+
22
+ # Load the known face encodings and their IDs from Firebase
23
+ def load_known_encodings():
24
+ # Code to download the 'EncodeFile.p' from Firebase Storage
25
+ # Assume 'EncodeFile.p' is already uploaded to Firebase Storage
26
+ blob = bucket.blob('EncodeFile.p')
27
+ blob.download_to_filename('/tmp/EncodeFile.p')
28
+ with open('/tmp/EncodeFile.p', 'rb') as file:
29
+ encodeListKnownWithIds = pickle.load(file)
30
+ return encodeListKnownWithIds
31
+
32
+ encodeListKnownWithIds = load_known_encodings()
33
+ encodeListKnown, studentsIds = encodeListKnownWithIds
34
+
35
+ def recognize_face(input_image):
36
+ # Convert the PIL Image to a numpy array
37
+ img = np.array(input_image)
38
+
39
+ # Convert the image to RGB
40
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
41
+
42
+ # Resize image for faster processing
43
+ img_small = cv2.resize(img, (0, 0), None, 0.25, 0.25)
44
+
45
+ # Find faces in the image
46
+ face_locations = face_recognition.face_locations(img_small)
47
+ face_encodings = face_recognition.face_encodings(img_small, face_locations)
48
+
49
+ # Convert the coordinates to full scale since the image was scaled to 1/4 size
50
+ face_locations = [(top*4, right*4, bottom*4, left*4) for top, right, bottom, left in face_locations]
51
+
52
+ # Recognize faces
53
+ for face_encoding, (top, right, bottom, left) in zip(face_encodings, face_locations):
54
+ matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
55
+ name = "Unknown"
56
+
57
+ # Use the known face with the smallest distance to the new face
58
+ face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
59
+ best_match_index = np.argmin(face_distances)
60
+ if matches[best_match_index]:
61
+ name = studentsIds[best_match_index]
62
+
63
+ # Draw rectangles and names on the original image
64
+ cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
65
+ cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
66
+
67
+ # Convert the image back to PIL format
68
+ return Image.fromarray(img)
69
+
70
+ # Define the Gradio interface
71
+ iface = gr.Interface(
72
+ fn=recognize_face,
73
+ inputs=gr.inputs.Image(type="pil"),
74
+ outputs=gr.outputs.Image(type="pil"),
75
+ title="Face Recognition Attendance System",
76
+ description="Upload an image to identify registered students."
77
+ )
78
+
79
+ # Run the Gradio app
80
+ if __name__ == "__main__":
81
+ iface.launch()
requirements.txt.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ face_recognition
3
+ opencv-python-headless
4
+ numpy
5
+ Pillow
6
+ firebase-admin