Tushar002013 commited on
Commit
b72bfb5
Β·
verified Β·
1 Parent(s): dfa02f8

Update streamlit_app_combined.py

Browse files
Files changed (1) hide show
  1. streamlit_app_combined.py +22 -30
streamlit_app_combined.py CHANGED
@@ -3,10 +3,11 @@ import numpy as np
3
  import cv2
4
  from PIL import Image
5
  import tensorflow as tf
6
- import requests
7
  import os
 
 
8
  st.set_page_config(page_title="Pothole Detection", layout="centered")
9
- # 🏒 Add NHAI logo top-right using HTML
10
  st.markdown(
11
  """
12
  <style>
@@ -23,44 +24,40 @@ st.markdown(
23
  unsafe_allow_html=True
24
  )
25
 
26
-
27
  st.title("πŸ›£οΈ AI-Based Pothole Detection & Water Logging")
 
28
 
29
- # βœ… TFLite model details
30
- MODEL_URL = "https://drive.google.com/file/d/1l4UAi3pK65nt7JrKhHTuopPqUfFL8ZcC/view?usp=share_link"
31
  MODEL_PATH = "pothole_model.tflite"
32
 
33
- # βœ… Download the model only if not already present
34
  if not os.path.exists(MODEL_PATH):
35
- response = requests.get(MODEL_URL)
36
- if response.status_code == 200:
37
- with open(MODEL_PATH, "wb") as f:
38
- f.write(response.content)
39
- print("βœ… TFLite model downloaded successfully.")
40
- else:
41
- raise ValueError(f"❌ Model download failed. Status: {response.status_code}")
42
 
43
- # βœ… Load the TFLite model
 
 
 
 
 
44
  interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
45
  interpreter.allocate_tensors()
46
-
47
- # βœ… Get input/output details
48
  input_details = interpreter.get_input_details()
49
  output_details = interpreter.get_output_details()
50
 
 
51
  uploaded_file = st.file_uploader("πŸ“€ Upload a road image", type=["jpg", "jpeg", "png"])
52
 
53
- # Prediction Function
54
  def predict_pothole(image: Image.Image):
55
  img_resized = image.resize((128, 128))
56
  img_array = np.array(img_resized, dtype=np.float32) / 255.0
57
  img_array = np.expand_dims(img_array, axis=0)
58
  interpreter.set_tensor(input_details[0]['index'], img_array)
59
  interpreter.invoke()
60
- output_data = interpreter.get_tensor(output_details[0]['index'])
61
- return output_data[0][0]
62
 
63
- # Estimate pothole severity
64
  def estimate_severity(img_cv):
65
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
66
  _, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
@@ -73,7 +70,7 @@ def estimate_severity(img_cv):
73
  else:
74
  return "High"
75
 
76
- # Bounding box calculation
77
  def bounding_box(img_cv):
78
  img_resized = cv2.resize(img_cv, (128, 128))
79
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
@@ -87,23 +84,23 @@ def bounding_box(img_cv):
87
  cv2.rectangle(img_resized, (x, y), (x+w, y+h), (255, 0, 0), 2)
88
  return area, (x, y, w, h), img_resized
89
 
90
- # Water logging detection
91
  def detect_water_logging(img_cv):
92
  img_resized = cv2.resize(img_cv, (128, 128))
93
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
94
  _, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
95
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
96
- water_detected = False
97
  total_area = 0
 
98
  for c in contours:
99
  area = cv2.contourArea(c)
100
  if area > 500:
101
  total_area += area
102
  water_detected = True
103
  cv2.drawContours(img_resized, [c], -1, (255, 0, 0), 2)
104
- return water_detected, int(total_area), img_resized
105
 
106
- # Streamlit Main Logic
107
  if uploaded_file:
108
  image = Image.open(uploaded_file).convert("RGB")
109
  st.image(image, caption="Uploaded Image", use_column_width=True)
@@ -140,8 +137,3 @@ if uploaded_file:
140
 
141
 
142
 
143
-
144
-
145
-
146
-
147
-
 
3
  import cv2
4
  from PIL import Image
5
  import tensorflow as tf
6
+ import gdown
7
  import os
8
+
9
+ # --- UI SETUP ---
10
  st.set_page_config(page_title="Pothole Detection", layout="centered")
 
11
  st.markdown(
12
  """
13
  <style>
 
24
  unsafe_allow_html=True
25
  )
26
 
 
27
  st.title("πŸ›£οΈ AI-Based Pothole Detection & Water Logging")
28
+ st.markdown("Upload a road image to detect potholes, estimate severity, and detect water logging.")
29
 
30
+ # --- DOWNLOAD MODEL FROM GOOGLE DRIVE IF NOT EXISTS ---
31
+ MODEL_ID = "1l4UAi3pK65nt7JrKhHTuopPqUfFL8ZcC"
32
  MODEL_PATH = "pothole_model.tflite"
33
 
 
34
  if not os.path.exists(MODEL_PATH):
35
+ gdown.download(id=MODEL_ID, output=MODEL_PATH, quiet=False)
 
 
 
 
 
 
36
 
37
+ # --- VALIDATE FILE HEADER (MUST START WITH TFL3) ---
38
+ with open(MODEL_PATH, "rb") as f:
39
+ if f.read(4) != b'TFL3':
40
+ raise ValueError("❌ Invalid TFLite model format: Expected header 'TFL3'")
41
+
42
+ # --- LOAD TFLITE MODEL ---
43
  interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
44
  interpreter.allocate_tensors()
 
 
45
  input_details = interpreter.get_input_details()
46
  output_details = interpreter.get_output_details()
47
 
48
+ # --- IMAGE UPLOAD ---
49
  uploaded_file = st.file_uploader("πŸ“€ Upload a road image", type=["jpg", "jpeg", "png"])
50
 
51
+ # --- PREDICTION FUNCTION ---
52
  def predict_pothole(image: Image.Image):
53
  img_resized = image.resize((128, 128))
54
  img_array = np.array(img_resized, dtype=np.float32) / 255.0
55
  img_array = np.expand_dims(img_array, axis=0)
56
  interpreter.set_tensor(input_details[0]['index'], img_array)
57
  interpreter.invoke()
58
+ return interpreter.get_tensor(output_details[0]['index'])[0][0]
 
59
 
60
+ # --- SEVERITY ESTIMATION ---
61
  def estimate_severity(img_cv):
62
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
63
  _, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
 
70
  else:
71
  return "High"
72
 
73
+ # --- BOUNDING BOX DRAWING ---
74
  def bounding_box(img_cv):
75
  img_resized = cv2.resize(img_cv, (128, 128))
76
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
 
84
  cv2.rectangle(img_resized, (x, y), (x+w, y+h), (255, 0, 0), 2)
85
  return area, (x, y, w, h), img_resized
86
 
87
+ # --- WATER LOGGING DETECTION ---
88
  def detect_water_logging(img_cv):
89
  img_resized = cv2.resize(img_cv, (128, 128))
90
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
91
  _, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
92
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
93
  total_area = 0
94
+ water_detected = False
95
  for c in contours:
96
  area = cv2.contourArea(c)
97
  if area > 500:
98
  total_area += area
99
  water_detected = True
100
  cv2.drawContours(img_resized, [c], -1, (255, 0, 0), 2)
101
+ return water_detected, total_area, img_resized
102
 
103
+ # --- MAIN LOGIC ---
104
  if uploaded_file:
105
  image = Image.open(uploaded_file).convert("RGB")
106
  st.image(image, caption="Uploaded Image", use_column_width=True)
 
137
 
138
 
139