Update app.py
#3
by
MLDeveloper
- opened
app.py
CHANGED
@@ -34,6 +34,7 @@ def classify_image_with_mobilenet(image):
|
|
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")
|
@@ -42,68 +43,27 @@ def get_user_location():
|
|
42 |
loc = ip_info.get("loc", "").split(",")
|
43 |
latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
|
44 |
if latitude and longitude:
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
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.
|
|
|
34 |
return {}
|
35 |
|
36 |
# Function to get user's location using geolocation API
|
37 |
+
# Function to get user's location using geolocation API and extract more detailed information
|
38 |
def get_user_location():
|
39 |
st.write("Fetching location, please allow location access in your browser.")
|
40 |
geolocator = Nominatim(user_agent="binsight")
|
|
|
43 |
loc = ip_info.get("loc", "").split(",")
|
44 |
latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
|
45 |
if latitude and longitude:
|
46 |
+
location = geolocator.reverse(f"{latitude}, {longitude}", language='en')
|
47 |
+
if location:
|
48 |
+
address = location.address
|
49 |
+
details = location.raw.get('address', {})
|
50 |
+
city = details.get('city', 'N/A')
|
51 |
+
state = details.get('state', 'N/A')
|
52 |
+
country = details.get('country', 'N/A')
|
53 |
+
return latitude, longitude, address, city, state, country
|
54 |
except Exception as e:
|
55 |
st.error(f"Error retrieving location: {e}")
|
56 |
+
return None, None, None, None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Get user location and display details
|
59 |
+
latitude, longitude, address, city, state, country = get_user_location()
|
60 |
if latitude and longitude:
|
61 |
st.success(f"Location detected: {address}")
|
62 |
+
st.write(f"**City:** {city}, **State:** {state}, **Country:** {country}")
|
63 |
else:
|
64 |
st.warning("Unable to fetch location, please ensure location access is enabled.")
|
65 |
st.stop()
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
# best with firebase but below code is not giving correct location of user.
|