File size: 1,407 Bytes
26fe3ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
import torch
from ultralytics import YOLO
import cv2
# Load the YOLOv8 model
model = YOLO("best.pt")
# Inference function
def predict(image):
# Run prediction
results = model(image)
# Annotated image
annotated_img = results[0].plot()
# Prepare detection summary
detections = results[0].boxes
output_text = ""
if detections is not None and len(detections.cls) > 0:
output_text += "Prediction Summary:\n\n"
for i, box in enumerate(detections):
cls_id = int(box.cls.item())
conf = float(box.conf.item())
label = model.names[cls_id]
health_status = "Diseased" if label.lower() != "healthy" else "Healthy"
output_text += f"Status: {health_status}\n"
output_text += f"Disease: {label}\n"
output_text += f"Confidence: {conf:.2f}\n\n"
else:
output_text = "No disease detected. The cow appears to be healthy."
return annotated_img, output_text
# Gradio Interface
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=[
gr.Image(type="pil", label="Annotated Image"),
gr.Textbox(label="Prediction Details")
],
title="CowSense - Livestock Disease Detection",
description="Upload an image of a cow to detect health status and disease type using a trained YOLOv8 model."
)
iface.launch() |