Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,29 @@
|
|
1 |
import torch
|
2 |
from ultralytics.nn.tasks import DetectionModel
|
3 |
from torch.nn.modules.container import Sequential
|
|
|
4 |
|
5 |
-
# Whitelist safe globals for unpickling (only
|
6 |
-
torch.serialization.add_safe_globals([DetectionModel, Sequential])
|
7 |
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
from ultralytics import YOLO
|
10 |
import gradio as gr
|
11 |
from PIL import Image
|
12 |
|
13 |
-
# Download the weights file (
|
14 |
weights_path = hf_hub_download(
|
15 |
repo_id="foduucom/plant-leaf-detection-and-classification",
|
16 |
filename="best.pt"
|
17 |
)
|
18 |
|
19 |
-
# Load the
|
20 |
model = YOLO(weights_path)
|
21 |
|
22 |
def predict_leaves(image_path):
|
23 |
-
# Run prediction on the
|
24 |
results = model.predict(source=image_path, save=True)
|
25 |
-
# Count the number of detected leaves using the first result
|
26 |
count = len(results[0].boxes)
|
27 |
return f"Detected leaves: {count}"
|
28 |
|
|
|
1 |
import torch
|
2 |
from ultralytics.nn.tasks import DetectionModel
|
3 |
from torch.nn.modules.container import Sequential
|
4 |
+
from ultralytics.nn.modules import Conv # Import Conv
|
5 |
|
6 |
+
# Whitelist safe globals for unpickling (only if you trust the model checkpoint!)
|
7 |
+
torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv])
|
8 |
|
9 |
from huggingface_hub import hf_hub_download
|
10 |
from ultralytics import YOLO
|
11 |
import gradio as gr
|
12 |
from PIL import Image
|
13 |
|
14 |
+
# Download the weights file (ensure the filename is correct)
|
15 |
weights_path = hf_hub_download(
|
16 |
repo_id="foduucom/plant-leaf-detection-and-classification",
|
17 |
filename="best.pt"
|
18 |
)
|
19 |
|
20 |
+
# Load the model using the local weights file
|
21 |
model = YOLO(weights_path)
|
22 |
|
23 |
def predict_leaves(image_path):
|
24 |
+
# Run prediction on the provided image
|
25 |
results = model.predict(source=image_path, save=True)
|
26 |
+
# Count the number of detected leaves (using the first result)
|
27 |
count = len(results[0].boxes)
|
28 |
return f"Detected leaves: {count}"
|
29 |
|