|
import gradio as gr |
|
from ultralytics import YOLO |
|
import cv2 |
|
import numpy as np |
|
import os |
|
import tempfile |
|
import base64 |
|
from io import BytesIO |
|
from PIL import Image |
|
|
|
def perform(image, task: str="detect"): |
|
|
|
results = model(image, save=True) |
|
|
|
|
|
prediction_image = Image.open(os.path.join(results[0].save_dir, os.listdir(results[0].save_dir)[0])) |
|
|
|
|
|
buffered = BytesIO() |
|
prediction_image.save(buffered, format="JPEG") |
|
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8') |
|
|
|
|
|
outputs = [] |
|
|
|
|
|
for result in results[0]: |
|
xywh = result.boxes.xyxy[0].numpy().tolist() |
|
name = result.names[result.boxes.cls.item()] |
|
confs = float(result.boxes.conf) |
|
outputs.append({"xywh": xywh, "conf": confs, "name": name}) |
|
|
|
|
|
|
|
return prediction_image, outputs, {"image_base64": f"data:image/jpeg;base64,{img_base64}"} |
|
|
|
def load_model(model_name: str = "yolo11n.pt"): |
|
"""Model loading""" |
|
try: |
|
print(f"Loading the model {model_name}") |
|
model = YOLO(model_name) |
|
except Exception as e: |
|
print(f"Error loading the model {e}") |
|
return model |
|
|
|
|
|
model = load_model() |
|
|
|
|
|
demo = gr.Interface( |
|
fn=perform, |
|
inputs=[gr.Image(type="pil")], |
|
outputs=[gr.Image(type="pil"), gr.JSON(), gr.JSON()], |
|
title="Object Detection using YOLO", |
|
description="Upload an image and get image with bbox in it", |
|
examples=None |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |