Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,51 @@
|
|
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
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
repo_id="foduucom/plant-leaf-detection-and-classification",
|
17 |
-
filename="best.pt"
|
18 |
-
)
|
19 |
|
20 |
-
#
|
21 |
-
model =
|
|
|
|
|
|
|
22 |
|
23 |
-
def
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
# Create
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from ultralyticsplus import YOLO, render_result
|
3 |
+
import cv2
|
4 |
|
5 |
+
# Load the YOLO model
|
6 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
|
|
|
|
|
|
7 |
|
8 |
+
# Set model parameters
|
9 |
+
model.overrides['conf'] = 0.25 # NMS confidence threshold
|
10 |
+
model.overrides['iou'] = 0.45 # NMS IoU threshold
|
11 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
12 |
+
model.overrides['max_det'] = 1000 # maximum detections per image
|
13 |
|
14 |
+
def detect_leaves(image):
|
15 |
+
# Convert from Gradio's numpy array to image file
|
16 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
17 |
+
cv2.imwrite('temp_image.jpg', image)
|
18 |
+
|
19 |
+
# Perform prediction
|
20 |
+
results = model.predict('temp_image.jpg')
|
21 |
+
|
22 |
+
# Count number of leaves detected
|
23 |
+
num_leaves = len(results[0].boxes)
|
24 |
+
|
25 |
+
# Render results on image
|
26 |
+
render = render_result(model=model, image='temp_image.jpg', result=results[0])
|
27 |
+
|
28 |
+
return render, num_leaves
|
29 |
|
30 |
+
# Create Gradio interface
|
31 |
+
with gr.Blocks(title="Leaf Detection & Classification") as demo:
|
32 |
+
gr.Markdown("# 🍃 Plant Leaf Detection & Classification")
|
33 |
+
gr.Markdown("Upload a plant image to detect and count leaves")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
with gr.Column():
|
37 |
+
input_image = gr.Image(label="Upload Plant Image")
|
38 |
+
submit_btn = gr.Button("Detect Leaves")
|
39 |
+
|
40 |
+
with gr.Column():
|
41 |
+
output_image = gr.Image(label="Detection Results")
|
42 |
+
leaf_count = gr.Number(label="Number of Leaves Detected")
|
43 |
+
|
44 |
+
submit_btn.click(
|
45 |
+
fn=detect_leaves,
|
46 |
+
inputs=[input_image],
|
47 |
+
outputs=[output_image, leaf_count]
|
48 |
+
)
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
+
demo.launch(server_port=7860, share=False)
|