Files changed (1) hide show
  1. app.py +4 -82
app.py CHANGED
@@ -1,43 +1,13 @@
1
  import streamlit as st
2
  import requests
3
- import firebase_admin
4
- from firebase_admin import credentials, db, auth
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
- import json
11
-
12
- # Initialize Firebase
13
- if not firebase_admin._apps:
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
-
19
- # Load MobileNetV2 pre-trained model
20
- mobilenet_model = MobileNetV2(weights="imagenet")
21
-
22
- # Function to classify the uploaded image using MobileNetV2
23
- def classify_image_with_mobilenet(image):
24
- try:
25
- img = image.resize((224, 224))
26
- img_array = np.array(img)
27
- img_array = np.expand_dims(img_array, axis=0)
28
- img_array = preprocess_input(img_array)
29
- predictions = mobilenet_model.predict(img_array)
30
- labels = decode_predictions(predictions, top=5)[0]
31
- return {label[1]: float(label[2]) for label in labels}
32
- except Exception as e:
33
- st.error(f"Error during image classification: {e}")
34
- return {}
35
 
36
- # Function to get user's location using geolocation API
37
  def get_user_location():
38
  st.write("Fetching location, please allow location access in your browser.")
39
  geolocator = Nominatim(user_agent="binsight")
40
  try:
 
41
  ip_info = requests.get("https://ipinfo.io/json").json()
42
  loc = ip_info.get("loc", "").split(",")
43
  latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
@@ -48,62 +18,14 @@ def get_user_location():
48
  st.error(f"Error retrieving location: {e}")
49
  return None, None, None
50
 
51
- # User Login
52
- st.sidebar.header("User Login")
53
- user_email = st.sidebar.text_input("Enter your email")
54
- login_button = st.sidebar.button("Login")
55
-
56
- if login_button:
57
- if user_email:
58
- st.session_state["user_email"] = user_email
59
- st.sidebar.success(f"Logged in as {user_email}")
60
-
61
- if "user_email" not in st.session_state:
62
- st.warning("Please log in first.")
63
- st.stop()
64
-
65
  # Get user location and display details
66
  latitude, longitude, address = get_user_location()
 
67
  if latitude and longitude:
68
  st.success(f"Location detected: {address}")
 
69
  else:
70
  st.warning("Unable to fetch location, please ensure location access is enabled.")
71
- st.stop()
72
-
73
- # Streamlit App
74
- st.title("BinSight: Upload Dustbin Image")
75
-
76
- uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"])
77
- submit_button = st.button("Analyze and Upload")
78
-
79
- if submit_button and uploaded_file:
80
- image = Image.open(uploaded_file)
81
- st.image(image, caption="Uploaded Image", use_container_width=True)
82
-
83
- classification_results = classify_image_with_mobilenet(image)
84
-
85
- if classification_results:
86
- db_ref = db.reference("dustbins")
87
- dustbin_data = {
88
- "user_email": st.session_state["user_email"],
89
- "latitude": latitude,
90
- "longitude": longitude,
91
- "address": address,
92
- "classification": classification_results,
93
- "allocated_truck": None,
94
- "status": "Pending"
95
- }
96
- db_ref.push(dustbin_data)
97
- st.success("Dustbin data uploaded successfully!")
98
- st.write(f"**Location:** {address}")
99
- st.write(f"**Latitude:** {latitude}, **Longitude:** {longitude}")
100
- else:
101
- st.error("Missing classification details. Cannot upload.")
102
-
103
-
104
-
105
-
106
-
107
 
108
 
109
  # best with firebase but below code is not giving correct location of user.
 
1
  import streamlit as st
2
  import requests
 
 
 
 
3
  from geopy.geocoders import Nominatim
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Function to get the user's live location based on IP
6
  def get_user_location():
7
  st.write("Fetching location, please allow location access in your browser.")
8
  geolocator = Nominatim(user_agent="binsight")
9
  try:
10
+ # Get the IP-based location
11
  ip_info = requests.get("https://ipinfo.io/json").json()
12
  loc = ip_info.get("loc", "").split(",")
13
  latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
 
18
  st.error(f"Error retrieving location: {e}")
19
  return None, None, None
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Get user location and display details
22
  latitude, longitude, address = get_user_location()
23
+
24
  if latitude and longitude:
25
  st.success(f"Location detected: {address}")
26
+ st.write(f"**Latitude:** {latitude}, **Longitude:** {longitude}")
27
  else:
28
  st.warning("Unable to fetch location, please ensure location access is enabled.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
 
31
  # best with firebase but below code is not giving correct location of user.