Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
4 |
-
import torch
|
5 |
import time
|
6 |
|
7 |
-
#
|
8 |
-
|
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 |
-
#
|
15 |
-
model
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
model.
|
20 |
-
|
21 |
-
model.overrides['max_det'] = 1000
|
22 |
|
23 |
def detect_leaves(image):
|
24 |
start_time = time.time()
|
25 |
|
26 |
-
# Convert image
|
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 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
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 |
-
|
48 |
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
|
49 |
|
50 |
-
# Create
|
51 |
interface = gr.Interface(
|
52 |
fn=detect_leaves,
|
53 |
-
inputs=gr.Image(label="
|
54 |
outputs=[
|
55 |
-
gr.Image(label="
|
56 |
-
gr.Number(label="
|
57 |
],
|
58 |
-
title="π
|
59 |
allow_flagging="never"
|
60 |
)
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
interface.launch(
|
64 |
server_port=7860,
|
65 |
-
|
66 |
-
enable_queue=True
|
67 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from ultralyticsplus import YOLO, render_result
|
3 |
import cv2
|
|
|
4 |
import time
|
5 |
|
6 |
+
# Load model with automatic device detection
|
7 |
+
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Optimize model configuration
|
10 |
+
model.overrides.update({
|
11 |
+
'conf': 0.25,
|
12 |
+
'iou': 0.45,
|
13 |
+
'imgsz': 640,
|
14 |
+
'device': '0' if model.device.type != 'cpu' else 'cpu'
|
15 |
+
})
|
|
|
16 |
|
17 |
def detect_leaves(image):
|
18 |
start_time = time.time()
|
19 |
|
20 |
+
# Convert image format
|
|
|
21 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
22 |
|
23 |
+
# Predict with optimized settings
|
24 |
+
results = model.predict(
|
25 |
+
source=img,
|
26 |
+
verbose=False, # Disable unnecessary logging
|
27 |
+
stream=False # Disable streaming mode
|
28 |
+
)
|
29 |
|
30 |
# Process results
|
31 |
num_leaves = len(results[0].boxes)
|
|
|
|
|
|
|
|
|
|
|
32 |
rendered_img = render_result(model=model, image=img, result=results[0])
|
|
|
33 |
|
34 |
+
print(f"Total processing time: {time.time() - start_time:.2f}s")
|
35 |
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
|
36 |
|
37 |
+
# Create lightweight interface
|
38 |
interface = gr.Interface(
|
39 |
fn=detect_leaves,
|
40 |
+
inputs=gr.Image(label="Plant Image"),
|
41 |
outputs=[
|
42 |
+
gr.Image(label="Detection Result", width=600),
|
43 |
+
gr.Number(label="Leaves Count")
|
44 |
],
|
45 |
+
title="π Leaf Detection",
|
46 |
allow_flagging="never"
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
50 |
interface.launch(
|
51 |
server_port=7860,
|
52 |
+
show_error=True,
|
53 |
+
enable_queue=True
|
54 |
)
|