Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,21 @@ import matplotlib
|
|
| 5 |
matplotlib.use("Agg")
|
| 6 |
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
-
import requests
|
| 9 |
import torch
|
| 10 |
import numpy as np
|
| 11 |
import sqlite3
|
| 12 |
import pandas as pd
|
| 13 |
import pytesseract
|
| 14 |
|
| 15 |
-
from urllib.parse import urlparse
|
| 16 |
from PIL import Image
|
| 17 |
from transformers import YolosImageProcessor, YolosForObjectDetection
|
| 18 |
|
| 19 |
-
# ----------------
|
| 20 |
|
| 21 |
MODEL_NAME = "nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 22 |
BASE_AMT = 100
|
| 23 |
|
| 24 |
-
# ----------------
|
| 25 |
|
| 26 |
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 27 |
cursor = conn.cursor()
|
|
@@ -44,33 +42,20 @@ CREATE TABLE IF NOT EXISTS feedback (
|
|
| 44 |
|
| 45 |
conn.commit()
|
| 46 |
|
| 47 |
-
# ----------------
|
| 48 |
|
| 49 |
processor = None
|
| 50 |
model = None
|
| 51 |
|
| 52 |
def load_model():
|
| 53 |
global processor, model
|
| 54 |
-
if processor is None
|
| 55 |
processor = YolosImageProcessor.from_pretrained(MODEL_NAME)
|
| 56 |
model = YolosForObjectDetection.from_pretrained(MODEL_NAME)
|
| 57 |
model.eval()
|
| 58 |
return processor, model
|
| 59 |
|
| 60 |
-
# ----------------
|
| 61 |
-
|
| 62 |
-
def is_valid_url(url):
|
| 63 |
-
try:
|
| 64 |
-
r = urlparse(url)
|
| 65 |
-
return all([r.scheme, r.netloc])
|
| 66 |
-
except:
|
| 67 |
-
return False
|
| 68 |
-
|
| 69 |
-
def get_original_image(url):
|
| 70 |
-
response = requests.get(url, stream=True)
|
| 71 |
-
return Image.open(response.raw).convert("RGB")
|
| 72 |
-
|
| 73 |
-
# -------------------- LOGIC --------------------
|
| 74 |
|
| 75 |
def compute_discount(vehicle_type):
|
| 76 |
if vehicle_type == "EV":
|
|
@@ -78,27 +63,33 @@ def compute_discount(vehicle_type):
|
|
| 78 |
return BASE_AMT
|
| 79 |
|
| 80 |
def classify_plate_color(plate_img):
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
def read_plate(plate_img):
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def make_prediction(img):
|
| 104 |
processor, model = load_model()
|
|
@@ -114,110 +105,126 @@ def make_prediction(img):
|
|
| 114 |
|
| 115 |
return results[0], model.config.id2label
|
| 116 |
|
| 117 |
-
# ----------------
|
| 118 |
|
| 119 |
def visualize(img, output, id2label, threshold):
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
if "plate" not in label_name:
|
| 131 |
-
continue
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
toll = compute_discount(vtype)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
-
conn.commit()
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
return fig, "No plate detected"
|
| 159 |
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
-
# ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def submit_feedback(result_text, feedback_choice):
|
| 165 |
if not result_text:
|
| 166 |
-
return "No result
|
| 167 |
|
| 168 |
cursor.execute(
|
| 169 |
"INSERT INTO feedback VALUES (?, ?)",
|
| 170 |
(result_text, feedback_choice)
|
| 171 |
)
|
| 172 |
conn.commit()
|
| 173 |
-
|
| 174 |
return "Feedback recorded!"
|
| 175 |
|
| 176 |
def show_accuracy():
|
| 177 |
df = pd.read_sql("SELECT * FROM feedback", conn)
|
| 178 |
|
| 179 |
if df.empty:
|
| 180 |
-
return "No feedback
|
| 181 |
|
| 182 |
correct = len(df[df["feedback"] == "Correct"])
|
| 183 |
total = len(df)
|
| 184 |
|
| 185 |
accuracy = (correct / total) * 100
|
| 186 |
-
return f"
|
| 187 |
|
| 188 |
-
# ----------------
|
| 189 |
|
| 190 |
-
def
|
| 191 |
if img is None:
|
| 192 |
return None, "No image provided"
|
| 193 |
output, id2label = make_prediction(img)
|
| 194 |
return visualize(img, output, id2label, threshold)
|
| 195 |
|
| 196 |
-
# ----------------
|
| 197 |
|
| 198 |
with gr.Blocks() as demo:
|
| 199 |
gr.Markdown("## 🚦 Smart Vehicle Classification System")
|
| 200 |
|
| 201 |
slider = gr.Slider(0.3, 1.0, 0.5, label="Confidence Threshold")
|
| 202 |
-
result_box = gr.Textbox(label="Detection Result", lines=4)
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
detect_btn = gr.Button("Detect")
|
| 208 |
detect_btn.click(
|
| 209 |
-
|
| 210 |
inputs=[img_input, slider],
|
| 211 |
outputs=[img_output, result_box]
|
| 212 |
)
|
| 213 |
|
| 214 |
-
gr.Markdown("###
|
| 215 |
-
|
| 216 |
-
feedback_radio = gr.Radio(
|
| 217 |
-
["Correct", "Incorrect"],
|
| 218 |
-
label="Was the prediction correct?"
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
feedback_btn = gr.Button("Submit Feedback")
|
| 222 |
feedback_msg = gr.Textbox(label="Feedback Status")
|
| 223 |
|
|
@@ -227,14 +234,16 @@ with gr.Blocks() as demo:
|
|
| 227 |
outputs=feedback_msg
|
| 228 |
)
|
| 229 |
|
| 230 |
-
gr.Markdown("### Model
|
| 231 |
-
|
| 232 |
accuracy_btn = gr.Button("Show Accuracy")
|
| 233 |
accuracy_box = gr.Textbox(label="Accuracy")
|
| 234 |
|
| 235 |
-
accuracy_btn.click(
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
)
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
demo.launch()
|
|
|
|
| 5 |
matplotlib.use("Agg")
|
| 6 |
|
| 7 |
import matplotlib.pyplot as plt
|
|
|
|
| 8 |
import torch
|
| 9 |
import numpy as np
|
| 10 |
import sqlite3
|
| 11 |
import pandas as pd
|
| 12 |
import pytesseract
|
| 13 |
|
|
|
|
| 14 |
from PIL import Image
|
| 15 |
from transformers import YolosImageProcessor, YolosForObjectDetection
|
| 16 |
|
| 17 |
+
# ---------------- CONFIG ----------------
|
| 18 |
|
| 19 |
MODEL_NAME = "nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 20 |
BASE_AMT = 100
|
| 21 |
|
| 22 |
+
# ---------------- DATABASE ----------------
|
| 23 |
|
| 24 |
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 25 |
cursor = conn.cursor()
|
|
|
|
| 42 |
|
| 43 |
conn.commit()
|
| 44 |
|
| 45 |
+
# ---------------- MODEL (Lazy Load) ----------------
|
| 46 |
|
| 47 |
processor = None
|
| 48 |
model = None
|
| 49 |
|
| 50 |
def load_model():
|
| 51 |
global processor, model
|
| 52 |
+
if processor is None:
|
| 53 |
processor = YolosImageProcessor.from_pretrained(MODEL_NAME)
|
| 54 |
model = YolosForObjectDetection.from_pretrained(MODEL_NAME)
|
| 55 |
model.eval()
|
| 56 |
return processor, model
|
| 57 |
|
| 58 |
+
# ---------------- LOGIC ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def compute_discount(vehicle_type):
|
| 61 |
if vehicle_type == "EV":
|
|
|
|
| 63 |
return BASE_AMT
|
| 64 |
|
| 65 |
def classify_plate_color(plate_img):
|
| 66 |
+
try:
|
| 67 |
+
img = np.array(plate_img)
|
| 68 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
| 69 |
|
| 70 |
+
green = np.sum(cv2.inRange(hsv, (35,40,40), (85,255,255)))
|
| 71 |
+
yellow = np.sum(cv2.inRange(hsv, (15,50,50), (35,255,255)))
|
| 72 |
|
| 73 |
+
if green > yellow:
|
| 74 |
+
return "EV"
|
| 75 |
+
elif yellow > green:
|
| 76 |
+
return "Commercial"
|
| 77 |
+
return "Personal"
|
| 78 |
+
except:
|
| 79 |
+
return "Unknown"
|
| 80 |
|
| 81 |
def read_plate(plate_img):
|
| 82 |
+
try:
|
| 83 |
+
gray = cv2.cvtColor(np.array(plate_img), cv2.COLOR_RGB2GRAY)
|
| 84 |
+
gray = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)[1]
|
| 85 |
|
| 86 |
+
text = pytesseract.image_to_string(
|
| 87 |
+
gray,
|
| 88 |
+
config="--psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
| 89 |
+
)
|
| 90 |
+
return text.strip() if text.strip() else "UNKNOWN"
|
| 91 |
+
except:
|
| 92 |
+
return "UNKNOWN"
|
| 93 |
|
| 94 |
def make_prediction(img):
|
| 95 |
processor, model = load_model()
|
|
|
|
| 105 |
|
| 106 |
return results[0], model.config.id2label
|
| 107 |
|
| 108 |
+
# ---------------- VISUALIZATION ----------------
|
| 109 |
|
| 110 |
def visualize(img, output, id2label, threshold):
|
| 111 |
+
try:
|
| 112 |
+
keep = output["scores"] > threshold
|
| 113 |
+
boxes = output["boxes"][keep]
|
| 114 |
+
labels = output["labels"][keep]
|
| 115 |
|
| 116 |
+
fig, ax = plt.subplots(figsize=(6,6))
|
| 117 |
+
ax.imshow(img)
|
| 118 |
+
results_text = []
|
| 119 |
|
| 120 |
+
for box, label in zip(boxes, labels):
|
| 121 |
+
label_name = id2label[label.item()].lower()
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
if "plate" not in label_name:
|
| 124 |
+
continue
|
| 125 |
|
| 126 |
+
x1,y1,x2,y2 = map(int, box.tolist())
|
| 127 |
+
plate_img = img.crop((x1,y1,x2,y2))
|
|
|
|
| 128 |
|
| 129 |
+
plate = read_plate(plate_img)
|
| 130 |
+
vtype = classify_plate_color(plate_img)
|
| 131 |
+
toll = compute_discount(vtype)
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
cursor.execute(
|
| 134 |
+
"INSERT INTO vehicles VALUES (?, ?, ?, datetime('now'))",
|
| 135 |
+
(plate, vtype, toll)
|
| 136 |
+
)
|
| 137 |
+
conn.commit()
|
| 138 |
|
| 139 |
+
results_text.append(f"{plate} | {vtype} | ₹{int(toll)}")
|
| 140 |
+
|
| 141 |
+
ax.add_patch(
|
| 142 |
+
plt.Rectangle((x1,y1), x2-x1, y2-y1,
|
| 143 |
+
fill=False, color="red", linewidth=2)
|
| 144 |
+
)
|
| 145 |
+
ax.text(x1, y1-5, f"{plate} ({vtype})",
|
| 146 |
+
color="yellow", fontsize=8)
|
| 147 |
+
|
| 148 |
+
ax.axis("off")
|
| 149 |
|
| 150 |
+
if not results_text:
|
| 151 |
+
return fig, "No plate detected"
|
| 152 |
|
| 153 |
+
return fig, "\n".join(results_text)
|
|
|
|
| 154 |
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return None, f"Error: {str(e)}"
|
| 157 |
|
| 158 |
+
# ---------------- DASHBOARD ----------------
|
| 159 |
+
|
| 160 |
+
def get_dashboard():
|
| 161 |
+
df = pd.read_sql("SELECT * FROM vehicles", conn)
|
| 162 |
+
fig, ax = plt.subplots()
|
| 163 |
+
|
| 164 |
+
if df.empty:
|
| 165 |
+
ax.text(0.5,0.5,"No data yet",ha="center")
|
| 166 |
+
ax.axis("off")
|
| 167 |
+
return fig
|
| 168 |
+
|
| 169 |
+
df["type"].value_counts().plot(kind="bar", ax=ax)
|
| 170 |
+
ax.set_title("Vehicle Types")
|
| 171 |
+
return fig
|
| 172 |
+
|
| 173 |
+
# ---------------- FEEDBACK ----------------
|
| 174 |
|
| 175 |
def submit_feedback(result_text, feedback_choice):
|
| 176 |
if not result_text:
|
| 177 |
+
return "No result available."
|
| 178 |
|
| 179 |
cursor.execute(
|
| 180 |
"INSERT INTO feedback VALUES (?, ?)",
|
| 181 |
(result_text, feedback_choice)
|
| 182 |
)
|
| 183 |
conn.commit()
|
|
|
|
| 184 |
return "Feedback recorded!"
|
| 185 |
|
| 186 |
def show_accuracy():
|
| 187 |
df = pd.read_sql("SELECT * FROM feedback", conn)
|
| 188 |
|
| 189 |
if df.empty:
|
| 190 |
+
return "No feedback yet."
|
| 191 |
|
| 192 |
correct = len(df[df["feedback"] == "Correct"])
|
| 193 |
total = len(df)
|
| 194 |
|
| 195 |
accuracy = (correct / total) * 100
|
| 196 |
+
return f"Accuracy (User Feedback Based): {accuracy:.2f}%"
|
| 197 |
|
| 198 |
+
# ---------------- CALLBACK ----------------
|
| 199 |
|
| 200 |
+
def detect_image(img, threshold):
|
| 201 |
if img is None:
|
| 202 |
return None, "No image provided"
|
| 203 |
output, id2label = make_prediction(img)
|
| 204 |
return visualize(img, output, id2label, threshold)
|
| 205 |
|
| 206 |
+
# ---------------- UI ----------------
|
| 207 |
|
| 208 |
with gr.Blocks() as demo:
|
| 209 |
gr.Markdown("## 🚦 Smart Vehicle Classification System")
|
| 210 |
|
| 211 |
slider = gr.Slider(0.3, 1.0, 0.5, label="Confidence Threshold")
|
|
|
|
| 212 |
|
| 213 |
+
with gr.Row():
|
| 214 |
+
img_input = gr.Image(type="pil")
|
| 215 |
+
img_output = gr.Plot()
|
| 216 |
+
|
| 217 |
+
result_box = gr.Textbox(label="Detection Result", lines=4)
|
| 218 |
|
| 219 |
detect_btn = gr.Button("Detect")
|
| 220 |
detect_btn.click(
|
| 221 |
+
detect_image,
|
| 222 |
inputs=[img_input, slider],
|
| 223 |
outputs=[img_output, result_box]
|
| 224 |
)
|
| 225 |
|
| 226 |
+
gr.Markdown("### Feedback")
|
| 227 |
+
feedback_radio = gr.Radio(["Correct", "Incorrect"], label="Prediction correct?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
feedback_btn = gr.Button("Submit Feedback")
|
| 229 |
feedback_msg = gr.Textbox(label="Feedback Status")
|
| 230 |
|
|
|
|
| 234 |
outputs=feedback_msg
|
| 235 |
)
|
| 236 |
|
| 237 |
+
gr.Markdown("### Model Accuracy")
|
|
|
|
| 238 |
accuracy_btn = gr.Button("Show Accuracy")
|
| 239 |
accuracy_box = gr.Textbox(label="Accuracy")
|
| 240 |
|
| 241 |
+
accuracy_btn.click(show_accuracy, outputs=accuracy_box)
|
| 242 |
+
|
| 243 |
+
gr.Markdown("### 📊 Dashboard")
|
| 244 |
+
dashboard_plot = gr.Plot()
|
| 245 |
+
|
| 246 |
+
refresh_btn = gr.Button("Refresh Dashboard")
|
| 247 |
+
refresh_btn.click(get_dashboard, outputs=dashboard_plot)
|
| 248 |
|
| 249 |
demo.launch()
|