Spaces:
Running
Running
File size: 2,118 Bytes
4a0cd82 7f97dd6 3b7e08f 4557350 2dd2d70 4557350 e3e70fd 4557350 3b7e08f d804613 4557350 d804613 e1976f4 7f97dd6 4557350 d804613 4557350 7f97dd6 4557350 7f97dd6 4557350 7f97dd6 4557350 d804613 4557350 7f97dd6 4557350 d804613 4a0cd82 4557350 7c5f722 d804613 7c5f722 4557350 7c5f722 4a0cd82 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 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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
) |