Update app.py
Browse files
app.py
CHANGED
|
@@ -19,23 +19,20 @@ CO2_SAVED_PER_EV = DISTANCE_KM * CO2_PER_KM_PETROL
|
|
| 19 |
FEEDBACK_FILE = "feedback.csv"
|
| 20 |
|
| 21 |
if not os.path.exists(FEEDBACK_FILE):
|
| 22 |
-
pd.DataFrame(columns=["Predicted_Label", "Feedback"])
|
|
|
|
| 23 |
|
| 24 |
# ===============================
|
| 25 |
-
#
|
| 26 |
-
# Replace with real color detection later
|
| 27 |
# ===============================
|
| 28 |
-
def
|
| 29 |
-
return random.choice([
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
|
| 36 |
-
"Black": "Rental Vehicle"
|
| 37 |
-
}
|
| 38 |
-
return mapping.get(colour, "Unknown")
|
| 39 |
|
| 40 |
# ===============================
|
| 41 |
# DASHBOARD
|
|
@@ -48,41 +45,25 @@ def generate_dashboard():
|
|
| 48 |
ax.bar(["EV", "Non-EV"], [ev_count, non_ev])
|
| 49 |
ax.set_title("Vehicle Distribution")
|
| 50 |
ax.set_ylabel("Count")
|
| 51 |
-
return fig
|
| 52 |
-
|
| 53 |
-
# ===============================
|
| 54 |
-
# EVALUATION SUMMARY
|
| 55 |
-
# ===============================
|
| 56 |
-
def get_evaluation_summary():
|
| 57 |
-
df = pd.read_csv(FEEDBACK_FILE)
|
| 58 |
-
total = len(df)
|
| 59 |
-
correct = len(df[df["Feedback"] == "Correct"])
|
| 60 |
-
incorrect = len(df[df["Feedback"] == "Incorrect"])
|
| 61 |
-
|
| 62 |
-
if total == 0:
|
| 63 |
-
return "Evaluation Summary:\nNo feedback yet."
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
return f"""Evaluation Summary
|
| 68 |
-
Total: {total}
|
| 69 |
-
Correct: {correct}
|
| 70 |
-
Incorrect: {incorrect}
|
| 71 |
-
Accuracy: {precision:.2f}
|
| 72 |
-
"""
|
| 73 |
|
| 74 |
# ===============================
|
| 75 |
# DETECTION FUNCTION
|
| 76 |
# ===============================
|
| 77 |
-
def detect_image(image):
|
| 78 |
|
| 79 |
global total_vehicles, ev_count, total_co2_saved
|
| 80 |
|
| 81 |
if image is None:
|
| 82 |
-
return None, "Upload image first.",
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
plate_number = "KA01AB1234"
|
| 87 |
|
| 88 |
total_vehicles += 1
|
|
@@ -105,36 +86,44 @@ def detect_image(image):
|
|
| 105 |
|
| 106 |
draw.rectangle(vehicle_box, outline="red", width=4)
|
| 107 |
draw.text((vehicle_box[0], vehicle_box[1]-20),
|
| 108 |
-
f"{vehicle_type}", fill="red")
|
| 109 |
|
| 110 |
draw.rectangle(plate_box, outline="green", width=4)
|
| 111 |
draw.text((plate_box[0], plate_box[1]-20),
|
| 112 |
-
f"{plate_number}
|
| 113 |
|
| 114 |
fig, ax = plt.subplots()
|
| 115 |
ax.imshow(img)
|
| 116 |
ax.axis("off")
|
|
|
|
| 117 |
|
| 118 |
result_text = f"""
|
| 119 |
Vehicle Type: {vehicle_type}
|
| 120 |
-
Plate
|
| 121 |
-
|
| 122 |
-
COβ Saved: {co2_saved_this:.2f} kg
|
| 123 |
"""
|
| 124 |
|
| 125 |
-
total_card = f"### π Total: {total_vehicles}"
|
| 126 |
-
ev_card = f"### β‘ EV: {ev_count}"
|
| 127 |
-
percent_card = f"### π EV Rate: {ev_percent:.2f}%"
|
| 128 |
co2_card = f"### π± COβ Saved: {total_co2_saved:.2f} kg"
|
| 129 |
|
| 130 |
dashboard_fig = generate_dashboard()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ===============================
|
| 137 |
-
# FEEDBACK
|
| 138 |
# ===============================
|
| 139 |
def save_feedback(predicted_label, feedback):
|
| 140 |
df = pd.read_csv(FEEDBACK_FILE)
|
|
@@ -142,85 +131,159 @@ def save_feedback(predicted_label, feedback):
|
|
| 142 |
[{"Predicted_Label": predicted_label, "Feedback": feedback}]
|
| 143 |
)], ignore_index=True)
|
| 144 |
df.to_csv(FEEDBACK_FILE, index=False)
|
| 145 |
-
return "Saved!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
# ===============================
|
| 148 |
-
# RESET DATABASE
|
| 149 |
-
# ===============================
|
| 150 |
def reset_database():
|
| 151 |
-
pd.DataFrame(columns=["Predicted_Label", "Feedback"])
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# ===============================
|
| 155 |
-
# UI
|
| 156 |
# ===============================
|
| 157 |
with gr.Blocks() as demo:
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
img_input = gr.Image(type="pil", label="Upload Vehicle Image")
|
| 163 |
-
img_output = gr.Plot()
|
| 164 |
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
total_card = gr.Markdown("### π Total: 0")
|
| 171 |
-
ev_card = gr.Markdown("### β‘ EV: 0")
|
| 172 |
-
percent_card = gr.Markdown("### π EV Rate: 0%")
|
| 173 |
-
co2_card = gr.Markdown("### π± COβ Saved: 0 kg")
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
-
fn=detect_image,
|
| 181 |
-
inputs=[img_input],
|
| 182 |
-
outputs=[
|
| 183 |
-
img_output,
|
| 184 |
-
result_box,
|
| 185 |
-
total_card,
|
| 186 |
-
ev_card,
|
| 187 |
-
percent_card,
|
| 188 |
-
co2_card,
|
| 189 |
-
dashboard_plot,
|
| 190 |
-
predicted_label_state,
|
| 191 |
-
gr.Markdown() # evaluation summary placeholder
|
| 192 |
-
]
|
| 193 |
-
)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
# FEEDBACK + EVALUATION ROW
|
| 197 |
-
# -------------------------
|
| 198 |
-
with gr.Row():
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
-
evaluation_summary = gr.Textbox(label="Evaluation Summary", lines=6)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
outputs=[feedback_status, evaluation_summary]
|
| 212 |
-
)
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
|
|
|
| 226 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 19 |
FEEDBACK_FILE = "feedback.csv"
|
| 20 |
|
| 21 |
if not os.path.exists(FEEDBACK_FILE):
|
| 22 |
+
df_init = pd.DataFrame(columns=["Predicted_Label", "Feedback"])
|
| 23 |
+
df_init.to_csv(FEEDBACK_FILE, index=False)
|
| 24 |
|
| 25 |
# ===============================
|
| 26 |
+
# DUMMY CLASSIFIER (Replace with YOLO)
|
|
|
|
| 27 |
# ===============================
|
| 28 |
+
def classify_vehicle():
|
| 29 |
+
return random.choice([
|
| 30 |
+
"Car",
|
| 31 |
+
"Bus",
|
| 32 |
+
"Truck",
|
| 33 |
+
"Motorcycle",
|
| 34 |
+
"Electric Vehicle"
|
| 35 |
+
])
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ===============================
|
| 38 |
# DASHBOARD
|
|
|
|
| 45 |
ax.bar(["EV", "Non-EV"], [ev_count, non_ev])
|
| 46 |
ax.set_title("Vehicle Distribution")
|
| 47 |
ax.set_ylabel("Count")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# ===============================
|
| 52 |
# DETECTION FUNCTION
|
| 53 |
# ===============================
|
| 54 |
+
def detect_image(image, threshold):
|
| 55 |
|
| 56 |
global total_vehicles, ev_count, total_co2_saved
|
| 57 |
|
| 58 |
if image is None:
|
| 59 |
+
return None, "Upload image first.", \
|
| 60 |
+
"### π Total Vehicles: 0", \
|
| 61 |
+
"### β‘ EV Vehicles: 0", \
|
| 62 |
+
"### π EV Adoption Rate: 0%", \
|
| 63 |
+
"### π± COβ Saved: 0 kg", \
|
| 64 |
+
generate_dashboard(), ""
|
| 65 |
+
|
| 66 |
+
vehicle_type = classify_vehicle()
|
| 67 |
plate_number = "KA01AB1234"
|
| 68 |
|
| 69 |
total_vehicles += 1
|
|
|
|
| 86 |
|
| 87 |
draw.rectangle(vehicle_box, outline="red", width=4)
|
| 88 |
draw.text((vehicle_box[0], vehicle_box[1]-20),
|
| 89 |
+
f"Vehicle: {vehicle_type}", fill="red")
|
| 90 |
|
| 91 |
draw.rectangle(plate_box, outline="green", width=4)
|
| 92 |
draw.text((plate_box[0], plate_box[1]-20),
|
| 93 |
+
f"Plate: {plate_number}", fill="green")
|
| 94 |
|
| 95 |
fig, ax = plt.subplots()
|
| 96 |
ax.imshow(img)
|
| 97 |
ax.axis("off")
|
| 98 |
+
ax.set_title("Detection Result")
|
| 99 |
|
| 100 |
result_text = f"""
|
| 101 |
Vehicle Type: {vehicle_type}
|
| 102 |
+
Number Plate: {plate_number}
|
| 103 |
+
Confidence Threshold: {threshold}
|
| 104 |
+
COβ Saved (This Detection): {co2_saved_this:.2f} kg
|
| 105 |
"""
|
| 106 |
|
| 107 |
+
total_card = f"### π Total Vehicles: {total_vehicles}"
|
| 108 |
+
ev_card = f"### β‘ EV Vehicles: {ev_count}"
|
| 109 |
+
percent_card = f"### π EV Adoption Rate: {ev_percent:.2f}%"
|
| 110 |
co2_card = f"### π± COβ Saved: {total_co2_saved:.2f} kg"
|
| 111 |
|
| 112 |
dashboard_fig = generate_dashboard()
|
| 113 |
|
| 114 |
+
return (
|
| 115 |
+
fig,
|
| 116 |
+
result_text,
|
| 117 |
+
total_card,
|
| 118 |
+
ev_card,
|
| 119 |
+
percent_card,
|
| 120 |
+
co2_card,
|
| 121 |
+
dashboard_fig,
|
| 122 |
+
vehicle_type
|
| 123 |
+
)
|
| 124 |
|
| 125 |
# ===============================
|
| 126 |
+
# FEEDBACK FUNCTIONS
|
| 127 |
# ===============================
|
| 128 |
def save_feedback(predicted_label, feedback):
|
| 129 |
df = pd.read_csv(FEEDBACK_FILE)
|
|
|
|
| 131 |
[{"Predicted_Label": predicted_label, "Feedback": feedback}]
|
| 132 |
)], ignore_index=True)
|
| 133 |
df.to_csv(FEEDBACK_FILE, index=False)
|
| 134 |
+
return "β
Feedback Saved!"
|
| 135 |
+
|
| 136 |
+
def calculate_metrics():
|
| 137 |
+
|
| 138 |
+
df = pd.read_csv(FEEDBACK_FILE)
|
| 139 |
+
|
| 140 |
+
if len(df) == 0:
|
| 141 |
+
return "No feedback data available.", None
|
| 142 |
+
|
| 143 |
+
tp = len(df[df["Feedback"] == "Correct"])
|
| 144 |
+
fp = len(df[df["Feedback"] == "Incorrect"])
|
| 145 |
+
total = len(df)
|
| 146 |
+
|
| 147 |
+
precision = tp / (tp + fp) if (tp + fp) else 0
|
| 148 |
+
recall = tp / total if total else 0
|
| 149 |
+
|
| 150 |
+
metrics_text = f"""
|
| 151 |
+
Total Samples: {total}
|
| 152 |
+
|
| 153 |
+
True Positives: {tp}
|
| 154 |
+
False Positives: {fp}
|
| 155 |
+
|
| 156 |
+
Precision: {precision:.2f}
|
| 157 |
+
Recall: {recall:.2f}
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
fig, ax = plt.subplots()
|
| 161 |
+
matrix = [[tp, fp], [0, 0]]
|
| 162 |
+
ax.imshow(matrix)
|
| 163 |
+
|
| 164 |
+
ax.set_xticks([0,1])
|
| 165 |
+
ax.set_yticks([0,1])
|
| 166 |
+
ax.set_xticklabels(["Correct", "Incorrect"])
|
| 167 |
+
ax.set_yticklabels(["Predicted"])
|
| 168 |
+
|
| 169 |
+
for i in range(2):
|
| 170 |
+
for j in range(2):
|
| 171 |
+
ax.text(j, i, matrix[i][j], ha="center", va="center")
|
| 172 |
+
|
| 173 |
+
ax.set_title("Confusion Matrix")
|
| 174 |
+
|
| 175 |
+
return metrics_text, fig
|
| 176 |
|
|
|
|
|
|
|
|
|
|
| 177 |
def reset_database():
|
| 178 |
+
df = pd.DataFrame(columns=["Predicted_Label", "Feedback"])
|
| 179 |
+
df.to_csv(FEEDBACK_FILE, index=False)
|
| 180 |
+
return "π Database Reset Successfully!"
|
| 181 |
+
|
| 182 |
+
def download_feedback():
|
| 183 |
+
return FEEDBACK_FILE
|
| 184 |
|
| 185 |
# ===============================
|
| 186 |
+
# GRADIO UI
|
| 187 |
# ===============================
|
| 188 |
with gr.Blocks() as demo:
|
| 189 |
|
| 190 |
+
# ----------------------------
|
| 191 |
+
# DETECTION TAB
|
| 192 |
+
# ----------------------------
|
| 193 |
+
with gr.Tab("π¦ Detection System"):
|
| 194 |
|
| 195 |
+
gr.Markdown("## Smart Vehicle Classification & EV COβ Dashboard")
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
slider = gr.Slider(0.3, 1.0, 0.5, step=0.05,
|
| 198 |
+
label="Confidence Threshold")
|
| 199 |
|
| 200 |
+
with gr.Row():
|
| 201 |
+
img_input = gr.Image(type="pil", label="Upload Vehicle Image")
|
| 202 |
+
img_output = gr.Plot(label="Detection Output")
|
| 203 |
|
| 204 |
+
result_box = gr.Textbox(label="Detection Result", lines=6)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
with gr.Row():
|
| 207 |
+
total_card = gr.Markdown("### π Total Vehicles: 0")
|
| 208 |
+
ev_card = gr.Markdown("### β‘ EV Vehicles: 0")
|
| 209 |
+
percent_card = gr.Markdown("### π EV Adoption Rate: 0%")
|
| 210 |
+
co2_card = gr.Markdown("### π± COβ Saved: 0 kg")
|
| 211 |
|
| 212 |
+
dashboard_plot = gr.Plot(label="Analytics Dashboard")
|
| 213 |
|
| 214 |
+
predicted_label_state = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
detect_btn = gr.Button("π Detect Vehicle")
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
detect_btn.click(
|
| 219 |
+
fn=detect_image,
|
| 220 |
+
inputs=[img_input, slider],
|
| 221 |
+
outputs=[
|
| 222 |
+
img_output,
|
| 223 |
+
result_box,
|
| 224 |
+
total_card,
|
| 225 |
+
ev_card,
|
| 226 |
+
percent_card,
|
| 227 |
+
co2_card,
|
| 228 |
+
dashboard_plot,
|
| 229 |
+
predicted_label_state
|
| 230 |
+
]
|
| 231 |
+
)
|
| 232 |
|
| 233 |
+
gr.Markdown("### Provide Feedback")
|
|
|
|
| 234 |
|
| 235 |
+
correct_btn = gr.Button("β Correct")
|
| 236 |
+
incorrect_btn = gr.Button("β Incorrect")
|
| 237 |
+
feedback_status = gr.Textbox(label="Feedback Status")
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
correct_btn.click(
|
| 240 |
+
fn=save_feedback,
|
| 241 |
+
inputs=[predicted_label_state, gr.State("Correct")],
|
| 242 |
+
outputs=feedback_status
|
| 243 |
+
)
|
| 244 |
|
| 245 |
+
incorrect_btn.click(
|
| 246 |
+
fn=save_feedback,
|
| 247 |
+
inputs=[predicted_label_state, gr.State("Incorrect")],
|
| 248 |
+
outputs=feedback_status
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
# ----------------------------
|
| 252 |
+
# ADMIN DASHBOARD TAB
|
| 253 |
+
# ----------------------------
|
| 254 |
+
with gr.Tab("π Admin Dashboard"):
|
| 255 |
+
|
| 256 |
+
gr.Markdown("## Model Evaluation Dashboard")
|
| 257 |
+
|
| 258 |
+
metrics_box = gr.Textbox(label="Evaluation Metrics", lines=10)
|
| 259 |
+
confusion_plot = gr.Plot()
|
| 260 |
+
|
| 261 |
+
evaluate_btn = gr.Button("Calculate Metrics")
|
| 262 |
+
|
| 263 |
+
evaluate_btn.click(
|
| 264 |
+
fn=calculate_metrics,
|
| 265 |
+
outputs=[metrics_box, confusion_plot]
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
gr.Markdown("### Download Feedback CSV")
|
| 269 |
+
|
| 270 |
+
download_button = gr.Button("Download CSV")
|
| 271 |
+
download_file = gr.File()
|
| 272 |
+
|
| 273 |
+
download_button.click(
|
| 274 |
+
fn=download_feedback,
|
| 275 |
+
outputs=download_file
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
gr.Markdown("### Reset Database")
|
| 279 |
+
|
| 280 |
+
reset_btn = gr.Button("Reset Database")
|
| 281 |
+
reset_output = gr.Textbox()
|
| 282 |
+
|
| 283 |
+
reset_btn.click(
|
| 284 |
+
fn=reset_database,
|
| 285 |
+
outputs=reset_output
|
| 286 |
+
)
|
| 287 |
|
| 288 |
+
# Gradio 6 theme must be passed here
|
| 289 |
demo.launch(theme=gr.themes.Soft())
|