Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,47 @@ import gradio as gr
|
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
# Check versions
|
7 |
print(f"Torch version: {torch.__version__}")
|
8 |
-
print(f"Ultralytics version: {
|
|
|
9 |
|
10 |
-
# Load model
|
11 |
-
|
12 |
-
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
13 |
-
model.overrides.update({
|
14 |
-
'conf': 0.25,
|
15 |
-
'iou': 0.45,
|
16 |
-
'agnostic_nms': False,
|
17 |
-
'max_det': 1000
|
18 |
-
})
|
19 |
-
return model
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def detect_leaves(image):
|
24 |
# Convert image format
|
25 |
-
|
26 |
-
cv2.imwrite('temp_image.jpg', image)
|
27 |
|
28 |
-
#
|
29 |
-
results = model.predict(
|
30 |
|
31 |
-
#
|
32 |
num_leaves = len(results[0].boxes)
|
33 |
-
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
-
# Create
|
38 |
interface = gr.Interface(
|
39 |
fn=detect_leaves,
|
40 |
inputs=gr.Image(label="Upload Plant Image"),
|
41 |
outputs=[
|
42 |
-
gr.Image(label="
|
43 |
-
gr.Number(label="Leaves
|
44 |
],
|
45 |
-
title="π Plant Leaf
|
46 |
-
description="Upload a plant
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
|
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
import torch
|
5 |
+
import ultralytics
|
6 |
+
import ultralyticsplus
|
7 |
|
8 |
# Check versions
|
9 |
print(f"Torch version: {torch.__version__}")
|
10 |
+
print(f"Ultralytics version: {ultralytics.__version__}")
|
11 |
+
print(f"UltralyticsPlus version: {ultralyticsplus.__version__}")
|
12 |
|
13 |
+
# Load model
|
14 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Model configuration
|
17 |
+
model.overrides['conf'] = 0.25 # Confidence threshold
|
18 |
+
model.overrides['iou'] = 0.45 # IoU threshold
|
19 |
+
model.overrides['agnostic_nms'] = False
|
20 |
+
model.overrides['max_det'] = 1000
|
21 |
|
22 |
def detect_leaves(image):
|
23 |
# Convert image format
|
24 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
25 |
|
26 |
+
# Perform prediction
|
27 |
+
results = model.predict(img)
|
28 |
|
29 |
+
# Get results
|
30 |
num_leaves = len(results[0].boxes)
|
31 |
+
rendered_img = render_result(model=model, image=img, result=results[0])
|
32 |
|
33 |
+
# Convert back to RGB for Gradio
|
34 |
+
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
|
35 |
|
36 |
+
# Create Gradio interface
|
37 |
interface = gr.Interface(
|
38 |
fn=detect_leaves,
|
39 |
inputs=gr.Image(label="Upload Plant Image"),
|
40 |
outputs=[
|
41 |
+
gr.Image(label="Detected Leaves"),
|
42 |
+
gr.Number(label="Number of Leaves Found")
|
43 |
],
|
44 |
+
title="π Plant Leaf Detection & Counting",
|
45 |
+
description="Upload an image of a plant to detect and count its leaves"
|
46 |
)
|
47 |
|
48 |
if __name__ == "__main__":
|