Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,42 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from ultralytics.nn.tasks import DetectionModel
|
4 |
from torch.nn.modules.container import Sequential
|
5 |
-
from ultralytics.nn.modules import Conv
|
6 |
|
7 |
-
# Whitelist
|
8 |
torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv])
|
9 |
|
10 |
-
from ultralyticsplus import YOLO
|
11 |
from PIL import Image
|
12 |
|
13 |
-
# Load
|
14 |
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def count_leaves(image):
|
17 |
-
# Convert
|
18 |
image = Image.open(image).convert("RGB")
|
19 |
-
|
|
|
20 |
results = model.predict(image)
|
21 |
-
|
|
|
22 |
num_leaves = len(results[0].boxes)
|
|
|
23 |
return f"Number of leaves detected: {num_leaves}"
|
24 |
|
25 |
-
# Gradio
|
26 |
iface = gr.Interface(
|
27 |
fn=count_leaves,
|
28 |
inputs=gr.Image(type="filepath"),
|
29 |
outputs="text",
|
30 |
title="Leaf Counter",
|
31 |
-
description="Upload an image of a plant
|
32 |
)
|
33 |
|
34 |
if __name__ == "__main__":
|
|
|
2 |
import torch
|
3 |
from ultralytics.nn.tasks import DetectionModel
|
4 |
from torch.nn.modules.container import Sequential
|
5 |
+
from ultralytics.nn.modules import Conv
|
6 |
|
7 |
+
# Whitelist safe globals (only do this if you trust the source of the model)
|
8 |
torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv])
|
9 |
|
10 |
+
from ultralyticsplus import YOLO, render_result
|
11 |
from PIL import Image
|
12 |
|
13 |
+
# Load the YOLOv8s Leaf Detection and Classification model from Hugging Face
|
14 |
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
15 |
|
16 |
+
# Set recommended model parameters
|
17 |
+
model.overrides['conf'] = 0.25 # NMS confidence threshold
|
18 |
+
model.overrides['iou'] = 0.45 # NMS IoU threshold
|
19 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic setting
|
20 |
+
model.overrides['max_det'] = 1000 # Maximum detections per image
|
21 |
+
|
22 |
def count_leaves(image):
|
23 |
+
# Convert the input to a PIL image (ensuring RGB)
|
24 |
image = Image.open(image).convert("RGB")
|
25 |
+
|
26 |
+
# Perform inference with the model
|
27 |
results = model.predict(image)
|
28 |
+
|
29 |
+
# Count the detected leaves using the bounding boxes from the first result
|
30 |
num_leaves = len(results[0].boxes)
|
31 |
+
|
32 |
return f"Number of leaves detected: {num_leaves}"
|
33 |
|
34 |
+
# Build a Gradio interface for the leaf counter
|
35 |
iface = gr.Interface(
|
36 |
fn=count_leaves,
|
37 |
inputs=gr.Image(type="filepath"),
|
38 |
outputs="text",
|
39 |
title="Leaf Counter",
|
40 |
+
description="Upload an image of a plant and the model will detect and count the number of leaves."
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|