File size: 9,814 Bytes
a0f7c5a
 
bedbb75
 
3d976a9
 
bedbb75
9917a7b
bedbb75
9917a7b
 
a0f7c5a
7cbf211
b3c2435
bedbb75
 
 
a0f7c5a
 
 
 
 
bedbb75
 
 
 
 
 
a0f7c5a
bedbb75
 
9917a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
a0f7c5a
9917a7b
 
a0f7c5a
9917a7b
a0f7c5a
 
 
 
 
 
 
 
 
 
 
 
 
b3c2435
 
a0f7c5a
f0023ea
b3c2435
 
 
 
 
 
 
 
 
 
 
 
9917a7b
b3c2435
 
a0f7c5a
 
 
b3c2435
 
 
 
 
 
9917a7b
 
bedbb75
a0f7c5a
 
bedbb75
 
 
9917a7b
bedbb75
 
 
3d976a9
bedbb75
 
 
a0f7c5a
 
bedbb75
 
 
 
 
9917a7b
bedbb75
a0f7c5a
 
bedbb75
9917a7b
bedbb75
 
 
 
 
a0f7c5a
 
bedbb75
 
 
 
 
 
 
 
 
 
 
 
 
a0f7c5a
 
 
 
bedbb75
 
a0f7c5a
 
 
 
 
 
bedbb75
 
 
 
 
 
 
 
 
a0f7c5a
bedbb75
a0f7c5a
bedbb75
 
 
d85a72a
bedbb75
 
9917a7b
bedbb75
a0f7c5a
9917a7b
bedbb75
 
 
 
 
 
 
 
a0f7c5a
 
 
 
bedbb75
 
 
 
9917a7b
bedbb75
a0f7c5a
bedbb75
 
3d976a9
a0f7c5a
bedbb75
 
 
3d976a9
a0f7c5a
bedbb75
 
 
3d976a9
bedbb75
3d976a9
bedbb75
 
 
 
3d976a9
bedbb75
 
 
a0f7c5a
bedbb75
 
 
 
 
 
 
 
 
0bfeae4
9917a7b
 
 
bedbb75
 
 
 
 
a0f7c5a
bedbb75
 
 
9917a7b
bedbb75
 
 
 
a0f7c5a
bedbb75
 
 
3d976a9
bedbb75
 
3d976a9
bedbb75
3d976a9
 
9917a7b
3d976a9
a0f7c5a
3d976a9
bedbb75
a0f7c5a
 
 
bedbb75
3d976a9
a0f7c5a
 
 
3d976a9
9917a7b
3d976a9
bedbb75
 
 
3d976a9
a0f7c5a
 
 
3d976a9
bedbb75
a0f7c5a
bedbb75
a0f7c5a
9917a7b
bedbb75
873b765
a0f7c5a
bedbb75
a0f7c5a
bedbb75
a0f7c5a
873b765
a0f7c5a
bedbb75
a0f7c5a
bedbb75
94f6a99
a0f7c5a
94f6a99
873b765
a0f7c5a
bedbb75
a0f7c5a
bedbb75
 
 
a0f7c5a
bedbb75
 
 
a0f7c5a
bedbb75
a0f7c5a
bedbb75
3d976a9
bedbb75
 
a0f7c5a
bedbb75
a0f7c5a
bedbb75
3d976a9
bedbb75
 
a0f7c5a
bedbb75
a0f7c5a
bedbb75
3d976a9
bedbb75
 
a0f7c5a
bedbb75
a0f7c5a
bedbb75
3d976a9
 
906908a
a0f7c5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# app.py (Clean Final Version for HF Spaces)

import io
import os
import cv2
import gradio as gr
import matplotlib.pyplot as plt
import requests
import torch
import numpy as np
from urllib.parse import urlparse
from PIL import Image
from transformers import YolosImageProcessor, YolosForObjectDetection
import easyocr

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

# ---------------- Globals (lazy loaded) ----------------
processor = None
model = None
reader = easyocr.Reader(["en"], gpu=False)

COLORS = [
    [0.000, 0.447, 0.741],
    [0.850, 0.325, 0.098],
    [0.929, 0.694, 0.125],
    [0.494, 0.184, 0.556],
    [0.466, 0.674, 0.188],
    [0.301, 0.745, 0.933],
]

