|
from ultralytics import YOLO
|
|
import cv2
|
|
import torch
|
|
import gradio as gr
|
|
from PIL import Image
|
|
import numpy as np
|
|
|
|
|
|
model = YOLO("best.pt")
|
|
|
|
def predict(input_img):
|
|
|
|
image = np.array(input_img)
|
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
results = model(image)
|
|
|
|
|
|
for result in results:
|
|
for box in result.boxes:
|
|
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
|
conf = float(box.conf[0])
|
|
label = f"Damage: {conf:.2f}"
|
|
|
|
|
|
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 3)
|
|
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
|
|
|
|
|
output_img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
|
return output_img
|
|
|
|
|
|
gradio_app = gr.Interface(
|
|
fn=predict,
|
|
inputs=gr.Image(label="Upload a car image", sources=['upload', 'webcam'], type="pil"),
|
|
outputs=gr.Image(label="Detected Damage"),
|
|
title="Car Damage Detection",
|
|
description="Upload an image of a car, and the model will detect and highlight damaged areas."
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
gradio_app.launch()
|
|
|