Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralyticsplus import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load YOLOv8 leaf detection model
|
| 6 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
| 7 |
+
|
| 8 |
+
def count_leaves(image):
|
| 9 |
+
# Convert to PIL Image if needed
|
| 10 |
+
image = Image.open(image).convert("RGB")
|
| 11 |
+
|
| 12 |
+
# Run inference
|
| 13 |
+
results = model.predict(image)
|
| 14 |
+
|
| 15 |
+
# Count number of detected leaves
|
| 16 |
+
num_leaves = len(results[0].boxes)
|
| 17 |
+
|
| 18 |
+
return f"Number of leaves detected: {num_leaves}"
|
| 19 |
+
|
| 20 |
+
# Gradio UI
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=count_leaves,
|
| 23 |
+
inputs=gr.Image(type="filepath"), # User uploads an image
|
| 24 |
+
outputs="text",
|
| 25 |
+
title="Leaf Counter",
|
| 26 |
+
description="Upload an image of a plant, and the model will detect and count the number of leaves."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Launch app
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
iface.launch()
|