Files changed (1) hide show
  1. app.py +34 -6
app.py CHANGED
@@ -1,13 +1,14 @@
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:
@@ -16,6 +17,9 @@ if not firebase_admin._apps:
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
 
@@ -48,6 +52,22 @@ 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")
@@ -65,7 +85,7 @@ if "user_email" not in st.session_state:
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()
@@ -80,6 +100,16 @@ 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:
@@ -95,7 +125,7 @@ if submit_button and uploaded_file:
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.")
@@ -104,8 +134,6 @@ if submit_button and uploaded_file:
104
 
105
 
106
 
107
-
108
-
109
  # best with firebase but below code is not giving correct location of user.
110
 
111
  # import streamlit as st
 
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
+ from tensorflow.keras import backend as K
11
+ from PIL import ExifTags
12
 
13
  # Initialize Firebase
14
  if not firebase_admin._apps:
 
17
  'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
18
  })
19
 
20
+ # Clear previous TensorFlow session to avoid session-related errors
21
+ K.clear_session()
22
+
23
  # Load MobileNetV2 pre-trained model
24
  mobilenet_model = MobileNetV2(weights="imagenet")
25
 
 
52
  st.error(f"Error retrieving location: {e}")
53
  return None, None, None
54
 
55
+ # Function to extract GPS location from EXIF data (if available)
56
+ def get_image_location(image):
57
+ try:
58
+ exif_data = image._getexif()
59
+ if exif_data is not None:
60
+ for tag, value in exif_data.items():
61
+ if tag == 34853: # GPS tag
62
+ gps_info = value
63
+ latitude = gps_info[2]
64
+ longitude = gps_info[4]
65
+ return latitude, longitude
66
+ return None, None
67
+ except Exception as e:
68
+ st.error(f"Error retrieving EXIF data: {e}")
69
+ return None, None
70
+
71
  # User Login
72
  st.sidebar.header("User Login")
73
  user_email = st.sidebar.text_input("Enter your email")
 
85
  # Get user location and display details
86
  latitude, longitude, address = get_user_location()
87
  if latitude and longitude:
88
+ st.success(f"User's live location detected: {address}")
89
  else:
90
  st.warning("Unable to fetch location, please ensure location access is enabled.")
91
  st.stop()
 
100
  image = Image.open(uploaded_file)
101
  st.image(image, caption="Uploaded Image", use_container_width=True)
102
 
103
+ # Get the GPS data from the image's EXIF
104
+ img_lat, img_lon = get_image_location(image)
105
+ if img_lat and img_lon:
106
+ st.write(f"**Image Location (from EXIF):** Latitude: {img_lat}, Longitude: {img_lon}")
107
+ geolocator = Nominatim(user_agent="binsight")
108
+ img_address = geolocator.reverse(f"{img_lat}, {img_lon}").address
109
+ st.write(f"**Image Address (from EXIF):** {img_address}")
110
+ else:
111
+ st.warning("No GPS information found in the image EXIF.")
112
+
113
  classification_results = classify_image_with_mobilenet(image)
114
 
115
  if classification_results:
 
125
  }
126
  db_ref.push(dustbin_data)
127
  st.success("Dustbin data uploaded successfully!")
128
+ st.write(f"**User Location:** {address}")
129
  st.write(f"**Latitude:** {latitude}, **Longitude:** {longitude}")
130
  else:
131
  st.error("Missing classification details. Cannot upload.")
 
134
 
135
 
136
 
 
 
137
  # best with firebase but below code is not giving correct location of user.
138
 
139
  # import streamlit as st