Update firebase_credentials.json
Browse files- firebase_credentials.json +105 -13
firebase_credentials.json
CHANGED
@@ -1,13 +1,105 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import firebase_admin
|
4 |
+
from firebase_admin import credentials, db
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
from geopy.geocoders import Nominatim
|
8 |
+
from tensorflow.keras.applications import MobileNetV2
|
9 |
+
from tensorflow.keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
|
10 |
+
|
11 |
+
# Initialize Firebase (Load credentials from JSON file)
|
12 |
+
if not firebase_admin._apps:
|
13 |
+
try:
|
14 |
+
cred = credentials.Certificate("firebase_credentials.json")
|
15 |
+
firebase_admin.initialize_app(cred, {
|
16 |
+
'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
17 |
+
})
|
18 |
+
print("✅ Firebase initialized successfully!")
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"❌ Firebase initialization failed: {e}")
|
21 |
+
|
22 |
+
# Load MobileNetV2 pre-trained model
|
23 |
+
mobilenet_model = MobileNetV2(weights="imagenet")
|
24 |
+
|
25 |
+
# Function to classify image using MobileNetV2
|
26 |
+
def classify_image_with_mobilenet(image):
|
27 |
+
try:
|
28 |
+
img = image.resize((224, 224))
|
29 |
+
img_array = np.array(img)
|
30 |
+
img_array = np.expand_dims(img_array, axis=0)
|
31 |
+
img_array = preprocess_input(img_array)
|
32 |
+
predictions = mobilenet_model.predict(img_array)
|
33 |
+
labels = decode_predictions(predictions, top=5)[0]
|
34 |
+
return {label[1]: float(label[2]) for label in labels}
|
35 |
+
except Exception as e:
|
36 |
+
st.error(f"Error during image classification: {e}")
|
37 |
+
return {}
|
38 |
+
|
39 |
+
# Function to get user location
|
40 |
+
def get_user_location():
|
41 |
+
st.write("Fetching location, please allow location access in your browser.")
|
42 |
+
geolocator = Nominatim(user_agent="binsight")
|
43 |
+
try:
|
44 |
+
ip_info = requests.get("https://ipinfo.io/json").json()
|
45 |
+
loc = ip_info.get("loc", "").split(",")
|
46 |
+
latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
|
47 |
+
if latitude and longitude:
|
48 |
+
address = geolocator.reverse(f"{latitude}, {longitude}").address
|
49 |
+
return latitude, longitude, address
|
50 |
+
except Exception as e:
|
51 |
+
st.error(f"Error retrieving location: {e}")
|
52 |
+
return None, None, None
|
53 |
+
|
54 |
+
# User Login
|
55 |
+
st.sidebar.header("User Login")
|
56 |
+
user_email = st.sidebar.text_input("Enter your email")
|
57 |
+
login_button = st.sidebar.button("Login")
|
58 |
+
|
59 |
+
if login_button:
|
60 |
+
if user_email:
|
61 |
+
st.session_state["user_email"] = user_email
|
62 |
+
st.sidebar.success(f"Logged in as {user_email}")
|
63 |
+
|
64 |
+
if "user_email" not in st.session_state:
|
65 |
+
st.warning("Please log in first.")
|
66 |
+
st.stop()
|
67 |
+
|
68 |
+
# Get user location
|
69 |
+
latitude, longitude, address = get_user_location()
|
70 |
+
if latitude and longitude:
|
71 |
+
st.success(f"Location detected: {address}")
|
72 |
+
else:
|
73 |
+
st.warning("Unable to fetch location, please enable location access.")
|
74 |
+
st.stop()
|
75 |
+
|
76 |
+
# Streamlit App
|
77 |
+
st.title("BinSight: Upload Dustbin Image")
|
78 |
+
|
79 |
+
uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"])
|
80 |
+
submit_button = st.button("Analyze and Upload")
|
81 |
+
|
82 |
+
if submit_button and uploaded_file:
|
83 |
+
image = Image.open(uploaded_file)
|
84 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
85 |
+
|
86 |
+
classification_results = classify_image_with_mobilenet(image)
|
87 |
+
|
88 |
+
if classification_results:
|
89 |
+
db_ref = db.reference("dustbins")
|
90 |
+
dustbin_data = {
|
91 |
+
"user_email": st.session_state["user_email"],
|
92 |
+
"latitude": latitude,
|
93 |
+
"longitude": longitude,
|
94 |
+
"address": address,
|
95 |
+
"classification": classification_results,
|
96 |
+
"allocated_truck": None,
|
97 |
+
"status": "Pending"
|
98 |
+
}
|
99 |
+
db_ref.push(dustbin_data)
|
100 |
+
st.success("Dustbin data uploaded successfully!")
|
101 |
+
st.write(f"**Location:** {address}")
|
102 |
+
st.write(f"**Latitude:** {latitude}, **Longitude:** {longitude}")
|
103 |
+
else:
|
104 |
+
st.error("Missing classification details. Cannot upload.")
|
105 |
+
|