leaf-counter / app.py
muskangoyal06's picture
Update app.py
e1976f4 verified
raw
history blame
968 Bytes
import gradio as gr
import torch
from torch.nn.modules.container import Sequential # import Sequential
# Allow safe globals for the Sequential class
torch.serialization.add_safe_globals([Sequential])
from ultralyticsplus import YOLO
from PIL import Image
# Load your custom YOLOv8 leaf detection model.
model = YOLO('foduucom/plant-leaf-detection-and-classification')
def count_leaves(image):
# Ensure the image is a PIL Image and convert to RGB
image = Image.open(image).convert("RGB")
# Run inference
results = model.predict(image)
# Count 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()