Sarvamangalak commited on
Commit
64d9963
Β·
verified Β·
1 Parent(s): edc41c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +184 -140
app.py CHANGED
@@ -1,182 +1,226 @@
1
  import gradio as gr
2
- import cv2
3
- import numpy as np
4
- import os
5
  import pandas as pd
6
- from datetime import datetime
7
-
8
- # Store evaluation data
9
- feedback_data = []
10
-
11
- ##############################################
12
- # Utility: Detect plate color and vehicle type
13
- ##############################################
14
-
15
- def classify_vehicle_by_plate_color(roi):
16
-
17
- hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
18
-
19
- # Masks for colors
20
- masks = {}
21
-
22
- # White
23
- masks["white"] = cv2.inRange(hsv, np.array([0, 0, 180]), np.array([180, 60, 255]))
24
-
25
- # Yellow
26
- masks["yellow"] = cv2.inRange(hsv, np.array([15, 80, 80]), np.array([40, 255, 255]))
27
-
28
- # Green
29
- masks["green"] = cv2.inRange(hsv, np.array([35, 50, 50]), np.array([85, 255, 255]))
30
-
31
- # Red
32
- masks["red1"] = cv2.inRange(hsv, np.array([0, 70, 50]), np.array([10, 255, 255]))
33
- masks["red2"] = cv2.inRange(hsv, np.array([170, 70, 50]), np.array([180, 255, 255]))
34
- masks["red"] = masks["red1"] + masks["red2"]
35
-
36
- # Blue
37
- masks["blue"] = cv2.inRange(hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Count pixels
40
- color_counts = {color: np.sum(mask) for color, mask in masks.items()}
41
 
42
- dominant_color = max(color_counts, key=color_counts.get)
43
 
44
- # Classification logic
45
- if dominant_color == "white":
46
- return "Private Vehicle"
47
- elif dominant_color == "yellow":
48
- return "Commercial Vehicle"
49
- elif dominant_color == "green":
50
- return "Electric Vehicle (EV)"
51
- elif dominant_color == "red":
52
- return "Temporary Registration Vehicle"
53
- elif dominant_color == "blue":
54
- return "Diplomatic Vehicle"
55
- else:
56
- return "Unknown Vehicle Type"
57
 
58
- ##############################################
59
- # Main Detection Function
60
- ##############################################
 
61
 
62
- def detect_vehicles(image):
63
 
64
  if image is None:
65
- return None, "No image uploaded."
66
-
67
- img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
68
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
69
-
70
- # Simple plate-like contour detection
71
- edges = cv2.Canny(gray, 100, 200)
72
- contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
73
-
74
- detected_info = []
75
- count = 0
76
-
77
- for cnt in contours:
78
- x, y, w, h = cv2.boundingRect(cnt)
79
-
80
- # Plate aspect ratio filter
81
- if 2 < w / h < 6 and w > 100:
82
- plate_roi = img[y:y+h, x:x+w]
83
- vehicle_type = classify_vehicle_by_plate_color(plate_roi)
84
 
85
- cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
86
- cv2.putText(img, vehicle_type,
87
- (x, y-10),
88
- cv2.FONT_HERSHEY_SIMPLEX,
89
- 0.6,
90
- (0, 255, 0),
91
- 2)
92
 
93
- detected_info.append(vehicle_type)
94
- count += 1
95
 
96
- if count == 0:
97
- summary = "No vehicles detected."
98
- else:
99
- summary = f"{count} Vehicle(s) Detected: " + ", ".join(detected_info)
100
 
101
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
102
 
103
- return img, summary
 
 
 
104
 
 
 
105
 
106
- ##############################################
107
- # Feedback System
108
- ##############################################
109
 
110
- def submit_feedback(is_correct):
111
- global feedback_data
 
112
 
113
- feedback_data.append({
114
- "timestamp": datetime.now(),
115
- "correct": is_correct
116
- })
117
 
118
- return generate_summary()
119
-
120
-
121
- def generate_summary():
122
- total = len(feedback_data)
123
- if total == 0:
124
- return "No evaluations yet."
125
-
126
- correct = sum(1 for f in feedback_data if f["correct"])
127
- accuracy = (correct / total) * 100
128
-
129
- return f"""
130
- πŸ“Š Evaluation Summary
131
- Total Samples: {total}
132
- Correct: {correct}
133
- Accuracy: {accuracy:.2f}%
134
  """
135
 
136
-
137
- ##############################################
138
- # Gradio UI
139
- ##############################################
140
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  with gr.Blocks() as demo:
142
 
143
- gr.Markdown("## Smart Traffic Vehicle Classification System")
144
 
145
  with gr.Row():
 
 
146
 
147
- with gr.Column(scale=2):
148
 
149
- image_input = gr.Image(type="pil", label="Upload Vehicle Image")
150
 
151
- detect_btn = gr.Button("Detect", size="sm")
 
 
 
 
152
 
153
- output_image = gr.Image(label="Output Image")
154
- output_text = gr.Textbox(label="Detection Summary")
155
 
156
- with gr.Column(scale=1):
157
 
158
- gr.Markdown("### Feedback")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- correct_btn = gr.Button("Correct", size="sm")
161
- incorrect_btn = gr.Button("Incorrect", size="sm")
 
 
162
 
163
- summary_box = gr.Textbox(label="Evaluation Summary")
 
 
 
164
 
165
- # Button actions
166
- detect_btn.click(
167
- fn=detect_vehicles,
168
- inputs=image_input,
169
- outputs=[output_image, output_text]
170
- )
171
 
172
  correct_btn.click(
173
- fn=lambda: submit_feedback(True),
174
- outputs=summary_box
 
175
  )
176
 
177
  incorrect_btn.click(
178
- fn=lambda: submit_feedback(False),
179
- outputs=summary_box
 
 
 
 
 
 
 
180
  )
181
 
182
  demo.launch(theme=gr.themes.Soft())
 
1
  import gradio as gr
2
+ import matplotlib.pyplot as plt
 
 
3
  import pandas as pd
4
+ import os
5
+ import random
6
+ from PIL import ImageDraw
7
+
8
+ # ===============================
9
+ # GLOBAL VARIABLES
10
+ # ===============================
11
+ total_vehicles = 0
12
+ ev_count = 0
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
+ pd.DataFrame(columns=["Predicted_Label", "Feedback"]).to_csv(FEEDBACK_FILE, index=False)
23
+
24
+ # ===============================
25
+ # SIMULATED NUMBER PLATE COLOUR DETECTION
26
+ # Replace with real color detection later
27
+ # ===============================
28
+ def detect_plate_colour():
29
+ return random.choice(["White", "Yellow", "Green", "Black"])
30
+
31
+ def get_vehicle_type_from_colour(colour):
32
+ mapping = {
33
+ "White": "Private Vehicle",
34
+ "Yellow": "Commercial Vehicle",
35
+ "Green": "Electric Vehicle",
36
+ "Black": "Rental Vehicle"
37
+ }
38
+ return mapping.get(colour, "Unknown")
39
+
40
+ # ===============================
41
+ # DASHBOARD
42
+ # ===============================
43
+ def generate_dashboard():
44
+ global total_vehicles, ev_count
45
+ non_ev = total_vehicles - ev_count
46
+
47
+ fig, ax = plt.subplots()
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
+ precision = correct / total
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
+ plate_colour = detect_plate_colour()
85
+ vehicle_type = get_vehicle_type_from_colour(plate_colour)
86
+ plate_number = "KA01AB1234"
 
 
 
 
87
 
88
+ total_vehicles += 1
89
+ co2_saved_this = 0
90
 
91
+ if vehicle_type == "Electric Vehicle":
92
+ ev_count += 1
93
+ co2_saved_this = CO2_SAVED_PER_EV
94
+ total_co2_saved += co2_saved_this
95
 
96
+ ev_percent = (ev_count / total_vehicles) * 100
97
 
98
+ # Draw boxes
99
+ img = image.copy()
100
+ draw = ImageDraw.Draw(img)
101
+ w, h = img.size
102
 
103
+ vehicle_box = [w*0.1, h*0.2, w*0.9, h*0.8]
104
+ plate_box = [w*0.4, h*0.6, w*0.7, h*0.75]
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} ({plate_colour})", fill="green")
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 Colour: {plate_colour}
121
+ Plate Number: {plate_number}
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
+ summary = get_evaluation_summary()
133
+
134
+ return fig, result_text, total_card, ev_card, percent_card, co2_card, dashboard_fig, vehicle_type, summary
135
+
136
+ # ===============================
137
+ # FEEDBACK SAVE
138
+ # ===============================
139
+ def save_feedback(predicted_label, feedback):
140
+ df = pd.read_csv(FEEDBACK_FILE)
141
+ df = pd.concat([df, pd.DataFrame(
142
+ [{"Predicted_Label": predicted_label, "Feedback": feedback}]
143
+ )], ignore_index=True)
144
+ df.to_csv(FEEDBACK_FILE, index=False)
145
+ return "Saved!", get_evaluation_summary()
146
+
147
+ # ===============================
148
+ # RESET DATABASE
149
+ # ===============================
150
+ def reset_database():
151
+ pd.DataFrame(columns=["Predicted_Label", "Feedback"]).to_csv(FEEDBACK_FILE, index=False)
152
+ return "Database Reset!", "Evaluation Summary:\nNo feedback yet."
153
+
154
+ # ===============================
155
+ # UI
156
+ # ===============================
157
  with gr.Blocks() as demo:
