Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
-
import torchvision.transforms as transforms
|
| 3 |
-
from PIL import Image
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load model
|
| 7 |
-
model =
|
| 8 |
-
model.eval()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
]
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
])
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
def predict(img):
|
| 26 |
-
img = transform(img).unsqueeze(0)
|
| 27 |
-
with torch.no_grad():
|
| 28 |
-
outputs = model(img)
|
| 29 |
-
_, predicted = torch.max(outputs, 1)
|
| 30 |
-
label = classes[predicted.item()]
|
| 31 |
-
return f"Prediction: {label}"
|
| 32 |
|
| 33 |
-
# Gradio
|
| 34 |
-
gr.Interface(
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
).launch()
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load YOLOv5 model
|
| 7 |
+
model = YOLO("best.pt") # assumes model is in the same directory
|
|
|
|
| 8 |
|
| 9 |
+
# Prediction function
|
| 10 |
+
def predict(image):
|
| 11 |
+
results = model(image)
|
| 12 |
+
boxes = results[0].boxes
|
| 13 |
+
names = model.names
|
| 14 |
+
output = ""
|
|
|
|
| 15 |
|
| 16 |
+
if len(boxes) == 0:
|
| 17 |
+
output = "✅ Healthy: No disease detected!"
|
| 18 |
+
else:
|
| 19 |
+
for box in boxes:
|
| 20 |
+
class_id = int(box.cls[0])
|
| 21 |
+
class_name = names[class_id]
|
| 22 |
+
confidence = float(box.conf[0])
|
| 23 |
+
output += f"🚨 Detected: {class_name} ({confidence*100:.2f}%)\n"
|
| 24 |
|
| 25 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Gradio UI
|
| 28 |
+
gr.Interface(
|
| 29 |
+
fn=predict,
|
| 30 |
+
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="🌿 AgroScan: Plant Disease Detector",
|
| 33 |
+
description="Upload a plant leaf image to detect disease using a YOLOv5 model.",
|
| 34 |
).launch()
|