|
import gradio as gr |
|
from ultralytics import YOLO |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
model = YOLO("best.pt") |
|
|
|
|
|
def detect_species(files): |
|
""" |
|
Detect species in uploaded images using the YOLO model. |
|
|
|
Args: |
|
files (list): List of uploaded image files. |
|
|
|
Returns: |
|
list: Annotated images or error messages for each file. |
|
""" |
|
annotated_images = [] |
|
for file in files: |
|
try: |
|
|
|
image = Image.open(file) |
|
image = np.array(image) |
|
results = model(image) |
|
annotated_image = results[0].plot() |
|
annotated_images.append(annotated_image) |
|
except Exception as e: |
|
|
|
error_message = f"Error processing file {file.name}: {str(e)}" |
|
print(error_message) |
|
annotated_images.append(np.zeros((100, 100, 3))) |
|
return annotated_images |
|
|
|
|
|
app = gr.Interface( |
|
fn=detect_species, |
|
inputs=gr.Files(file_types=["image"], label="Upload Images"), |
|
outputs=gr.Gallery(label="Detection Results"), |
|
title="Finnish Meadow Plants Detection", |
|
description="Upload one or more leaf images, and the model will detect the species.", |
|
examples=["example1.jpg", "example2.jpg"], |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch(share=True) |
|
|