File size: 1,429 Bytes
4a0cd82
7f97dd6
 
4557350
2dd2d70
f305096
 
e3e70fd
f305096
 
 
 
 
 
 
e1976f4
7f97dd6
4557350
 
f305096
d804613
7f97dd6
f305096
 
 
 
 
 
7f97dd6
4557350
7f97dd6
d804613
7f97dd6
f305096
d804613
4a0cd82
f305096
7c5f722
 
f305096
7c5f722
f305096
 
7c5f722
f305096
4557350
7c5f722
4a0cd82
 
4557350
 
f305096
 
4557350
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from ultralyticsplus import YOLO, render_result
import cv2
import time

# Load model with automatic device detection
model = YOLO('foduucom/plant-leaf-detection-and-classification')

# Optimize model configuration
model.overrides.update({
    'conf': 0.25,
    'iou': 0.45,
    'imgsz': 640,
    'device': '0' if model.device.type != 'cpu' else 'cpu'
})

def detect_leaves(image):
    start_time = time.time()
    
    # Convert image format
    img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # Predict with optimized settings
    results = model.predict(
        source=img,
        verbose=False,  # Disable unnecessary logging
        stream=False    # Disable streaming mode
    )
    
    # Process results
    num_leaves = len(results[0].boxes)
    rendered_img = render_result(model=model, image=img, result=results[0])
    
    print(f"Total processing time: {time.time() - start_time:.2f}s")
    return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves

# Create lightweight interface
interface = gr.Interface(
    fn=detect_leaves,
    inputs=gr.Image(label="Plant Image"),
    outputs=[
        gr.Image(label="Detection Result", width=600),
        gr.Number(label="Leaves Count")
    ],
    title="πŸƒ Leaf Detection",
    allow_flagging="never"
)

if __name__ == "__main__":
    interface.launch(
        server_port=7860,
        show_error=True,
        enable_queue=True
    )