Sarvamangalak commited on
Commit
8804e72
·
verified ·
1 Parent(s): 24dc078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -30
app.py CHANGED
@@ -2,9 +2,8 @@ import gradio as gr
2
  import cv2
3
  import numpy as np
4
  import easyocr
5
- from datetime import datetime
6
 
7
- reader = easyocr.Reader(['en'])
8
 
9
  feedback_data = []
10
 
@@ -14,22 +13,20 @@ feedback_data = []
14
 
15
  def classify_vehicle_by_plate_color(plate_img):
16
 
17
- img = np.array(plate_img)
18
- hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
19
 
20
  green = np.sum(cv2.inRange(hsv, (35, 40, 40), (85, 255, 255)))
21
  yellow = np.sum(cv2.inRange(hsv, (15, 50, 50), (35, 255, 255)))
22
  white = np.sum(cv2.inRange(hsv, (0, 0, 200), (180, 30, 255)))
23
 
24
  if green > yellow and green > white:
25
- return "EV"
26
  elif yellow > green and yellow > white:
27
- return "Commercial"
28
  else:
29
- return "Personal"
30
 
31
 
32
-
33
  #########################################################
34
  # 2️⃣ Detection + OCR + EV Benefits
35
  #########################################################
@@ -49,15 +46,22 @@ def detect_vehicles(image):
49
  count = 0
50
 
51
  for cnt in contours:
 
52
  x, y, w, h = cv2.boundingRect(cnt)
53
 
54
- # Indian plate shape filter
55
- if 2 < w/h < 6 and w > 100:
 
 
 
 
 
56
 
57
  plate_img = img[y:y+h, x:x+w]
58
 
59
- # OCR for plate number
60
  results = reader.readtext(plate_img)
 
61
  plate_number = "Unknown"
62
 
63
  if len(results) > 0:
@@ -71,16 +75,20 @@ def detect_vehicles(image):
71
  else:
72
  benefit = "No EV Benefits"
73
 
74
- # Draw box + label
75
  cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
76
 
77
  label = f"{plate_number} | {vehicle_type}"
78
- cv2.putText(img, label,
79
- (x, y-10),
80
- cv2.FONT_HERSHEY_SIMPLEX,
81
- 0.6,
82
- (0,255,0),
83
- 2)
 
 
 
 
84
 
85
  detected_summary.append(
86
  f"Plate: {plate_number} | Type: {vehicle_type} | {benefit}"
@@ -89,7 +97,7 @@ def detect_vehicles(image):
89
  count += 1
90
 
91
  if count == 0:
92
- summary = "No vehicles detected."
93
  else:
94
  summary = "\n".join(detected_summary)
95
 
@@ -103,23 +111,21 @@ def detect_vehicles(image):
103
  #########################################################
104
 
105
  def submit_feedback(is_correct):
106
- feedback_data.append(is_correct)
107
- return generate_summary()
108
 
 
109
 
110
- def generate_summary():
111
  total = len(feedback_data)
112
- if total == 0:
113
- return "No evaluations yet."
114
 
115
  correct = sum(feedback_data)
116
- accuracy = (correct/total) * 100
 
117
 
118
  return f"""
119
- Evaluation Summary:
120
- Total Samples: {total}
121
- Correct: {correct}
122
- Accuracy: {accuracy:.2f}%
 
123
  """
124
 
125
 
@@ -140,6 +146,7 @@ with gr.Blocks() as demo:
140
  detect_btn = gr.Button("Detect", size="sm")
141
 
142
  output_image = gr.Image(label="Output")
 
143
  output_text = gr.Textbox(label="Detection Summary")
144
 
145
  with gr.Column(scale=1):
@@ -147,6 +154,7 @@ with gr.Blocks() as demo:
147
  gr.Markdown("### Feedback")
148
 
149
  correct_btn = gr.Button("Correct", size="sm")
 
150
  incorrect_btn = gr.Button("Incorrect", size="sm")
151
 
152
  summary_box = gr.Textbox(label="Evaluation Summary")
@@ -167,4 +175,4 @@ with gr.Blocks() as demo:
167
  outputs=summary_box
168
  )
