Firebase Setup
Firebase Setup
Let's fully rebuild and explain the Firebase setup and how to connect it to the Python
face processing script, step-by-step — clearly and completely so you can proceed with confidence.
Step-by-Step: Firebase Setup & Python Integration (for Face Recognition Smart Door System)
1. Go to Firebase Console.
1. Inside your Firebase project dashboard → click Build > Realtime Database.
2. Click “Create Database” → Choose location → Start in test mode for now.
"rules": {
".read": true,
".write": true
You don’t need to do this manually — the Python code will create these nodes when it writes data.
But for clarity, your database structure will look like:
"authorized_users": {
"face_id_1": {
"name": "John",
"image_url": "https://..."
},
...
},
"unknown_faces": {
"auto_generated_id": {
"image_url": "https://...",
"timestamp": 1713000000
},
"approved": {
"auto_generated_id": {
"image_url": "https://...",
"timestamp": 1713000000
},
"access_logs": {
"auto_generated_id": {
"status": "granted",
"time": 1713000000,
3. Rename it to:
serviceAccountKey.json
This Python script connects to your Firebase Realtime Database and uploads data for:
• Unknown faces
• Logs
import firebase_admin
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
})
You’ll find it in the Firebase Console under Realtime Database > Data > URL
import time
# Store unknown face (image URL and timestamp)
def store_unknown_face(image_url):
ref = db.reference("/unknown_faces")
new_entry = ref.push()
new_entry.set({
"image_url": image_url,
"timestamp": time.time()
})
ref = db.reference("/access_logs")
new_entry = ref.push()
new_entry.set({
"time": time.time(),
})
def store_approved_face(image_url):
ref = db.reference("/approved")
new_entry = ref.push()
new_entry.set({
"image_url": image_url,
"timestamp": time.time()
})
def get_authorized_faces():
ref = db.reference("/authorized_users")
project-folder/
├── templates/
Final Reminder:
• You don’t need to create database nodes manually – code does it.
o Realtime DB is enabled
the python code you gave ..how it will create autimaticlly database?.....for authorized person (initial)
we have to add manually right?
ChatGPT said:
Great questions! Let's go step-by-step and clarify exactly how the Firebase Realtime Database gets
created and how it handles authorized and unknown users:
1. Will the Python code automatically create the Firebase Realtime Database?
YES, partially.
When your server.py connects to Firebase and uses this line:
python
CopyEdit
ref = db.reference("/unknown_faces")
Firebase will automatically create this node (unknown_faces) if it doesn’t already exist.
Similarly, if you later use:
python
CopyEdit
db.reference("/approved_faces")
db.reference("/access_logs")
• These images are stored in a Google Drive folder (let's say called Authorized_Faces).
• These faces are used for matching with new incoming faces.
So yes, you manually add those initial images to the Google Drive folder and label them clearly (like
john.jpg, alice.jpg).
Then in code:
python
CopyEdit
AUTHORIZED_FOLDER_ID = "your-folder-id-here"
The code fetches all images from that Drive folder and uses them to extract face encodings for
matching.
"unknown_faces": {
"-NkD...": {
"image_url": "https://drive.google.com/uc?id=abc123",
"timestamp": 1712345678.9
},
...
},
"approved_faces": {
"-NkZ...": {
"image_url": "https://drive.google.com/uc?id=def456",
"timestamp": 1712346789.1
},
...
},
"access_logs": {
"-NkL...": {
"name": "John",
"status": "granted",
"timestamp": 1712347890.3
},
...
All of this is created automatically from Python + Firebase code as long as you call the respective
functions.
Summary