leaf-counter / app.py
muskangoyal06's picture
Update app.py
4557350 verified
raw
history blame
2.12 kB
import gradio as gr
from ultralyticsplus import YOLO, render_result
import cv2
import torch
import time
# Check GPU availability
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device count: {torch.cuda.device_count()}")
if torch.cuda.is_available():
print(f"Current device: {torch.cuda.current_device()}")
print(f"Device name: {torch.cuda.get_device_name(0)}")
# Load model with GPU acceleration
model = YOLO('foduucom/plant-leaf-detection-and-classification').to('cuda' if torch.cuda.is_available() else 'cpu')
# Model configuration
model.overrides['conf'] = 0.25
model.overrides['iou'] = 0.45
model.overrides['agnostic_nms'] = False
model.overrides['max_det'] = 1000
def detect_leaves(image):
start_time = time.time()
# Convert image and check processing time
print("Converting image format...")
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
print(f"Conversion time: {time.time() - start_time:.2f}s")
# Perform prediction with timing
print("Starting prediction...")
pred_start = time.time()
results = model.predict(img, imgsz=640) # Fixed inference size
print(f"Prediction time: {time.time() - pred_start:.2f}s")
# Process results
num_leaves = len(results[0].boxes)
print(f"Detected {num_leaves} leaves")
# Render results
print("Rendering results...")
render_start = time.time()
rendered_img = render_result(model=model, image=img, result=results[0])
print(f"Rendering time: {time.time() - render_start:.2f}s")
# Convert back to RGB
return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
# Create Gradio interface with queue
interface = gr.Interface(
fn=detect_leaves,
inputs=gr.Image(label="Upload Plant Image"),
outputs=[
gr.Image(label="Detected Leaves"),
gr.Number(label="Number of Leaves Found")
],
title="πŸƒ Plant Leaf Detection",
allow_flagging="never"
)
if __name__ == "__main__":
interface.launch(
server_port=7860,
share=False,
enable_queue=True # Essential for heavy computations
)