Sarvamangalak commited on
Commit
d852fbc
·
verified ·
1 Parent(s): 1ad0707

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -21,6 +21,19 @@ COLORS = [
21
  [0.301, 0.745, 0.933]
22
  ]
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def make_prediction(img, feature_extractor, model):
25
  inputs = feature_extractor(img, return_tensors="pt")
26
  outputs = model(**inputs)
@@ -44,23 +57,21 @@ def visualize_prediction(img, output_dict, threshold=0.5, id2label=None):
44
  boxes = output_dict["boxes"].tolist()
45
  scores = output_dict["scores"].tolist()
46
  labels = output_dict["labels"].tolist()
47
-
48
  if id2label is not None:
49
-
50
  labels = [id2label[x] for x in labels]
51
-
52
 
53
- plt.figure(figsize=(50, 50))
54
  plt.imshow(img)
55
  ax = plt.gca()
56
  colors = COLORS * 100
 
57
  for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
58
- if label == 'license-plates':
 
59
 
60
- # Crop plate
61
  plate_crop = img.crop((xmin, ymin, xmax, ymax))
62
-
63
- # Check EV
64
  ev = is_green_plate(plate_crop)
65
 
66
  if ev:
@@ -69,19 +80,23 @@ def visualize_prediction(img, output_dict, threshold=0.5, id2label=None):
69
  else:
70
  plate_type = "Non-EV Plate"
71
  box_color = "red"
 
 
 
72
 
73
  ax.add_patch(
74
  plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
75
- fill=False, color=box_color, linewidth=10)
76
  )
77
 
78
  ax.text(
79
- xmin, ymin - 20,
80
- f"{plate_type} | {score:0.2f}",
81
- fontsize=50,
82
  bbox=dict(facecolor=box_color, alpha=0.7),
83
  color="white"
84
  )
 
85
  plt.axis("off")
86
  return fig2img(plt.gcf())
87
 
@@ -111,7 +126,7 @@ def detect_objects(model_name,url_input,image_input,webcam_input,threshold):
111
  image = webcam_input
112
 
113
  #Make prediction
114
- processed_outputs = make_prediction(image, feature_extractor, model)
115
 
116
  #Visualize prediction
117
  viz_img = visualize_prediction(image, processed_outputs, threshold, model.config.id2label)
 
21
  [0.301, 0.745, 0.933]
22
  ]
23
 
24
+ import numpy as np
25
+ import tensorflow as tf
26
+
27
+ # Load EV plate classifier
28
+ ev_model = tf.keras.models.load_model("plate_color_model.h5")
29
+
30
+ def is_green_plate(plate_img):
31
+ plate_img = plate_img.resize((128,128))
32
+ plate_img = np.array(plate_img)/255.0
33
+ plate_img = np.expand_dims(plate_img, axis=0)
34
+ pred = ev_model.predict(plate_img)[0][0]
35
+ return pred > 0.5
36
+
37
  def make_prediction(img, feature_extractor, model):
38
  inputs = feature_extractor(img, return_tensors="pt")
39
  outputs = model(**inputs)
 
57
  boxes = output_dict["boxes"].tolist()
58
  scores = output_dict["scores"].tolist()
59
  labels = output_dict["labels"].tolist()
60
+
61
  if id2label is not None:
 
62
  labels = [id2label[x] for x in labels]
 
63
 
64
+ plt.figure(figsize=(20, 20))
65
  plt.imshow(img)
66
  ax = plt.gca()
67
  colors = COLORS * 100
68
+
69
  for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
70
+ if score < threshold:
71
+ continue
72
 
73
+ if label in ["license-plates", "Rego Plates"]:
74
  plate_crop = img.crop((xmin, ymin, xmax, ymax))
 
 
75
  ev = is_green_plate(plate_crop)
76
 
77
  if ev:
 
80
  else:
81
  plate_type = "Non-EV Plate"
82
  box_color = "red"
83
+ else:
84
+ plate_type = label
85
+ box_color = "blue"
86
 
87
  ax.add_patch(
88
  plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
89
+ fill=False, color=box_color, linewidth=3)
90
  )
91
 
92
  ax.text(
93
+ xmin, ymin - 10,
94
+ f"{plate_type} | {score:.2f}",
95
+ fontsize=14,
96
  bbox=dict(facecolor=box_color, alpha=0.7),
97
  color="white"
98
  )
99
+
100
  plt.axis("off")
101
  return fig2img(plt.gcf())
102
 
 
126
  image = webcam_input
127
 
128
  #Make prediction
129
+ processed_outputs = make_prediction(image.convert("RGB"), feature_extractor, model)
130
 
131
  #Visualize prediction
132
  viz_img = visualize_prediction(image, processed_outputs, threshold, model.config.id2label)