|
import gradio as gr |
|
import torch |
|
from PIL import Image, ImageDraw, ImageFont |
|
import io |
|
from ultralytics import YOLO |
|
|
|
|
|
MODEL_PATH = 'model/char.pt' |
|
|
|
try: |
|
model = YOLO(MODEL_PATH) |
|
print(f"Model loaded successfully from: {MODEL_PATH}") |
|
except Exception as e: |
|
print(f"Error loading model: {e}") |
|
model = None |
|
|
|
|
|
def predict(image): |
|
if model is None: |
|
return "Model is not loaded properly." |
|
|
|
try: |
|
img = Image.fromarray(image).convert('RGB') |
|
results = model(img) |
|
|
|
draw = ImageDraw.Draw(img) |
|
font = ImageFont.load_default() |
|
|
|
for result in results: |
|
if hasattr(result, 'boxes') and result.boxes is not None: |
|
for box in result.boxes: |
|
x1, y1, x2, y2 = map(int, box.xyxy[0]) |
|
label = model.model.names[int(box.cls)] |
|
confidence = float(box.conf[0]) |
|
|
|
|
|
draw.rectangle([x1, y1, x2, y2], outline="green", width=3) |
|
text = f"{label} ({confidence:.2f})" |
|
draw.text((x1, y1 - 10), text, fill="red", font=font) |
|
|
|
return img |
|
|
|
except Exception as e: |
|
return f"Error during prediction: {e}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(label="Upload an Image"), |
|
outputs=gr.Image(label="Image with Predictions"), |
|
title="YOLO Object Detection", |
|
description="Upload an image to see object detection predictions using a YOLO model.", |
|
) |
|
|
|
iface.launch() |
|
|