Spaces:
Running
Running
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 | |
) |