Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import matplotlib.pyplot as plt
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
# ===============================
|
| 8 |
# GLOBAL VARIABLES
|
|
@@ -12,51 +13,41 @@ ev_count = 0
|
|
| 12 |
total_co2_saved = 0
|
| 13 |
|
| 14 |
DISTANCE_KM = 10
|
| 15 |
-
CO2_PER_KM_PETROL = 0.150
|
| 16 |
-
CO2_SAVED_PER_EV = DISTANCE_KM * CO2_PER_KM_PETROL
|
| 17 |
|
| 18 |
FEEDBACK_FILE = "feedback.csv"
|
| 19 |
|
| 20 |
-
# Create CSV if not exists
|
| 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 |
# ===============================
|
| 27 |
-
# DUMMY
|
| 28 |
-
# Replace with YOLO model
|
| 29 |
# ===============================
|
| 30 |
def classify_vehicle():
|
| 31 |
-
|
| 32 |
"Car",
|
| 33 |
"Bus",
|
| 34 |
"Truck",
|
| 35 |
"Motorcycle",
|
| 36 |
"Electric Vehicle"
|
| 37 |
-
]
|
| 38 |
-
return random.choice(vehicle_types)
|
| 39 |
-
|
| 40 |
|
| 41 |
# ===============================
|
| 42 |
-
# DASHBOARD
|
| 43 |
# ===============================
|
| 44 |
def generate_dashboard():
|
| 45 |
global total_vehicles, ev_count
|
| 46 |
-
|
| 47 |
non_ev = total_vehicles - ev_count
|
| 48 |
|
| 49 |
fig, ax = plt.subplots()
|
| 50 |
-
|
| 51 |
-
values = [ev_count, non_ev]
|
| 52 |
-
|
| 53 |
-
ax.bar(labels, values)
|
| 54 |
ax.set_title("Vehicle Distribution")
|
| 55 |
ax.set_ylabel("Count")
|
| 56 |
|
| 57 |
return fig
|
| 58 |
|
| 59 |
-
|
| 60 |
# ===============================
|
| 61 |
# DETECTION FUNCTION
|
| 62 |
# ===============================
|
|
@@ -65,7 +56,7 @@ def detect_image(image, threshold):
|
|
| 65 |
global total_vehicles, ev_count, total_co2_saved
|
| 66 |
|
| 67 |
if image is None:
|
| 68 |
-
return None, "Upload
|
| 69 |
"### Total Vehicles: 0", \
|
| 70 |
"### EV Vehicles: 0", \
|
| 71 |
"### EV Adoption Rate: 0%", \
|
|
@@ -73,6 +64,7 @@ def detect_image(image, threshold):
|
|
| 73 |
generate_dashboard(), ""
|
| 74 |
|
| 75 |
vehicle_type = classify_vehicle()
|
|
|
|
| 76 |
|
| 77 |
total_vehicles += 1
|
| 78 |
co2_saved_this = 0
|
|
@@ -84,13 +76,30 @@ def detect_image(image, threshold):
|
|
| 84 |
|
| 85 |
ev_percent = (ev_count / total_vehicles) * 100
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
fig, ax = plt.subplots()
|
| 88 |
-
ax.imshow(
|
| 89 |
-
ax.set_title(f"Detected: {vehicle_type}")
|
| 90 |
ax.axis("off")
|
|
|
|
| 91 |
|
| 92 |
result_text = f"""
|
| 93 |
Vehicle Type: {vehicle_type}
|
|
|
|
| 94 |
Confidence Threshold: {threshold}
|
| 95 |
CO₂ Saved (This Detection): {co2_saved_this:.2f} kg
|
| 96 |
"""
|
|
@@ -113,24 +122,16 @@ CO₂ Saved (This Detection): {co2_saved_this:.2f} kg
|
|
| 113 |
vehicle_type
|
| 114 |
)
|
| 115 |
|
| 116 |
-
|
| 117 |
# ===============================
|
| 118 |
# FEEDBACK FUNCTIONS
|
| 119 |
# ===============================
|
| 120 |
def save_feedback(predicted_label, feedback):
|
| 121 |
-
|
| 122 |
df = pd.read_csv(FEEDBACK_FILE)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
"Feedback": feedback
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 130 |
df.to_csv(FEEDBACK_FILE, index=False)
|
| 131 |
-
|
| 132 |
-
return "✅ Feedback Saved!"
|
| 133 |
-
|
| 134 |
|
| 135 |
def calculate_metrics():
|
| 136 |
|
|
@@ -141,27 +142,25 @@ def calculate_metrics():
|
|
| 141 |
|
| 142 |
tp = len(df[df["Feedback"] == "Correct"])
|
| 143 |
fp = len(df[df["Feedback"] == "Incorrect"])
|
| 144 |
-
|
| 145 |
total = len(df)
|
| 146 |
|
| 147 |
-
precision = tp / (tp + fp) if (tp + fp)
|
| 148 |
-
recall = tp / total if total
|
| 149 |
|
| 150 |
metrics_text = f"""
|
| 151 |
Total Samples: {total}
|
| 152 |
|
| 153 |
-
True Positives
|
| 154 |
-
False Positives
|
| 155 |
|
| 156 |
Precision: {precision:.2f}
|
| 157 |
Recall: {recall:.2f}
|
| 158 |
"""
|
| 159 |
|
| 160 |
fig, ax = plt.subplots()
|
| 161 |
-
matrix = [[tp, fp],
|
| 162 |
-
[0, 0]]
|
| 163 |
-
|
| 164 |
ax.imshow(matrix)
|
|
|
|
| 165 |
ax.set_xticks([0,1])
|
| 166 |
ax.set_yticks([0,1])
|
| 167 |
ax.set_xticklabels(["Correct", "Incorrect"])
|
|
@@ -175,21 +174,18 @@ Recall: {recall:.2f}
|
|
| 175 |
|
| 176 |
return metrics_text, fig
|
| 177 |
|
| 178 |
-
|
| 179 |
def reset_database():
|
| 180 |
df = pd.DataFrame(columns=["Predicted_Label", "Feedback"])
|
| 181 |
df.to_csv(FEEDBACK_FILE, index=False)
|
| 182 |
-
return "
|
| 183 |
-
|
| 184 |
|
| 185 |
def download_feedback():
|
| 186 |
return FEEDBACK_FILE
|
| 187 |
|
| 188 |
-
|
| 189 |
# ===============================
|
| 190 |
# GRADIO UI
|
| 191 |
# ===============================
|
| 192 |
-
with gr.Blocks(
|
| 193 |
|
| 194 |
# ----------------------------
|
| 195 |
# DETECTION TAB
|
|
@@ -198,13 +194,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 198 |
|
| 199 |
gr.Markdown("## Smart Vehicle Classification & EV CO₂ Dashboard")
|
| 200 |
|
| 201 |
-
slider = gr.Slider(0.3, 1.0, 0.5, step=0.05,
|
|
|
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
img_input = gr.Image(type="pil", label="Upload Vehicle Image")
|
| 205 |
img_output = gr.Plot(label="Detection Output")
|
| 206 |
|
| 207 |
-
result_box = gr.Textbox(label="Detection Result", lines=
|
| 208 |
|
| 209 |
with gr.Row():
|
| 210 |
total_card = gr.Markdown("### Total Vehicles: 0")
|
|
@@ -269,14 +266,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 269 |
)
|
| 270 |
|
| 271 |
gr.Markdown("### Download Feedback CSV")
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
gr.Markdown("### Reset Database")
|
|
|
|
| 276 |
reset_btn = gr.Button("Reset Database")
|
| 277 |
reset_output = gr.Textbox()
|
| 278 |
|
| 279 |
-
reset_btn.click(
|
| 280 |
-
|
|
|
|
|
|
|
| 281 |
|
| 282 |
-
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import random
|
| 6 |
+
from PIL import ImageDraw
|
| 7 |
|
| 8 |
# ===============================
|
| 9 |
# GLOBAL VARIABLES
|
|
|
|
| 13 |
total_co2_saved = 0
|
| 14 |
|
| 15 |
DISTANCE_KM = 10
|
| 16 |
+
CO2_PER_KM_PETROL = 0.150
|
| 17 |
+
CO2_SAVED_PER_EV = DISTANCE_KM * CO2_PER_KM_PETROL
|
| 18 |
|
| 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
|
| 39 |
# ===============================
|
| 40 |
def generate_dashboard():
|
| 41 |
global total_vehicles, ev_count
|
|
|
|
| 42 |
non_ev = total_vehicles - ev_count
|
| 43 |
|
| 44 |
fig, ax = plt.subplots()
|
| 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 |
# ===============================
|
|
|
|
| 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%", \
|
|
|
|
| 64 |
generate_dashboard(), ""
|
| 65 |
|
| 66 |
vehicle_type = classify_vehicle()
|
| 67 |
+
plate_number = "KA01AB1234"
|
| 68 |
|
| 69 |
total_vehicles += 1
|
| 70 |
co2_saved_this = 0
|
|
|
|
| 76 |
|
| 77 |
ev_percent = (ev_count / total_vehicles) * 100
|
| 78 |
|
| 79 |
+
# Draw boxes
|
| 80 |
+
img = image.copy()
|
| 81 |
+
draw = ImageDraw.Draw(img)
|
| 82 |
+
w, h = img.size
|
| 83 |
+
|
| 84 |
+
vehicle_box = [w*0.1, h*0.2, w*0.9, h*0.8]
|
| 85 |
+
plate_box = [w*0.4, h*0.6, w*0.7, h*0.75]
|
| 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 |
"""
|
|
|
|
| 122 |
vehicle_type
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
# ===============================
|
| 126 |
# FEEDBACK FUNCTIONS
|
| 127 |
# ===============================
|
| 128 |
def save_feedback(predicted_label, feedback):
|
|
|
|
| 129 |
df = pd.read_csv(FEEDBACK_FILE)
|
| 130 |
+
df = pd.concat([df, pd.DataFrame(
|
| 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 |
|
|
|
|
| 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"])
|
|
|
|
| 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
|
|
|
|
| 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")
|
|
|
|
| 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 passed here
|
| 289 |
+
demo.launch(theme=gr.themes.Soft())
|