158
 
159
+ gr.Markdown("## 🚦 Smart Vehicle Detection Based on Number Plate Colour")
160
 
161
  with gr.Row():
162
+ img_input = gr.Image(type="pil", label="Upload Vehicle Image")
163
+ img_output = gr.Plot()
164
 
165
+ detect_btn = gr.Button("Detect", size="sm")
166
 
167
+ result_box = gr.Textbox(label="Detection Result", lines=5)
168
 
169
+ with gr.Row():
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
+ dashboard_plot = gr.Plot()
 
176
 
177
+ predicted_label_state = gr.State()
178
 
179
+ detect_btn.click(
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
+ with gr.Column(scale=1):
201
+ correct_btn = gr.Button("βœ”", size="sm")
202
+ incorrect_btn = gr.Button("✘", size="sm")
203
+ feedback_status = gr.Textbox(label="Feedback", lines=1)
204
 
205
+ with gr.Column(scale=1):
206
+ evaluation_summary = gr.Textbox(label="Evaluation Summary", lines=6)
 
 
 
 
207
 
208
  correct_btn.click(
209
+ fn=save_feedback,
210
+ inputs=[predicted_label_state, gr.State("Correct")],
211
+ outputs=[feedback_status, evaluation_summary]
212
  )
213
 
214
  incorrect_btn.click(
215
+ fn=save_feedback,
216
+ inputs=[predicted_label_state, gr.State("Incorrect")],
217
+ outputs=[feedback_status, evaluation_summary]
218
+ )
219
+
220
+ reset_btn = gr.Button("Reset Database", size="sm")
221
+ reset_btn.click(
222
+ fn=reset_database,
223
+ outputs=[feedback_status, evaluation_summary]
224
  )
225
 
226
  demo.launch(theme=gr.themes.Soft())