Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,38 +2,52 @@ import gradio as gr
|
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
import torch
|
5 |
-
import
|
6 |
-
import ultralyticsplus
|
7 |
|
8 |
-
# Check
|
9 |
-
print(f"
|
10 |
-
print(f"
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
# Load model
|
14 |
-
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
15 |
|
16 |
# Model configuration
|
17 |
-
model.overrides['conf'] = 0.25
|
18 |
-
model.overrides['iou'] = 0.45
|
19 |
model.overrides['agnostic_nms'] = False
|
20 |
model.overrides['max_det'] = 1000
|
21 |
|
22 |
def detect_leaves(image):
|
23 |
-
|
|
|
|
|
|
|
24 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
25 |
|
26 |
-
# Perform prediction
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
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
|
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"),
|
@@ -41,9 +55,13 @@ interface = gr.Interface(
|
|
41 |
gr.Image(label="Detected Leaves"),
|
42 |
gr.Number(label="Number of Leaves Found")
|
43 |
],
|
44 |
-
title="π Plant Leaf Detection
|
45 |
-
|
46 |
)
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
-
interface.launch(
|
|
|
|
|
|
|
|
|
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
import torch
|
5 |
+
import time
|
|
|
6 |
|
7 |
+
# Check GPU availability
|
8 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
9 |
+
print(f"CUDA device count: {torch.cuda.device_count()}")
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
print(f"Current device: {torch.cuda.current_device()}")
|
12 |
+
print(f"Device name: {torch.cuda.get_device_name(0)}")
|
13 |
|
14 |
+
# Load model with GPU acceleration
|
15 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification').to('cuda' if torch.cuda.is_available() else 'cpu')
|
16 |
|
17 |
# Model configuration
|
18 |
+
model.overrides['conf'] = 0.25
|
19 |
+
model.overrides['iou'] = 0.45
|
20 |
model.overrides['agnostic_nms'] = False
|
21 |
model.overrides['max_det'] = 1000
|
22 |
|
23 |
def detect_leaves(image):
|
24 |
+
start_time = time.time()
|
25 |
+
|
26 |
+
# Convert image and check processing time
|
27 |
+
print("Converting image format...")
|
28 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
29 |
+
print(f"Conversion time: {time.time() - start_time:.2f}s")
|
30 |
|
31 |
+
# Perform prediction with timing
|
32 |
+
print("Starting prediction...")
|
33 |
+
pred_start = time.time()
|
34 |
+
results = model.predict(img, imgsz=640) # Fixed inference size
|
35 |
+
print(f"Prediction time: {time.time() - pred_start:.2f}s")
|
36 |
|
37 |
+
# Process results
|
38 |
num_leaves = len(results[0].boxes)
|
39 |
+
print(f"Detected {num_leaves} leaves")
|
40 |
+
|
41 |
+
# Render results
|
42 |
+
print("Rendering results...")
|
43 |
+
render_start = time.time()
|
44 |
rendered_img = render_result(model=model, image=img, result=results[0])
|
45 |
+
print(f"Rendering time: {time.time() - render_start:.2f}s")
|
46 |
|
47 |
+
# Convert back to RGB
|
48 |
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
|
49 |
|
50 |
+
# Create Gradio interface with queue
|
51 |
interface = gr.Interface(
|
52 |
fn=detect_leaves,
|
53 |
inputs=gr.Image(label="Upload Plant Image"),
|
|
|
55 |
gr.Image(label="Detected Leaves"),
|
56 |
gr.Number(label="Number of Leaves Found")
|
57 |
],
|
58 |
+
title="π Plant Leaf Detection",
|
59 |
+
allow_flagging="never"
|
60 |
)
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
interface.launch(
|
64 |
+
server_port=7860,
|
65 |
+
share=False,
|
66 |
+
enable_queue=True # Essential for heavy computations
|
67 |
+
)
|