Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,30 @@
|
|
|
|
1 |
from ultralytics import YOLO
|
2 |
import gradio as gr
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def predict_leaves(image_path):
|
8 |
-
|
9 |
-
Given an image file path, run prediction on it using the YOLOvv8 model.
|
10 |
-
The model will automatically save the result if configured.
|
11 |
-
"""
|
12 |
-
# Run the prediction; you can pass additional kwargs as needed (like save=True)
|
13 |
results = model.predict(source=image_path, save=True)
|
14 |
-
|
15 |
-
# Optionally, count the detected leaves using the first result
|
16 |
count = len(results[0].boxes)
|
17 |
-
|
18 |
return f"Detected leaves: {count}"
|
19 |
|
20 |
-
#
|
21 |
iface = gr.Interface(
|
22 |
fn=predict_leaves,
|
23 |
-
inputs=gr.Image(type="filepath"),
|
24 |
outputs="text",
|
25 |
title="Leaf Detection & Classification",
|
26 |
-
description="Upload an image to detect and count leaves using the
|
27 |
)
|
28 |
|
29 |
if __name__ == "__main__":
|
|
|
1 |
+
from huggingface_hub import hf_hub_download
|
2 |
from ultralytics import YOLO
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Download the weights file (e.g., "best.pt") from the Hugging Face Hub
|
6 |
+
weights_path = hf_hub_download(
|
7 |
+
repo_id="foduucom/plant-leaf-detection-and-classification",
|
8 |
+
filename="best.pt"
|
9 |
+
)
|
10 |
+
|
11 |
+
# Load the model using the local weights file
|
12 |
+
model = YOLO(weights_path)
|
13 |
|
14 |
def predict_leaves(image_path):
|
15 |
+
# Run the model prediction on the provided image path
|
|
|
|
|
|
|
|
|
16 |
results = model.predict(source=image_path, save=True)
|
17 |
+
# Count the number of detected leaves (using the first result)
|
|
|
18 |
count = len(results[0].boxes)
|
|
|
19 |
return f"Detected leaves: {count}"
|
20 |
|
21 |
+
# Create a Gradio interface
|
22 |
iface = gr.Interface(
|
23 |
fn=predict_leaves,
|
24 |
+
inputs=gr.Image(type="filepath"),
|
25 |
outputs="text",
|
26 |
title="Leaf Detection & Classification",
|
27 |
+
description="Upload an image to detect and count leaves using the YOLO model."
|
28 |
)
|
29 |
|
30 |
if __name__ == "__main__":
|