Sarvamangalak commited on
Commit
36c0170
·
verified ·
1 Parent(s): b48508f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -46
app.py CHANGED
@@ -16,8 +16,13 @@ from transformers import YolosImageProcessor, YolosForObjectDetection
16
 
17
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
18
 
19
- BASE_TOLL = 100
20
-
 
 
 
 
 
21
  COLORS = [
22
  [0.000, 0.447, 0.741],
23
  [0.850, 0.325, 0.098],
@@ -176,23 +181,19 @@ def visualize_prediction(img, output_dict, threshold=0.5, id2label=None):
176
  ax = plt.gca()
177
  colors = COLORS * 100
178
 
 
 
179
  for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
180
  if "plate" in label.lower():
181
  crop = img.crop((int(xmin), int(ymin), int(xmax), int(ymax)))
182
- plate_type = classify_plate_color(crop)
183
-
184
- if plate_type == "EV":
185
- amount = BASE_TOLL * 0.9
186
- price_text = f"EV |Congratulations !! 10% off on the amount "
187
- else:
188
- amount = BASE_TOLL
189
- price_text = f"{plate_type} | ₹{amount:.0f}"
190
-
191
- cursor.execute(
192
- "INSERT INTO vehicles VALUES (?, ?, ?, datetime('now'))",
193
- ("UNKNOWN", plate_type, amount)
194
  )
195
- conn.commit()
196
 
197
  ax.add_patch(
198
  plt.Rectangle(
@@ -200,20 +201,29 @@ def visualize_prediction(img, output_dict, threshold=0.5, id2label=None):
200
  fill=False, color=color, linewidth=4
201
  )
202
  )
 
203
  ax.text(
204
  xmin, ymin - 10,
205
- f"{price_text} | {score:0.2f}",
206
  fontsize=12,
207
  bbox=dict(facecolor="yellow", alpha=0.8)
208
  )
209
 
210
  plt.axis("off")
211
- return fig2img(plt.gcf())
212
 
 
 
 
 
 
 
213
 
214
  # ---------------- Image Detection ----------------
215
 
216
- def detect_objects_image(url_input, image_input, webcam_input, threshold):
 
 
217
  if url_input and is_valid_url(url_input):
218
  image = get_original_image(url_input)
219
  elif image_input is not None:
@@ -221,30 +231,32 @@ def detect_objects_image(url_input, image_input, webcam_input, threshold):
221
  elif webcam_input is not None:
222
  image = webcam_input
223
  else:
224
- return None, None
 
 
225
 
226
- processed_outputs = make_prediction(image)
227
- viz_img = visualize_prediction(image, processed_outputs, threshold, load_model()[1].config.id2label)
228
- dashboard_fig = get_dashboard()
229
 
230
- return viz_img, dashboard_fig
231
 
232
 
233
  # ---------------- UI ----------------
234
 
235
- title = """<h1 id="title">License Plate Detection + Toll Billing</h1>"""
236
 
237
  description = """
238
  Detect license plates using YOLOS.
239
  Features:
240
- - Image URL
241
- - Image Upload
242
- - Webcam
243
- - Vehicle type classification by plate color
244
- - EV vehicles get 10% discount
245
- - Billing dashboard
246
  """
247
-
 
 
 
 
248
  demo = gr.Blocks()
249
 
250
  with demo:
@@ -281,27 +293,30 @@ with demo:
281
  height=200,
282
  streaming=True
283
  )
284
- img_output_from_webcam = gr.Image(height=400)
285
  dashboard_output_webcam = gr.Plot()
286
  cam_but = gr.Button('Detect')
287
 
288
  url_but.click(
289
- detect_objects_image,
290
- inputs=[url_input, img_input, web_input, slider_input],
291
- outputs=[img_output_from_url, dashboard_output_url]
292
- )
 
293
 
294
- img_but.click(
295
- detect_objects_image,
296
- inputs=[url_input, img_input, web_input, slider_input],
297
- outputs=[img_output_from_upload, dashboard_output_upload]
298
- )
 
299
 
300
- cam_but.click(
301
- detect_objects_image,
302
- inputs=[url_input, img_input, web_input, slider_input],
303
- outputs=[img_output_from_webcam, dashboard_output_webcam]
304
- )
 
