|
|
import gradio as gr |
|
|
import torch |
|
|
from ultralytics import YOLO |
|
|
import cv2 |
|
|
|
|
|
|
|
|
model = YOLO("best.pt") |
|
|
|
|
|
|
|
|
def predict(image): |
|
|
|
|
|
results = model(image) |
|
|
|
|
|
|
|
|
annotated_img = results[0].plot() |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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() |