Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,29 @@
|
|
|
|
1 |
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 |
-
from torch.nn.modules.conv import Conv2d # Import Conv2d
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
model.overrides['conf'] = 0.25 # NMS confidence threshold
|
19 |
-
model.overrides['iou'] = 0.45 # NMS IoU threshold
|
20 |
-
model.overrides['agnostic_nms'] = False # NMS class-agnostic setting
|
21 |
-
model.overrides['max_det'] = 1000 # Maximum detections per image
|
22 |
-
|
23 |
-
def count_leaves(image):
|
24 |
-
# Convert the input to a PIL image (ensuring RGB)
|
25 |
-
image = Image.open(image).convert("RGB")
|
26 |
-
|
27 |
-
# Perform inference with the model
|
28 |
-
results = model.predict(image)
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
|
33 |
-
return f"
|
34 |
|
35 |
-
# Build a Gradio
|
36 |
iface = gr.Interface(
|
37 |
-
fn=
|
38 |
-
inputs=gr.Image(type="filepath"),
|
39 |
outputs="text",
|
40 |
-
title="Leaf
|
41 |
-
description="Upload an image
|
42 |
)
|
43 |
|
44 |
if __name__ == "__main__":
|
|
|
1 |
+
from ultralytics import YOLOvv8
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load the pretrained model directly using YOLOvv8.from_pretrained
|
5 |
+
model = YOLOvv8.from_pretrained("foduucom/plant-leaf-detection-and-classification")
|
6 |
|
7 |
+
def predict_leaves(image_path):
|
8 |
+
"""
|
9 |
+
Given an image file path, run prediction on it using the YOLOvv8 model.
|
10 |
+
The model will automatically save the result if configured.
|
11 |
+
"""
|
12 |
+
# Run the prediction; you can pass additional kwargs as needed (like save=True)
|
13 |
+
results = model.predict(source=image_path, save=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Optionally, count the detected leaves using the first result
|
16 |
+
count = len(results[0].boxes)
|
17 |
|
18 |
+
return f"Detected leaves: {count}"
|
19 |
|
20 |
+
# Build a Gradio Interface for the leaf detection app
|
21 |
iface = gr.Interface(
|
22 |
+
fn=predict_leaves,
|
23 |
+
inputs=gr.Image(type="filepath"), # Users can upload an image file
|
24 |
outputs="text",
|
25 |
+
title="Leaf Detection & Classification",
|
26 |
+
description="Upload an image to detect and count leaves using the YOLOvv8 model."
|
27 |
)
|
28 |
|
29 |
if __name__ == "__main__":
|