# ---------------- Utilities ----------------

def is_valid_url(url):
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc])
    except Exception:
        return False


def get_original_image(url_input):
    if url_input and is_valid_url(url_input):
        image = Image.open(requests.get(url_input, stream=True).raw).convert("RGB")
        return image
    return None


# ---------------- Model Loader ----------------

def load_model():
    global processor, model
    if processor is None or model is None:
        processor = YolosImageProcessor.from_pretrained(
            "nickmuchi/yolos-small-finetuned-license-plate-detection"
        )
        model = YolosForObjectDetection.from_pretrained(
            "nickmuchi/yolos-small-finetuned-license-plate-detection",
            use_safetensors=True,
            torch_dtype=torch.float32,
        )
        model.eval()
    return processor, model


# ---------------- Plate Color Classifier ----------------

def classify_plate_color(plate_img):
    img = np.array(plate_img)
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

    green = np.sum(cv2.inRange(hsv, (35, 40, 40), (85, 255, 255)))
    yellow = np.sum(cv2.inRange(hsv, (15, 50, 50), (35, 255, 255)))
    white = np.sum(cv2.inRange(hsv, (0, 0, 200), (180, 30, 255)))

    if green > yellow and green > white:
        return "EV"
    elif yellow > green and yellow > white:
        return "Commercial"
    else:
        return "Personal"


# ---------------- OCR ----------------

def read_plate(plate_img):
    results = reader.readtext(np.array(plate_img))
    if results:
        return results[0][1]
    return "UNKNOWN"


# ---------------- Core Inference ----------------

def make_prediction(img):
    processor, model = load_model()
    inputs = processor(images=img, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)

    img_size = torch.tensor([tuple(reversed(img.size))])
    processed_outputs = processor.post_process_object_detection(
        outputs, threshold=0.0, target_sizes=img_size
    )
    return processed_outputs[0]


# ---------------- Visualization ----------------

def fig2img(fig):
    buf = io.BytesIO()
    fig.savefig(buf)
    buf.seek(0)
    pil_img = Image.open(buf)

    basewidth = 750
    wpercent = basewidth / float(pil_img.size[0])
    hsize = int(float(pil_img.size[1]) * float(wpercent))
    img = pil_img.resize((basewidth, hsize), Image.Resampling.LANCZOS)

    plt.close(fig)
    return img


def visualize_prediction(img, output_dict, threshold=0.5, id2label=None):
    BASE_TOLL = 100  # base amount for all vehicles

    keep = output_dict["scores"] > threshold
    boxes = output_dict["boxes"][keep].tolist()
    scores = output_dict["scores"][keep].tolist()
    labels = output_dict["labels"][keep].tolist()

    if id2label is not None:
        labels = [id2label[x] for x in labels]

    plt.figure(figsize=(20, 20))
    plt.imshow(img)
    ax = plt.gca()
    colors = COLORS * 100

    for score, (xmin, ymin, xmax, ymax), label, color in zip(
        scores, boxes, labels, colors
    ):
        if "plate" in label.lower():
            crop = img.crop((int(xmin), int(ymin), int(xmax), int(ymax)))
            plate_type = classify_plate_color(crop)
            # Apply 10% discount for EV vehicles
            if plate_type == "EV":
                discounted_amount = BASE_TOLL * 0.9
                price_text = f"EV | ₹{discounted_amount:.0f} (10% off)"
            else:
                price_text = f"{plate_type} | ₹{BASE_TOLL}"

            ax.add_patch(
                plt.Rectangle(
                    (xmin, ymin), xmax - xmin, ymax - ymin,
                    fill=False, color=color, linewidth=4
                )
            )
            ax.text(
                xmin, ymin - 10,
                f"{price_text} | {score:0.2f}",
                fontsize=12,
                bbox=dict(facecolor="yellow", alpha=0.8),
            )

    plt.axis("off")
    return fig2img(plt.gcf())


# ---------------- Image Detection ----------------

