leaf-counter / app.py
muskangoyal06's picture
Update app.py
169781f verified
raw
history blame
1.31 kB
import gradio as gr
from ultralyticsplus import YOLO, render_result
import cv2
import time
# Load model
model = YOLO('foduucom/plant-leaf-detection-and-classification')
# Model configuration
model.overrides.update({
'conf': 0.25,
'iou': 0.45,
'imgsz': 640,
'device': '0' if next(model.model.parameters()).is_cuda else 'cpu'
})
def detect_leaves(image):
start_time = time.time()
# Convert image format
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Predict
results = model.predict(img, verbose=False)
# Process results
num_leaves = len(results[0].boxes)
rendered_img = render_result(model=model, image=img, result=results[0])
print(f"Processing time: {time.time() - start_time:.2f}s")
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
# Create interface with queue support
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",
flagging_mode="never" # Updated from allow_flagging
)
if __name__ == "__main__":
interface.launch(
server_port=7860,
share=False,
# Removed enable_queue parameter
)