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