File size: 20,605 Bytes
4a2da89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
import streamlit as st
import requests
import firebase_admin
from firebase_admin import credentials, db, auth
from PIL import Image
import numpy as np
from geopy.geocoders import Nominatim
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
import json
# Initialize Firebase
if not firebase_admin._apps:
cred = credentials.Certificate("firebase_credentials.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
})
# Load MobileNetV2 pre-trained model
mobilenet_model = MobileNetV2(weights="imagenet")
# Function to classify the uploaded image using MobileNetV2
def classify_image_with_mobilenet(image):
try:
img = image.resize((224, 224))
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
predictions = mobilenet_model.predict(img_array)
labels = decode_predictions(predictions, top=5)[0]
return {label[1]: float(label[2]) for label in labels}
except Exception as e:
st.error(f"Error during image classification: {e}")
return {}
# Function to get user's location using geolocation API
def get_user_location():
st.write("Fetching location, please allow location access in your browser.")
geolocator = Nominatim(user_agent="binsight")
try:
ip_info = requests.get("https://ipinfo.io/json").json()
loc = ip_info.get("loc", "").split(",")
latitude, longitude = loc[0], loc[1] if len(loc) == 2 else (None, None)
if latitude and longitude:
address = geolocator.reverse(f"{latitude}, {longitude}").address
return latitude, longitude, address
except Exception as e:
st.error(f"Error retrieving location: {e}")
return None, None, None
# User Login
st.sidebar.header("User Login")
user_email = st.sidebar.text_input("Enter your email")
login_button = st.sidebar.button("Login")
if login_button:
if user_email:
st.session_state["user_email"] = user_email
st.sidebar.success(f"Logged in as {user_email}")
if "user_email" not in st.session_state:
st.warning("Please log in first.")
st.stop()
# Get user location and display details
latitude, longitude, address = get_user_location()
if latitude and longitude:
st.success(f"Location detected: {address}")
else:
st.warning("Unable to fetch location, please ensure location access is enabled.")
st.stop()
# Streamlit App
st.title("BinSight: Upload Dustbin Image")
uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"])
submit_button = st.button("Analyze and Upload")
if submit_button and uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
classification_results = classify_image_with_mobilenet(image)
if classification_results:
db_ref = db.reference("dustbins")
dustbin_data = {
"user_email": st.session_state["user_email"],
"latitude": latitude,
"longitude": longitude,
"address": address,
"classification": classification_results,
"allocated_truck": None,
"status": "Pending"
}
db_ref.push(dustbin_data)
st.success("Dustbin data uploaded successfully!")
st.write(f"**Location:** {address}")
st.write(f"**Latitude:** {latitude}, **Longitude:** {longitude}")
else:
st.error("Missing classification details. Cannot upload.")
# best with firebase but below code is not giving correct location of user.
# import streamlit as st
# import requests
# import firebase_admin
# from firebase_admin import credentials, db, auth
# from PIL import Image
# import numpy as np
# from geopy.geocoders import Nominatim
# from tensorflow.keras.applications import MobileNetV2
# from tensorflow.keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
# # Initialize Firebase
# if not firebase_admin._apps:
# cred = credentials.Certificate("firebase_credentials.json")
# firebase_admin.initialize_app(cred, {
# 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
# })
# # Load MobileNetV2 pre-trained model
# mobilenet_model = MobileNetV2(weights="imagenet")
# # Function to classify the uploaded image using MobileNetV2
# def classify_image_with_mobilenet(image):
# try:
# img = image.resize((224, 224))
# img_array = np.array(img)
# img_array = np.expand_dims(img_array, axis=0)
# img_array = preprocess_input(img_array)
# predictions = mobilenet_model.predict(img_array)
# labels = decode_predictions(predictions, top=5)[0]
# return {label[1]: float(label[2]) for label in labels}
# except Exception as e:
# st.error(f"Error during image classification: {e}")
# return {}
# # Function to get user's location
# def get_user_location():
# try:
# ip_info = requests.get("https://ipinfo.io/json").json()
# location = ip_info.get("loc", "").split(",")
# latitude = location[0] if len(location) > 0 else None
# longitude = location[1] if len(location) > 1 else None
# if latitude and longitude:
# geolocator = Nominatim(user_agent="binsight")
# address = geolocator.reverse(f"{latitude}, {longitude}").address
# return latitude, longitude, address
# return None, None, None
# except Exception as e:
# st.error(f"Unable to get location: {e}")
# return None, None, None
# # User Login
# st.sidebar.header("User Login")
# user_email = st.sidebar.text_input("Enter your email")
# login_button = st.sidebar.button("Login")
# if login_button:
# if user_email:
# st.session_state["user_email"] = user_email
# st.sidebar.success(f"Logged in as {user_email}")
# if "user_email" not in st.session_state:
# st.warning("Please log in first.")
# st.stop()
# # Streamlit App
# st.title("BinSight: Upload Dustbin Image")
# uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"])
# submit_button = st.button("Analyze and Upload")
# if submit_button and uploaded_file:
# image = Image.open(uploaded_file)
# st.image(image, caption="Uploaded Image", use_container_width=True)
# classification_results = classify_image_with_mobilenet(image)
# latitude, longitude, address = get_user_location()
# if latitude and longitude and classification_results:
# db_ref = db.reference("dustbins")
# dustbin_data = {
# "user_email": st.session_state["user_email"],
# "latitude": latitude,
# "longitude": longitude,
# "address": address,
# "classification": classification_results,
# "allocated_truck": None,
# "status": "Pending"
# }
# db_ref.push(dustbin_data)
# st.success("Dustbin data uploaded successfully!")
# else:
# st.error("Missing classification or location details. Cannot upload.")
# Below is the old version but it is without of firebase and here is the addition of gemini.
# import streamlit as st
# import os
# from PIL import Image
# import numpy as np
# from io import BytesIO
# from dotenv import load_dotenv
# from geopy.geocoders import Nominatim
# from tensorflow.keras.applications import MobileNetV2
# from tensorflow.keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
# import requests
# import google.generativeai as genai
# # Load environment variables
# load_dotenv()
# # Configure Generative AI
# genai.configure(api_key='AIzaSyBREh8Uei7uDCbzPaYW2WdalOdjVWcQLAM')
# # Load MobileNetV2 pre-trained model
# mobilenet_model = MobileNetV2(weights="imagenet")
# # Function to classify the uploaded image using MobileNetV2
# def classify_image_with_mobilenet(image):
# try:
# img = image.resize((224, 224))
# img_array = np.array(img)
# img_array = np.expand_dims(img_array, axis=0)
# img_array = preprocess_input(img_array)
# predictions = mobilenet_model.predict(img_array)
# labels = decode_predictions(predictions, top=5)[0]
# return {label[1]: float(label[2]) for label in labels}
# except Exception as e:
# st.error(f"Error during image classification: {e}")
# return {}
# # Function to get user's location
# def get_user_location():
# try:
# ip_info = requests.get("https://ipinfo.io/json").json()
# location = ip_info.get("loc", "").split(",")
# latitude = location[0] if len(location) > 0 else None
# longitude = location[1] if len(location) > 1 else None
# if latitude and longitude:
# geolocator = Nominatim(user_agent="binsight")
# address = geolocator.reverse(f"{latitude}, {longitude}").address
# return latitude, longitude, address
# return None, None, None
# except Exception as e:
# st.error(f"Unable to get location: {e}")
# return None, None, None
# # Function to get nearest municipal details with contact info
# def get_nearest_municipal_details(latitude, longitude):
# try:
# if latitude and longitude:
# # Simulating municipal service retrieval
# municipal_services = [
# {"latitude": "12.9716", "longitude": "77.5946", "office": "Bangalore Municipal Office", "phone": "+91-80-12345678"},
# {"latitude": "28.7041", "longitude": "77.1025", "office": "Delhi Municipal Office", "phone": "+91-11-98765432"},
# {"latitude": "19.0760", "longitude": "72.8777", "office": "Mumbai Municipal Office", "phone": "+91-22-22334455"},
# ]
# # Find the nearest municipal service (mock logic: matching first two decimal points)
# for service in municipal_services:
# if str(latitude).startswith(service["latitude"][:5]) and str(longitude).startswith(service["longitude"][:5]):
# return f"""
# **Office**: {service['office']}
# **Phone**: {service['phone']}
# """
# return "No nearby municipal office found. Please check manually."
# else:
# return "Location not available. Unable to fetch municipal details."
# except Exception as e:
# st.error(f"Unable to fetch municipal details: {e}")
# return None
# # Function to interact with Generative AI
# def get_genai_response(classification_results, location):
# try:
# classification_summary = "\n".join([f"{label}: {score:.2f}" for label, score in classification_results.items()])
# location_summary = f"""
# Latitude: {location[0] if location[0] else 'N/A'}
# Longitude: {location[1] if location[1] else 'N/A'}
# Address: {location[2] if location[2] else 'N/A'}
# """
# prompt = f"""
# ### You are an environmental expert. Analyze the following:
# 1. **Image Classification**:
# - {classification_summary}
# 2. **Location**:
# - {location_summary}
# ### Output Required:
# 1. Detailed insights about the waste detected in the image.
# 2. Specific health risks associated with the detected waste type.
# 3. Precautions to mitigate these health risks.
# 4. Recommendations for proper disposal.
# """
# model = genai.GenerativeModel('gemini-pro')
# response = model.generate_content(prompt)
# return response
# except Exception as e:
# st.error(f"Error using Generative AI: {e}")
# return None
# # Function to display Generative AI response
# def display_genai_response(response):
# st.subheader("Detailed Analysis and Recommendations")
# if response and response.candidates:
# response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
# st.write(response_content)
# else:
# st.write("No response received from Generative AI or quota exceeded.")
# # Streamlit App
# st.title("BinSight: AI-Powered Dustbin and Waste Analysis System")
# st.text("Upload a dustbin image and get AI-powered analysis of the waste and associated health recommendations.")
# uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"], help="Upload a clear image of a dustbin for analysis.")
# submit_button = st.button("Analyze Dustbin")
# if submit_button:
# if uploaded_file is not None:
# image = Image.open(uploaded_file)
# st.image(image, caption="Uploaded Image", use_container_width =True)
# # Classify the image using MobileNetV2
# st.subheader("Image Classification")
# classification_results = classify_image_with_mobilenet(image)
# for label, score in classification_results.items():
# st.write(f"- **{label}**: {score:.2f}")
# # Get user location
# location = get_user_location()
# latitude, longitude, address = location
# st.subheader("User Location")
# st.write(f"Latitude: {latitude if latitude else 'N/A'}")
# st.write(f"Longitude: {longitude if longitude else 'N/A'}")
# st.write(f"Address: {address if address else 'N/A'}")
# # Get nearest municipal details with contact info
# st.subheader("Nearest Municipal Details")
# municipal_details = get_nearest_municipal_details(latitude, longitude)
# st.write(municipal_details)
# # Generate detailed analysis with Generative AI
# if classification_results:
# response = get_genai_response(classification_results, location)
# display_genai_response(response)
# else:
# st.write("Please upload an image for analysis.")
# # import streamlit as st
# # import os
# # from PIL import Image
# # import numpy as np
# # from io import BytesIO
# # from dotenv import load_dotenv
# # from geopy.geocoders import Nominatim
# # from tensorflow.keras.applications import MobileNetV2
# # from tensorflow.keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
# # import requests
# # import google.generativeai as genai
# # # Load environment variables
# # load_dotenv()
# # # Configure Generative AI
# # genai.configure(api_key='AIzaSyBREh8Uei7uDCbzPaYW2WdalOdjVWcQLAM')
# # # Load MobileNetV2 pre-trained model
# # mobilenet_model = MobileNetV2(weights="imagenet")
# # # Function to classify the uploaded image using MobileNetV2
# # def classify_image_with_mobilenet(image):
# # try:
# # # Resize the image to the input size of MobileNetV2
# # img = image.resize((224, 224))
# # img_array = np.array(img)
# # img_array = np.expand_dims(img_array, axis=0)
# # img_array = preprocess_input(img_array)
# # # Predict using the MobileNetV2 model
# # predictions = mobilenet_model.predict(img_array)
# # labels = decode_predictions(predictions, top=5)[0]
# # return {label[1]: float(label[2]) for label in labels}
# # except Exception as e:
# # st.error(f"Error during image classification: {e}")
# # return {}
# # # Function to get user's location
# # def get_user_location():
# # try:
# # # Fetch location using the IPInfo API
# # ip_info = requests.get("https://ipinfo.io/json").json()
# # location = ip_info.get("loc", "").split(",")
# # latitude = location[0] if len(location) > 0 else None
# # longitude = location[1] if len(location) > 1 else None
# # if latitude and longitude:
# # geolocator = Nominatim(user_agent="binsight")
# # address = geolocator.reverse(f"{latitude}, {longitude}").address
# # return latitude, longitude, address
# # return None, None, None
# # except Exception as e:
# # st.error(f"Unable to get location: {e}")
# # return None, None, None
# # # Function to get nearest municipal details
# # def get_nearest_municipal_details(latitude, longitude):
# # try:
# # if latitude and longitude:
# # # Simulating municipal service retrieval
# # return f"The nearest municipal office is at ({latitude}, {longitude}). Please contact your local authority for waste management services."
# # else:
# # return "Location not available. Unable to fetch municipal details."
# # except Exception as e:
# # st.error(f"Unable to fetch municipal details: {e}")
# # return None
# # # Function to interact with Generative AI
# # def get_genai_response(classification_results, location):
# # try:
# # # Construct prompt for Generative AI
# # classification_summary = "\n".join([f"{label}: {score:.2f}" for label, score in classification_results.items()])
# # location_summary = f"""
# # Latitude: {location[0] if location[0] else 'N/A'}
# # Longitude: {location[1] if location[1] else 'N/A'}
# # Address: {location[2] if location[2] else 'N/A'}
# # """
# # prompt = f"""
# # ### You are an environmental expert. Analyze the following:
# # 1. **Image Classification**:
# # - {classification_summary}
# # 2. **Location**:
# # - {location_summary}
# # ### Output Required:
# # 1. Detailed insights about the waste detected in the image.
# # 2. Specific health risks associated with the detected waste type.
# # 3. Precautions to mitigate these health risks.
# # 4. Recommendations for proper disposal.
# # """
# # model = genai.GenerativeModel('gemini-pro')
# # response = model.generate_content(prompt)
# # return response
# # except Exception as e:
# # st.error(f"Error using Generative AI: {e}")
# # return None
# # # Function to display Generative AI response
# # def display_genai_response(response):
# # st.subheader("Detailed Analysis and Recommendations")
# # if response and response.candidates:
# # response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
# # st.write(response_content)
# # else:
# # st.write("No response received from Generative AI or quota exceeded.")
# # # Streamlit App
# # st.title("BinSight: AI-Powered Dustbin and Waste Analysis System")
# # st.text("Upload a dustbin image and get AI-powered analysis of the waste and associated health recommendations.")
# # uploaded_file = st.file_uploader("Upload an image of the dustbin", type=["jpg", "jpeg", "png"], help="Upload a clear image of a dustbin for analysis.")
# # submit_button = st.button("Analyze Dustbin")
# # if submit_button:
# # if uploaded_file is not None:
# # image = Image.open(uploaded_file)
# # st.image(image, caption="Uploaded Image", use_column_width=True)
# # # Classify the image using MobileNetV2
# # st.subheader("Image Classification")
# # classification_results = classify_image_with_mobilenet(image)
# # for label, score in classification_results.items():
# # st.write(f"- **{label}**: {score:.2f}")
# # # Get user location
# # location = get_user_location()
# # latitude, longitude, address = location
# # st.subheader("User Location")
# # st.write(f"Latitude: {latitude if latitude else 'N/A'}")
# # st.write(f"Longitude: {longitude if longitude else 'N/A'}")
# # st.write(f"Address: {address if address else 'N/A'}")
# # # Get nearest municipal details
# # st.subheader("Nearest Municipal Details")
# # municipal_details = get_nearest_municipal_details(latitude, longitude)
# # st.write(municipal_details)
# # # Generate detailed analysis with Generative AI
# # if classification_results:
# # response = get_genai_response(classification_results, location)
# # display_genai_response(response)
# # else:
# # st.write("Please upload an image for analysis.")
|