Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
import cv2
|
| 4 |
-
import easyocr
|
| 5 |
-
import numpy as np
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
-
|
| 11 |
-
model.fuse()
|
| 12 |
-
|
| 13 |
-
# Load EasyOCR
|
| 14 |
-
reader = easyocr.Reader(['en'])
|
| 15 |
-
|
| 16 |
-
# Load Transformers pipeline for car damage classification
|
| 17 |
damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
def detect_license_plate(
|
| 21 |
-
results =
|
| 22 |
-
|
| 23 |
-
img_array = np.array(image)
|
| 24 |
-
img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 25 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 26 |
-
|
| 27 |
-
img_height, img_width, _ = img.shape
|
| 28 |
-
|
| 29 |
-
for result in results:
|
| 30 |
-
for bbox in result.boxes.xyxy:
|
| 31 |
-
x1, y1, x2, y2 = map(int, bbox.tolist())
|
| 32 |
-
plate = img[int(y1):int(y2), int(x1):int(x2)]
|
| 33 |
-
scale = 2
|
| 34 |
-
height, width = plate.shape[:2]
|
| 35 |
-
plate = cv2.resize(plate, (width * scale, height * scale), interpolation=cv2.INTER_CUBIC)
|
| 36 |
-
lab = cv2.cvtColor(plate, cv2.COLOR_RGB2LAB)
|
| 37 |
-
l, a, b = cv2.split(lab)
|
| 38 |
-
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 39 |
-
l = clahe.apply(l)
|
| 40 |
-
plate = cv2.merge((l, a, b))
|
| 41 |
-
plate = cv2.cvtColor(plate, cv2.COLOR_LAB2RGB)
|
| 42 |
-
|
| 43 |
-
text = reader.readtext(plate, detail=0, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-")
|
| 44 |
-
text = " ".join(text).upper()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
text_box_y1 = text_y - text_height - 5
|
| 53 |
-
text_box_y2 = text_y + 5
|
| 54 |
-
cv2.rectangle(img, (text_x - 8, text_box_y1 - 3), (text_x + text_width + 8, text_box_y2 + 3), (0, 0, 0), -1)
|
| 55 |
-
cv2.rectangle(img, (text_x - 5, text_box_y1), (text_x + text_width + 5, text_box_y2), (255, 255, 255), -1)
|
| 56 |
-
cv2.putText(img, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, text_scale, (0, 0, 0), thickness)
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
fig
|
| 72 |
-
data=[go.Bar(x=labels, y=scores, marker_color="crimson")],
|
| 73 |
-
layout=go.Layout(
|
| 74 |
-
title="Car Damage Classification",
|
| 75 |
-
yaxis=dict(title="Confidence Score", range=[0, 1]),
|
| 76 |
-
xaxis=dict(title="Damage Type"),
|
| 77 |
-
template="plotly_white"
|
| 78 |
-
)
|
| 79 |
-
)
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
outputs=[
|
| 88 |
-
gr.Image(type="pil", label="Detected Image"),
|
| 89 |
-
gr.Textbox(label="Detected License Plates")
|
| 90 |
-
],
|
| 91 |
-
title="π License Plate Detector",
|
| 92 |
-
description="Upload an image with license plates to detect them using YOLO and EasyOCR."
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
fn=
|
| 97 |
-
inputs=gr.Image(type="pil", label="Upload Damaged Car Image"),
|
| 98 |
-
outputs=[
|
| 99 |
-
gr.Image(type="pil", label="Car Image"),
|
| 100 |
-
gr.Plot(label="Damage Classification")
|
| 101 |
-
],
|
| 102 |
-
title="π₯ Car Damage Detector",
|
| 103 |
-
description="Classifies car damage severity (minor, moderate, severe)."
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Load models
|
| 9 |
+
license_model = YOLO('yolov8n.pt') # public YOLOv8 model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
|
| 11 |
|
| 12 |
+
# License plate detection
|
| 13 |
+
def detect_license_plate(img: Image.Image):
|
| 14 |
+
results = license_model.predict(img)
|
| 15 |
+
result = results[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Plot boxes
|
| 18 |
+
img = np.array(img.convert("RGB"))
|
| 19 |
+
for box in result.boxes.xyxy:
|
| 20 |
+
x1, y1, x2, y2 = box.int().tolist()
|
| 21 |
+
img = img.copy()
|
| 22 |
+
img = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
return Image.fromarray(img)
|
| 25 |
|
| 26 |
+
# Damage classification with bar chart
|
| 27 |
+
def classify_damage(img: Image.Image):
|
| 28 |
+
predictions = damage_pipe(img)
|
| 29 |
|
| 30 |
+
# Prepare bar graph
|
| 31 |
+
labels = [pred["label"] for pred in predictions]
|
| 32 |
+
scores = [pred["score"] for pred in predictions]
|
| 33 |
|
| 34 |
+
fig, ax = plt.subplots()
|
| 35 |
+
ax.barh(labels, scores, color='orange')
|
| 36 |
+
ax.set_xlim(0, 1)
|
| 37 |
+
ax.set_xlabel("Confidence")
|
| 38 |
+
ax.set_title("Damage Classification")
|
| 39 |
+
plt.tight_layout()
|
| 40 |
|
| 41 |
+
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Combined interface
|
| 44 |
+
with gr.Blocks() as app:
|
| 45 |
+
gr.Markdown("# π Car License Plate & Damage Detector")
|
| 46 |
|
| 47 |
+
with gr.Row():
|
| 48 |
+
image_input = gr.Image(type="pil", label="Upload Car Image")
|
| 49 |
+
license_output = gr.Image(label="License Plate Detection")
|
| 50 |
+
damage_output = gr.Plot(label="Damage Classification")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
image_input.change(fn=detect_license_plate, inputs=image_input, outputs=license_output)
|
| 53 |
+
image_input.change(fn=classify_damage, inputs=image_input, outputs=damage_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
app.launch()
|