|
import gradio as gr |
|
from ultralytics import YOLO |
|
from PIL import Image, ImageDraw |
|
import numpy as np |
|
|
|
|
|
model = YOLO("my_yolo_model.onnx") |
|
|
|
def predict(image): |
|
|
|
pil_image = Image.fromarray(image) |
|
|
|
|
|
results = model(pil_image) |
|
|
|
|
|
boxes = results[0].boxes.xyxy.cpu().numpy() |
|
classes = results[0].boxes.cls.cpu().numpy() |
|
confidences = results[0].boxes.conf.cpu().numpy() |
|
|
|
|
|
draw = ImageDraw.Draw(pil_image) |
|
for box, cls, conf in zip(boxes, classes, confidences): |
|
x1, y1, x2, y2 = box |
|
label = f"{model.names[int(cls)]} {conf:.2f}" |
|
|
|
|
|
draw.rectangle([x1, y1, x2, y2], outline="blue", width=2) |
|
draw.text((x1, y1), label, fill="red") |
|
|
|
return pil_image |
|
|
|
|
|
demo = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(label="Input Image"), |
|
outputs=gr.Image(label="Detected Objects"), |
|
title="Pathole Detection by Yunusa Jibrin ", |
|
examples=["example1.jpg", "example2.jpg"], |
|
) |
|
|
|
demo.launch() |