169
 
170
- demo.launch(theme=gr.themes.Soft())
 
2
  import cv2
3
  import numpy as np
4
  import easyocr
 
5
 
6
+ reader = easyocr.Reader(['en'], gpu=False)
7
 
8
  feedback_data = []
9
 
 
13
 
14
  def classify_vehicle_by_plate_color(plate_img):
15
 
16
+ hsv = cv2.cvtColor(plate_img, cv2.COLOR_BGR2HSV)
 
17
 
18
  green = np.sum(cv2.inRange(hsv, (35, 40, 40), (85, 255, 255)))
19
  yellow = np.sum(cv2.inRange(hsv, (15, 50, 50), (35, 255, 255)))
20
  white = np.sum(cv2.inRange(hsv, (0, 0, 200), (180, 30, 255)))
21
 
22
  if green > yellow and green > white:
23
+ return "EV", True
24
  elif yellow > green and yellow > white:
25
+ return "Commercial", False
26
  else:
27
+ return "Personal", False
28
 
29
 
 
30
  #########################################################
31
  # 2️⃣ Detection + OCR + EV Benefits
32
  #########################################################
 
46
  count = 0
47
 
48
  for cnt in contours:
49
+
50
  x, y, w, h = cv2.boundingRect(cnt)
51
 
52
+ # plate shape filtering
53
+ if h == 0:
54
+ continue
55
+
56
+ ratio = w / h
57
+
58
+ if 2 < ratio < 6 and w > 120 and h > 30:
59
 
60
  plate_img = img[y:y+h, x:x+w]
61
 
62
+ # OCR
63
  results = reader.readtext(plate_img)
64
+
65
  plate_number = "Unknown"
66
 
67
  if len(results) > 0:
 
75
  else:
76
  benefit = "No EV Benefits"
77
 
78
+ # draw detection
79
  cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
80
 
81
  label = f"{plate_number} | {vehicle_type}"
82
+
83
+ cv2.putText(
84
+ img,
85
+ label,
86
+ (x, y-10),
87
+ cv2.FONT_HERSHEY_SIMPLEX,
88
+ 0.6,
89
+ (0,255,0),
90
+ 2
91
+ )
92
 
93
  detected_summary.append(
94
  f"Plate: {plate_number} | Type: {vehicle_type} | {benefit}"
 
97
  count += 1
98
 
99
  if count == 0:
100
+ summary = "No number plate detected."
101
  else:
102
  summary = "\n".join(detected_summary)
103
 
 
111
  #########################################################
112
 
113
  def submit_feedback(is_correct):
 
 
114
 
115
+ feedback_data.append(is_correct)
116
 
 
117
  total = len(feedback_data)
 
 
118
 
119
  correct = sum(feedback_data)
120
+
121
+ accuracy = (correct / total) * 100
122
 
123
  return f"""
124
+ Evaluation Summary
125
+ -------------------
126
+ Total Samples : {total}
127
+ Correct : {correct}
128
+ Accuracy : {accuracy:.2f} %
129
  """
130
 
131
 
 
146
  detect_btn = gr.Button("Detect", size="sm")
147
 
148
  output_image = gr.Image(label="Output")
149
+
150
  output_text = gr.Textbox(label="Detection Summary")
151
 
152
  with gr.Column(scale=1):
 
154
  gr.Markdown("### Feedback")
155
 
156
  correct_btn = gr.Button("Correct", size="sm")
157
+
158
  incorrect_btn = gr.Button("Incorrect", size="sm")
159
 
160
  summary_box = gr.Textbox(label="Evaluation Summary")
 
175
  outputs=summary_box
176
  )
177
 
178
+ demo.launch()