Absolutely!
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)
PART 1: Firebase Setup (Console Side)
Step 1: Create a Firebase Project
1. Go to Firebase Console.
2. Click “Add Project” → Give your project a name (e.g., SmartFaceDoor).
3. Accept the terms and create the project.
4. Wait for the project to be set up.
Step 2: Enable Realtime Database
1. Inside your Firebase project dashboard → click Build > Realtime Database.
2. Click “Create Database” → Choose location → Start in test mode for now.
3. You'll see a blank database structure like this:
"rules": {
".read": true,
".write": true
Step 3: Set Up Project Structure in Database
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,
"source": "ESP32" // or "Manual"
You’ll get this automatically as you run the code.
Step 4: Generate Admin SDK Key (serviceAccountKey.json)
1. Go to Project Settings > Service Accounts.
2. Click “Generate New Private Key” → Download the .json file.
3. Rename it to:
serviceAccountKey.json
4. Put it in your Python project folder next to server.py.
Done with Firebase Console!
PART 2: Connect Python Flask App to Firebase
This Python script connects to your Firebase Realtime Database and uploads data for:
• Unknown faces
• Approved remote unlocks
• Logs
Step 1: Install Firebase Admin SDK in Python
In your terminal or command prompt, run:
pip install firebase-admin
Step 2: Add Python Code to Connect to Firebase
In your server.py, add this setup at the top:
import firebase_admin
from firebase_admin import credentials, db
# Load your Firebase Admin SDK key
cred = credentials.Certificate("serviceAccountKey.json")
# Initialize the Firebase app with the Realtime Database URL
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://<your-project-id>.firebaseio.com/' # Replace this
})
Replace <your-project-id> with your actual Firebase project ID
(e.g., smartfacedoor-default-rtdb.firebaseio.com)
You’ll find it in the Firebase Console under Realtime Database > Data > URL
Step 3: Create Functions for Firebase Operations
Now add these Python functions to upload and retrieve data:
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()
})
# Store access approval log
def store_access_log(status, source):
ref = db.reference("/access_logs")
new_entry = ref.push()
new_entry.set({
"status": status, # "granted" or "denied"
"time": time.time(),
"source": source # "ESP32" or "Web"
})
# Store approved face remotely
def store_approved_face(image_url):
ref = db.reference("/approved")
new_entry = ref.push()
new_entry.set({
"image_url": image_url,
"timestamp": time.time()
})
# Get authorized users (to match against)
def get_authorized_faces():
ref = db.reference("/authorized_users")
return ref.get() # Returns a dictionary of users
Folder Structure Recap (after Firebase is set up):
project-folder/
├── serviceAccountKey.json Firebase credential
├── server.py Your Flask app
├── templates/
│ └── index.html Web interface
Final Reminder:
• You don’t need to create database nodes manually – code does it.
• Just make sure:
o Firebase project is created
o Realtime DB is enabled
o serviceAccountKey.json is placed correctly
o firebase-admin package is installed
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")
…they will also be created automatically during runtime.
2. What about "authorized_faces" (initial face dataset)?
This part requires manual setup, because:
• You need to store the images of known/authorized people.
• 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.
Final Firebase Structure (Auto-Created by Code)
Your Realtime Database will look like this after usage:
"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
Task Automatic? Notes
Create Firebase database Yes Nodes are auto-created on first push
Upload unknown face Yes Goes to Google Drive + Firebase (unknown_faces)
Store access logs Yes Logs entry attempts
Add authorized faces No Manually upload to Google Drive folder (Authorized_Faces)