Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS vehicles (
|
|
| 19 |
date TEXT NOT NULL,
|
| 20 |
time TEXT NOT NULL,
|
| 21 |
plate TEXT,
|
| 22 |
-
|
| 23 |
is_ev INTEGER NOT NULL DEFAULT 0,
|
| 24 |
toll REAL DEFAULT 0.0,
|
| 25 |
parking REAL DEFAULT 0.0,
|
|
@@ -73,14 +73,7 @@ def read_plate(plate_img):
|
|
| 73 |
if results:
|
| 74 |
return results[0][1]
|
| 75 |
return "UNKNOWN"
|
| 76 |
-
# -----------added to calculate discount
|
| 77 |
|
| 78 |
-
is_ev = (type == "EV")
|
| 79 |
-
|
| 80 |
-
discount = calculate_discounts(
|
| 81 |
-
type,
|
| 82 |
-
is_ev
|
| 83 |
-
)
|
| 84 |
|
| 85 |
# -------------------- Vehicle Detection --------------------
|
| 86 |
|
|
@@ -132,7 +125,7 @@ def detect_vehicle_type(image):
|
|
| 132 |
|
| 133 |
# -------------------- Discount Engine --------------------
|
| 134 |
|
| 135 |
-
def calculate_discounts(
|
| 136 |
|
| 137 |
toll = 0
|
| 138 |
parking = 0
|
|
@@ -144,13 +137,13 @@ def calculate_discounts(type, is_ev):
|
|
| 144 |
|
| 145 |
parking = 100
|
| 146 |
|
| 147 |
-
if
|
| 148 |
incentive = 10000
|
| 149 |
|
| 150 |
-
elif
|
| 151 |
incentive = 50000
|
| 152 |
|
| 153 |
-
elif
|
| 154 |
incentive = 75000
|
| 155 |
|
| 156 |
else:
|
|
@@ -187,7 +180,7 @@ def get_dashboard():
|
|
| 187 |
return fig
|
| 188 |
|
| 189 |
# Count vehicle types
|
| 190 |
-
counts = df["
|
| 191 |
|
| 192 |
# Create bar chart
|
| 193 |
bars = ax.bar(
|
|
@@ -274,7 +267,7 @@ def process_image(img):
|
|
| 274 |
return (
|
| 275 |
image,
|
| 276 |
plate_text,
|
| 277 |
-
|
| 278 |
f"{discount['toll_discount']}%",
|
| 279 |
f"{discount['parking_discount']}%",
|
| 280 |
f"₹{discount['government_incentive']}",
|
|
@@ -285,11 +278,11 @@ def process_image(img):
|
|
| 285 |
plate_img = image.crop((x1, y1, x2, y2))
|
| 286 |
|
| 287 |
plate_text = read_plate(plate_img)
|
| 288 |
-
|
| 289 |
is_ev = vehicle_type == "EV"
|
| 290 |
|
| 291 |
discount = calculate_discounts(
|
| 292 |
-
|
| 293 |
is_ev
|
| 294 |
)
|
| 295 |
|
|
@@ -301,7 +294,7 @@ def process_image(img):
|
|
| 301 |
date,
|
| 302 |
time,
|
| 303 |
plate,
|
| 304 |
-
|
| 305 |
is_ev,
|
| 306 |
toll,
|
| 307 |
parking,
|
|
@@ -313,7 +306,7 @@ def process_image(img):
|
|
| 313 |
now.strftime("%Y-%m-%d"),
|
| 314 |
now.strftime("%H:%M:%S"),
|
| 315 |
plate_text,
|
| 316 |
-
|
| 317 |
int(is_ev),
|
| 318 |
discount["toll_discount"],
|
| 319 |
discount["parking_discount"],
|
|
@@ -325,11 +318,11 @@ def process_image(img):
|
|
| 325 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 326 |
draw.text(
|
| 327 |
(x1, y1-15),
|
| 328 |
-
f"{
|
| 329 |
fill="red"
|
| 330 |
)
|
| 331 |
|
| 332 |
-
return image, plate_text,
|
| 333 |
|
| 334 |
# -------------------- Gradio UI --------------------
|
| 335 |
css = """
|
|
@@ -355,7 +348,7 @@ with gr.Blocks() as demo:
|
|
| 355 |
with gr.Row():
|
| 356 |
|
| 357 |
plate_box = gr.Textbox(label="Number Plate")
|
| 358 |
-
|
| 359 |
|
| 360 |
with gr.Row():
|
| 361 |
|
|
@@ -374,7 +367,7 @@ with gr.Row():
|
|
| 374 |
outputs=[
|
| 375 |
output_img,
|
| 376 |
plate_box,
|
| 377 |
-
|
| 378 |
toll_box,
|
| 379 |
parking_box,
|
| 380 |
incentive_box,
|
|
|
|
| 19 |
date TEXT NOT NULL,
|
| 20 |
time TEXT NOT NULL,
|
| 21 |
plate TEXT,
|
| 22 |
+
vehicle_type TEXT,
|
| 23 |
is_ev INTEGER NOT NULL DEFAULT 0,
|
| 24 |
toll REAL DEFAULT 0.0,
|
| 25 |
parking REAL DEFAULT 0.0,
|
|
|
|
| 73 |
if results:
|
| 74 |
return results[0][1]
|
| 75 |
return "UNKNOWN"
|
|
|
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# -------------------- Vehicle Detection --------------------
|
| 79 |
|
|
|
|
| 125 |
|
| 126 |
# -------------------- Discount Engine --------------------
|
| 127 |
|
| 128 |
+
def calculate_discounts(vehicle_type, is_ev):
|
| 129 |
|
| 130 |
toll = 0
|
| 131 |
parking = 0
|
|
|
|
| 137 |
|
| 138 |
parking = 100
|
| 139 |
|
| 140 |
+
if vehicle_type == "Car":
|
| 141 |
incentive = 10000
|
| 142 |
|
| 143 |
+
elif vehicle_type == "Bus":
|
| 144 |
incentive = 50000
|
| 145 |
|
| 146 |
+
elif vehicle_type == "Truck":
|
| 147 |
incentive = 75000
|
| 148 |
|
| 149 |
else:
|
|
|
|
| 180 |
return fig
|
| 181 |
|
| 182 |
# Count vehicle types
|
| 183 |
+
counts = df["vehicle_type"].value_counts()
|
| 184 |
|
| 185 |
# Create bar chart
|
| 186 |
bars = ax.bar(
|
|
|
|
| 267 |
return (
|
| 268 |
image,
|
| 269 |
plate_text,
|
| 270 |
+
vehicle_type,
|
| 271 |
f"{discount['toll_discount']}%",
|
| 272 |
f"{discount['parking_discount']}%",
|
| 273 |
f"₹{discount['government_incentive']}",
|
|
|
|
| 278 |
plate_img = image.crop((x1, y1, x2, y2))
|
| 279 |
|
| 280 |
plate_text = read_plate(plate_img)
|
| 281 |
+
vehicle_type = classify_plate_color(plate_img)
|
| 282 |
is_ev = vehicle_type == "EV"
|
| 283 |
|
| 284 |
discount = calculate_discounts(
|
| 285 |
+
vehicle_type,
|
| 286 |
is_ev
|
| 287 |
)
|
| 288 |
|
|
|
|
| 294 |
date,
|
| 295 |
time,
|
| 296 |
plate,
|
| 297 |
+
vehicle_type,
|
| 298 |
is_ev,
|
| 299 |
toll,
|
| 300 |
parking,
|
|
|
|
| 306 |
now.strftime("%Y-%m-%d"),
|
| 307 |
now.strftime("%H:%M:%S"),
|
| 308 |
plate_text,
|
| 309 |
+
vehicle_type,
|
| 310 |
int(is_ev),
|
| 311 |
discount["toll_discount"],
|
| 312 |
discount["parking_discount"],
|
|
|
|
| 318 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 319 |
draw.text(
|
| 320 |
(x1, y1-15),
|
| 321 |
+
f"{vehicle_type} | {plate_category} | {plate_text}",
|
| 322 |
fill="red"
|
| 323 |
)
|
| 324 |
|
| 325 |
+
return image, plate_text, vehicle_type, get_dashboard()
|
| 326 |
|
| 327 |
# -------------------- Gradio UI --------------------
|
| 328 |
css = """
|
|
|
|
| 348 |
with gr.Row():
|
| 349 |
|
| 350 |
plate_box = gr.Textbox(label="Number Plate")
|
| 351 |
+
vehicle_type_box = gr.Textbox(label="Vehicle Type")
|
| 352 |
|
| 353 |
with gr.Row():
|
| 354 |
|
|
|
|
| 367 |
outputs=[
|
| 368 |
output_img,
|
| 369 |
plate_box,
|
| 370 |
+
vehicle_type_box,
|
| 371 |
toll_box,
|
| 372 |
parking_box,
|
| 373 |
incentive_box,
|