Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
import torch
|
2 |
from ultralytics.nn.tasks import DetectionModel
|
|
|
3 |
|
4 |
-
#
|
5 |
-
torch.serialization.add_safe_globals([DetectionModel])
|
6 |
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from ultralytics import YOLO
|
9 |
import gradio as gr
|
|
|
10 |
|
11 |
# Download the weights file (e.g., "best.pt") from the Hugging Face Hub
|
12 |
weights_path = hf_hub_download(
|
@@ -18,9 +20,9 @@ weights_path = hf_hub_download(
|
|
18 |
model = YOLO(weights_path)
|
19 |
|
20 |
def predict_leaves(image_path):
|
21 |
-
# Run
|
22 |
results = model.predict(source=image_path, save=True)
|
23 |
-
# Count the number of detected leaves
|
24 |
count = len(results[0].boxes)
|
25 |
return f"Detected leaves: {count}"
|
26 |
|
|
|
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 do this if you trust the model checkpoint)
|
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 (e.g., "best.pt") from the Hugging Face Hub
|
14 |
weights_path = hf_hub_download(
|
|
|
20 |
model = YOLO(weights_path)
|
21 |
|
22 |
def predict_leaves(image_path):
|
23 |
+
# Run prediction on the given image
|
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 |
|