Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,32 @@
|
|
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
|
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"),
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from ultralytics.nn.tasks import DetectionModel
|
4 |
+
# Allow safe globals for custom model loading (do this only if you trust the source)
|
5 |
+
torch.serialization.add_safe_globals([DetectionModel])
|
6 |
from ultralyticsplus import YOLO
|
7 |
from PIL import Image
|
8 |
|
9 |
+
# Load your custom YOLOv8 leaf detection model.
|
10 |
+
# If this still causes issues, try using a supported model like 'yolov8n.pt'
|
11 |
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
12 |
|
13 |
def count_leaves(image):
|
14 |
+
# Convert to a PIL Image (if not already)
|
15 |
image = Image.open(image).convert("RGB")
|
|
|
16 |
# Run inference
|
17 |
results = model.predict(image)
|
18 |
+
# Count the number of detected leaves
|
|
|
19 |
num_leaves = len(results[0].boxes)
|
|
|
20 |
return f"Number of leaves detected: {num_leaves}"
|
21 |
|
22 |
# Gradio UI
|
23 |
iface = gr.Interface(
|
24 |
fn=count_leaves,
|
25 |
+
inputs=gr.Image(type="filepath"),
|
26 |
outputs="text",
|
27 |
title="Leaf Counter",
|
28 |
description="Upload an image of a plant, and the model will detect and count the number of leaves."
|
29 |
)
|
30 |
|
|
|
31 |
if __name__ == "__main__":
|
32 |
+
iface.launch()
|