305
 
306
 
307
  demo.queue()
 
16
 
17
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
18
 
19
+ base_amt = 100
20
+ #--- calculate discount
21
+ def compute_discount(vehicle_type):
22
+ if vehicle_type == "EV":
23
+ return base_amt * 0.9, "10% discount applied (EV)"
24
+ return toll_parking_amt, "No discount"
25
+ #------------
26
  COLORS = [
27
  [0.000, 0.447, 0.741],
28
  [0.850, 0.325, 0.098],
 
181
  ax = plt.gca()
182
  colors = COLORS * 100
183
 
184
+ result_lines = []
185
+
186
  for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
187
  if "plate" in label.lower():
188
  crop = img.crop((int(xmin), int(ymin), int(xmax), int(ymax)))
189
+
190
+ plate_text = read_plate(crop)
191
+ vehicle_type = classify_plate_color(crop)
192
+ toll, discount_msg = compute_discount(vehicle_type)
193
+
194
+ result_lines.append(
195
+ f"License: {plate_text} | Type: {vehicle_type} | Toll: ₹{int(toll)} | {discount_msg}"
 
 
 
 
 
196
  )
 
197
 
198
  ax.add_patch(
199
  plt.Rectangle(
 
201
  fill=False, color=color, linewidth=4
202
  )
203
  )
204
+
205
  ax.text(
206
  xmin, ymin - 10,
207
+ f"{plate_text} | {vehicle_type} | ₹{int(toll)}",
208
  fontsize=12,
209
  bbox=dict(facecolor="yellow", alpha=0.8)
210
  )
211
 
212
  plt.axis("off")
213
+ final_img = fig2img(plt.gcf())
214
 
215
+ if result_lines:
216
+ result_text = "\n".join(result_lines)
217
+ else:
218
+ result_text = "No license plate detected."
219
+
220
+ return final_img, result_text
221
 
222
  # ---------------- Image Detection ----------------
223
 
224
+ def detect_objects_image(model_name, url_input, image_input, webcam_input, threshold):
225
+ processor, model = load_model(model_name)
226
+
227
  if url_input and is_valid_url(url_input):
228
  image = get_original_image(url_input)
229
  elif image_input is not None:
 
231
  elif webcam_input is not None:
232
  image = webcam_input
233
  else:
234
+ return None, "No image provided."
235
+
236
+ processed_outputs = make_prediction(image, processor, model)
237
 
238
+ viz_img, result_text = visualize_prediction(
239
+ image, processed_outputs, threshold, model.config.id2label
240
+ )
241
 
242
+ return viz_img, result_text
243
 
244
 
245
  # ---------------- UI ----------------
246
 
247
+ title = """<h1 id="title">Smart Vehicle classification</h1>"""
248
 
249
  description = """
250
  Detect license plates using YOLOS.
251
  Features:
252
+ - Image URL, Image Upload, Webcam,Vehicle type classification by plate color
253
+ - EV vehicles get 10% discount on Tolls, Tax, parking
 
 
 
 
254
  """
255
+ result_box = gr.Textbox(
256
+ label="Detection Result",
257
+ lines=5,
258
+ interactive=False
259
+ )
260
  demo = gr.Blocks()
261
 
262
  with demo:
 
293
  height=200,
294
  streaming=True
295
  )
296
+ img_output_from_webcam = gr.Image(height=200)
297
  dashboard_output_webcam = gr.Plot()
298
  cam_but = gr.Button('Detect')
299
 
300
  url_but.click(
301
+ detect_objects_image,
302
+ inputs=[options, url_input, img_input, web_input, slider_input],
303
+ outputs=[img_output_from_url, result_box],
304
+ queue=True
305
+ )
306
 
307
+ img_but.click(
308
+ detect_objects_image,
309
+ inputs=[options, url_input, img_input, web_input, slider_input],
310
+ outputs=[img_output_from_upload, result_box],
311
+ queue=True
312
+ )
313
 
314
+ cam_but.click(
315
+ detect_objects_image,
316
+ inputs=[options, url_input, img_input, web_input, slider_input],
317
+ outputs=[img_output_from_webcam, result_box],
318
+ queue=True
319
+ )
320
 
321
 
322
  demo.queue()