Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
model
|
12 |
-
model.overrides['
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def detect_leaves(image):
|
15 |
-
# Convert
|
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 |
-
#
|
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
|
32 |
-
gr.Markdown("
|
33 |
-
gr.Markdown("Upload a plant image to
|
34 |
|
35 |
with gr.Row():
|
36 |
-
|
37 |
-
|
38 |
-
|
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],
|
@@ -48,4 +53,4 @@ with gr.Blocks(title="Leaf Detection & Classification") as demo:
|
|
48 |
)
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
demo.launch(server_port=7860,
|
|
|
1 |
import gradio as gr
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
+
import torch
|
5 |
|
6 |
+
# Verify torch version
|
7 |
+
print(f"Using torch version: {torch.__version__}")
|
8 |
|
9 |
+
# Load model with compatibility fix
|
10 |
+
def load_model():
|
11 |
+
try:
|
12 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
13 |
+
model.overrides['conf'] = 0.25
|
14 |
+
model.overrides['iou'] = 0.45
|
15 |
+
model.overrides['agnostic_nms'] = False
|
16 |
+
model.overrides['max_det'] = 1000
|
17 |
+
return model
|
18 |
+
except Exception as e:
|
19 |
+
raise RuntimeError("Error loading model. Please check the requirements versions.") from e
|
20 |
+
|
21 |
+
model = load_model()
|
22 |
|
23 |
def detect_leaves(image):
|
24 |
+
# Convert image format
|
25 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
26 |
cv2.imwrite('temp_image.jpg', image)
|
27 |
|
28 |
# Perform prediction
|
29 |
results = model.predict('temp_image.jpg')
|
30 |
|
31 |
+
# Process results
|
32 |
num_leaves = len(results[0].boxes)
|
|
|
|
|
33 |
render = render_result(model=model, image='temp_image.jpg', result=results[0])
|
34 |
|
35 |
return render, num_leaves
|
36 |
|
37 |
# Create Gradio interface
|
38 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Leaf Detection") as demo:
|
39 |
+
gr.Markdown("## π Plant Leaf Detection & Counter")
|
40 |
+
gr.Markdown("Upload a plant image to analyze leaf count and species")
|
41 |
|
42 |
with gr.Row():
|
43 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
44 |
+
output_image = gr.Image(label="Detected Leaves", interactive=False)
|
45 |
+
|
46 |
+
leaf_count = gr.Number(label="Total Leaves Detected", precision=0)
|
|
|
|
|
|
|
47 |
|
48 |
+
submit_btn = gr.Button("Analyze Image", variant="primary")
|
49 |
submit_btn.click(
|
50 |
fn=detect_leaves,
|
51 |
inputs=[input_image],
|
|
|
53 |
)
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
+
demo.launch(server_port=7860, show_error=True)
|