Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,62 @@
|
|
| 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 |
-
|
| 10 |
damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
|
| 11 |
|
| 12 |
-
# License
|
| 13 |
def detect_license_plate(img: Image.Image):
|
| 14 |
-
results =
|
| 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
|
| 21 |
-
|
| 22 |
-
img = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 23 |
|
| 24 |
-
return Image.fromarray(
|
| 25 |
|
| 26 |
-
# Damage
|
| 27 |
-
def classify_damage(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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=
|
| 36 |
-
ax.set_xlim(0, 1)
|
| 37 |
ax.set_xlabel("Confidence")
|
| 38 |
-
ax.
|
|
|
|
| 39 |
plt.tight_layout()
|
| 40 |
|
| 41 |
return fig
|
| 42 |
|
| 43 |
-
#
|
| 44 |
with gr.Blocks() as app:
|
| 45 |
-
gr.Markdown("#
|
| 46 |
-
|
| 47 |
-
with gr.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from transformers import pipeline
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
|
| 9 |
# Load models
|
| 10 |
+
yolo_model = YOLO("yolov8n.pt")
|
| 11 |
damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
|
| 12 |
|
| 13 |
+
# License Plate Detection
|
| 14 |
def detect_license_plate(img: Image.Image):
|
| 15 |
+
results = yolo_model.predict(img)
|
| 16 |
result = results[0]
|
| 17 |
+
img_array = np.array(img.convert("RGB"))
|
| 18 |
|
|
|
|
|
|
|
| 19 |
for box in result.boxes.xyxy:
|
| 20 |
+
x1, y1, x2, y2 = map(int, box)
|
| 21 |
+
img_array = cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
|
|
| 22 |
|
| 23 |
+
return Image.fromarray(img_array)
|
| 24 |
|
| 25 |
+
# Car Damage Classification
|
| 26 |
+
def classify_damage(image: Image.Image):
|
| 27 |
+
results = damage_pipe(image)
|
| 28 |
+
labels = [res["label"] for res in results]
|
| 29 |
+
scores = [res["score"] for res in results]
|
|
|
|
|
|
|
| 30 |
|
| 31 |
fig, ax = plt.subplots()
|
| 32 |
+
ax.barh(labels, scores, color="crimson")
|
|
|
|
| 33 |
ax.set_xlabel("Confidence")
|
| 34 |
+
ax.set_xlim(0, 1)
|
| 35 |
+
ax.set_title("Damage Type Classification")
|
| 36 |
plt.tight_layout()
|
| 37 |
|
| 38 |
return fig
|
| 39 |
|
| 40 |
+
# Build UI
|
| 41 |
with gr.Blocks() as app:
|
| 42 |
+
gr.Markdown("# π Car Analyzer\nChoose a tool below:")
|
| 43 |
+
|
| 44 |
+
with gr.Tab("π License Plate Detection"):
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
lp_input = gr.Image(type="pil", label="Upload Car Image")
|
| 48 |
+
lp_btn = gr.Button("Detect")
|
| 49 |
+
with gr.Column():
|
| 50 |
+
lp_output = gr.Image(label="Detected License Plate")
|
| 51 |
+
lp_btn.click(detect_license_plate, inputs=lp_input, outputs=lp_output)
|
| 52 |
+
|
| 53 |
+
with gr.Tab("π₯ Car Damage Classification"):
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
dmg_input = gr.Image(type="pil", label="Upload Damaged Car Image")
|
| 57 |
+
dmg_btn = gr.Button("Classify")
|
| 58 |
+
with gr.Column():
|
| 59 |
+
dmg_output = gr.Plot(label="Classification Results")
|
| 60 |
+
dmg_btn.click(classify_damage, inputs=dmg_input, outputs=dmg_output)
|
| 61 |
|
| 62 |
app.launch()
|