File size: 1,291 Bytes
445566f
 
ff587c5
f87cf07
445566f
f87cf07
 
445566f
e3e70fd
5a596d2
4a0cd82
ff587c5
2dd2d70
f87cf07
e3e70fd
445566f
e3e70fd
 
 
f87cf07
e3e70fd
e1976f4
0770f2c
f87cf07
0770f2c
f87cf07
0770f2c
 
4a0cd82
445566f
4a0cd82
0770f2c
e3e70fd
4a0cd82
0770f2c
e3e70fd
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
34
35
36
37
38
39
40
41
import torch
from ultralytics.nn.tasks import DetectionModel
from torch.nn.modules.container import Sequential
from ultralytics.nn.modules import Conv  # Import Conv

# Whitelist safe globals for unpickling (only if you trust the model checkpoint!)
torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv])

from huggingface_hub import hf_hub_download
from ultralytics import YOLO
import gradio as gr
from PIL import Image

# Download the weights file (ensure the filename is correct)
weights_path = hf_hub_download(
    repo_id="foduucom/plant-leaf-detection-and-classification",
    filename="best.pt"
)

# Load the model using the local weights file
model = YOLO(weights_path)

def predict_leaves(image_path):
    # Run prediction on the provided image
    results = model.predict(source=image_path, save=True)
    # Count the number of detected leaves (using the first result)
    count = len(results[0].boxes)
    return f"Detected leaves: {count}"

# Create a Gradio interface for your app
iface = gr.Interface(
    fn=predict_leaves,
    inputs=gr.Image(type="filepath"),
    outputs="text",
    title="Leaf Detection & Classification",
    description="Upload an image to detect and count leaves using the YOLO model."
)

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