Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load the YOLOv8 model
|
| 6 |
-
model = YOLO(
|
| 7 |
|
|
|
|
| 8 |
def predict(image):
|
| 9 |
-
#
|
| 10 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 11 |
|
| 12 |
# Run prediction
|
| 13 |
results = model(img)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
result_img = results[0].plot()
|
| 17 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 18 |
|
| 19 |
return result_img
|
| 20 |
|
| 21 |
-
# Gradio interface
|
| 22 |
iface = gr.Interface(
|
| 23 |
-
fn=predict,
|
| 24 |
-
inputs=gr.Image(type="numpy"),
|
| 25 |
-
outputs=gr.Image(type="numpy")
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# Step 1: Download YOLOv8 weights from Hugging Face
|
| 7 |
+
weights_path = hf_hub_download(
|
| 8 |
+
repo_id="foduucom/plant-leaf-detection-and-classification",
|
| 9 |
+
filename="best.pt"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# Step 2: Load the YOLOv8 model
|
| 13 |
+
model = YOLO(weights_path)
|
| 14 |
|
| 15 |
+
# Step 3: Define prediction function
|
| 16 |
def predict(image):
|
| 17 |
+
# Convert RGB to BGR for YOLO
|
| 18 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 19 |
|
| 20 |
# Run prediction
|
| 21 |
results = model(img)
|
| 22 |
|
| 23 |
+
# Draw bounding boxes with class names and confidence
|
| 24 |
+
result_img = results[0].plot()
|
| 25 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 26 |
|
| 27 |
return result_img
|
| 28 |
|
| 29 |
+
# Step 4: Create Gradio interface
|
| 30 |
iface = gr.Interface(
|
| 31 |
+
fn=predict,
|
| 32 |
+
inputs=gr.Image(type="numpy", tool="editor"), # allows upload & minor edits
|
| 33 |
+
outputs=gr.Image(type="numpy"),
|
| 34 |
+
title="Plant Leaf Detection & Classification",
|
| 35 |
+
description="Upload a plant leaf image to detect diseases and classify leaves."
|
| 36 |
)
|
| 37 |
|
| 38 |
iface.launch()
|