File size: 1,067 Bytes
4a0cd82
a835c9c
 
 
 
4a0cd82
 
 
a835c9c
 
4a0cd82
 
 
a835c9c
4a0cd82
 
 
a835c9c
4a0cd82
 
 
 
 
 
a835c9c
4a0cd82
 
 
 
 
 
a835c9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
import torch
from ultralytics.nn.tasks import DetectionModel
# Allow safe globals for custom model loading (do this only if you trust the source)
torch.serialization.add_safe_globals([DetectionModel])
from ultralyticsplus import YOLO
from PIL import Image

# Load your custom YOLOv8 leaf detection model.
# If this still causes issues, try using a supported model like 'yolov8n.pt'
model = YOLO('foduucom/plant-leaf-detection-and-classification')

def count_leaves(image):
    # Convert to a PIL Image (if not already)
    image = Image.open(image).convert("RGB")
    # Run inference
    results = model.predict(image)
    # Count the number of detected leaves
    num_leaves = len(results[0].boxes)
    return f"Number of leaves detected: {num_leaves}"

# Gradio UI
iface = gr.Interface(
    fn=count_leaves,
    inputs=gr.Image(type="filepath"),
    outputs="text",
    title="Leaf Counter",
    description="Upload an image of a plant, and the model will detect and count the number of leaves."
)

if __name__ == "__main__":
    iface.launch()