def detect_objects_image(url_input, image_input, webcam_input, threshold):
    if url_input and is_valid_url(url_input):
        image = get_original_image(url_input)
    elif image_input is not None:
        image = image_input
    elif webcam_input is not None:
        image = webcam_input
    else:
        return None

    processed_outputs = make_prediction(image)
    viz_img = visualize_prediction(
        image, processed_outputs, threshold, load_model()[1].config.id2label
    )

    return viz_img


# ---------------- Video Detection ----------------

def detect_objects_video(video_input, threshold):
    if video_input is None:
        return None

    processor, model = load_model()

    cap = cv2.VideoCapture(video_input)
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")

    output_path = "/tmp/output_detected.mp4"
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        pil_img = Image.fromarray(rgb_frame)

        processed_outputs = make_prediction(pil_img)

        keep = processed_outputs["scores"] > threshold
        boxes = processed_outputs["boxes"][keep].tolist()
        scores = processed_outputs["scores"][keep].tolist()
        labels = processed_outputs["labels"][keep].tolist()

        labels = [model.config.id2label[x] for x in labels]

        for score, (xmin, ymin, xmax, ymax), label in zip(scores, boxes, labels):
            if "plate" in label.lower():
                crop = pil_img.crop((int(xmin), int(ymin), int(xmax), int(ymax)))
                plate_type = classify_plate_color(crop)

                cv2.rectangle(
                    frame,
                    (int(xmin), int(ymin)),
                    (int(xmax), int(ymax)),
                    (0, 255, 0),
                    2,
                )
                cv2.putText(
                    frame,
                    f"{plate_type} | {score:.2f}",
                    (int(xmin), int(ymin) - 10),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.6,
                    (0, 255, 0),
                    2,
                )

        out.write(frame)

    cap.release()
    out.release()

    return output_path


# ---------------- UI ----------------

title = """<h1 id="title">Smart Vehicle Classification (Image + Video)</h1>"""

description = """
Smart Vehicle Classification system to Promote EV by applying discount on Toll,
Tax, parking. 
Supports:Image URL, Image Upload, Webcam, Video Upload,Vehicle type classification by plate color
"""

css = """
h1#title { text-align: center; }
"""

demo = gr.Blocks()

with demo:
    gr.Markdown(title)
    gr.Markdown(description)

    slider_input = gr.Slider(
        minimum=0.2, maximum=1, value=0.5, step=0.1, label="Prediction Threshold"
    )

    with gr.Tabs():
        with gr.TabItem("Image URL"):
            with gr.Row():
                url_input = gr.Textbox(lines=2, label="Enter valid image URL here..")
                original_image = gr.Image(height=750, width=750)
                url_input.change(get_original_image, url_input, original_image)
                img_output_from_url = gr.Image(height=750, width=750)
            url_but = gr.Button("Detect")

        with gr.TabItem("Image Upload"):
            with gr.Row():
                img_input = gr.Image(type="pil", height=750, width=750)
                img_output_from_upload = gr.Image(height=750, width=750)
            img_but = gr.Button("Detect")

        with gr.TabItem("WebCam"):
            with gr.Row():
                web_input = gr.Image(
                    sources=["webcam"], type="pil", height=750, width=750, streaming=True
                )
                img_output_from_webcam = gr.Image(height=750, width=750)
            cam_but = gr.Button("Detect")

        with gr.TabItem("Video Upload"):
            with gr.Row():
                video_input = gr.Video(label="Upload Video")
                video_output = gr.Video(label="Detected Video")
            vid_but = gr.Button("Detect Video")

    url_but.click(
        detect_objects_image,
        inputs=[url_input, img_input, web_input, slider_input],
        outputs=[img_output_from_url],
        queue=True,
    )

    img_but.click(
        detect_objects_image,
        inputs=[url_input, img_input, web_input, slider_input],
        outputs=[img_output_from_upload],
        queue=True,
    )

    cam_but.click(
        detect_objects_image,
        inputs=[url_input, img_input, web_input, slider_input],
        outputs=[img_output_from_webcam],
        queue=True,
    )

    vid_but.click(
        detect_objects_video,
        inputs=[video_input, slider_input],
        outputs=[video_output],
        queue=True,
    )


demo.queue()
demo.launch(debug=True, ssr_mode=False)