Spaces:
Sleeping
Sleeping
File size: 1,491 Bytes
d225b91 78f674c d225b91 78f674c d225b91 78f674c d225b91 |
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 |
import gradio as gr
from ultralytics import YOLO
import os
import glob
import shutil
# Load the fine-tuned YOLOv10 model
model = YOLO('best.pt')
# Inference function for Gradio
def detect_objects(image):
# Define the results directory
results_dir = "runs/detect"
# Clear previous detection results to avoid caching issues
if os.path.exists(results_dir):
shutil.rmtree(results_dir) # Remove the entire 'runs/detect' folder
# Run YOLO prediction and save results
results = model.predict(source=image, save=True, save_txt=True, conf=0.5)
# Get the latest result folder
latest_result_folder = sorted(glob.glob(f"{results_dir}/*"), key=os.path.getmtime, reverse=True)[0]
# Find the latest detected image inside the folder
predicted_images = sorted(glob.glob(f"{latest_result_folder}/*.jpg") + glob.glob(f"{latest_result_folder}/*.png"), key=os.path.getmtime, reverse=True)
if predicted_images:
return predicted_images[0] # Return the latest output image
else:
return "Error: No output image found!"
# Create the Gradio interface
app = gr.Interface(
fn=detect_objects,
inputs=gr.Image(type="filepath", label="π€ Upload Image"),
outputs=gr.Image(type="filepath", label="β
Detected Image"),
title="π¬ YOLOv10 Blood Cell Detection App",
description="Upload an image to detect blood cells (RBC, WBC, Platelets) using the fine-tuned YOLOv10 model."
)
# Launch the app
app.launch()
|