|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
import pandas as pd |
|
from collections import Counter |
|
from ultralytics import YOLO |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model = YOLO("best.pt") |
|
|
|
def process_image(image): |
|
"""Detect cells in the image, extract attributes, and return results.""" |
|
|
|
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
results = model.predict(source=image_rgb, imgsz=640, conf=0.25) |
|
|
|
|
|
annotated_img = results[0].plot() |
|
|
|
|
|
detections = results[0].boxes.data if results[0].boxes is not None else [] |
|
if len(detections) > 0: |
|
class_names = [model.names[int(cls)] for cls in detections[:, 5]] |
|
count = Counter(class_names) |
|
detection_str = ', '.join([f"{name}: {count[name]}" for name in count]) |
|
|
|
|
|
df = pd.DataFrame(detections.numpy(), columns=["x_min", "y_min", "x_max", "y_max", "confidence", "class"]) |
|
df["class_name"] = df["class"].apply(lambda x: model.names[int(x)]) |
|
df["width"] = df["x_max"] - df["x_min"] |
|
df["height"] = df["y_max"] - df["y_min"] |
|
df["area"] = df["width"] * df["height"] |
|
|
|
summary = df.groupby("class_name")["area"].describe().reset_index() |
|
else: |
|
detection_str = "No detections" |
|
summary = pd.DataFrame(columns=["class_name", "count", "mean", "std", "min", "25%", "50%", "75%", "max"]) |
|
|
|
return annotated_img, detection_str, summary |
|
|
|
|
|
app = gr.Interface( |
|
fn=process_image, |
|
inputs=gr.Image(type="numpy", label="Upload an Image"), |
|
outputs=[ |
|
gr.Image(type="numpy", label="Annotated Image"), |
|
gr.Textbox(label="Detection Counts"), |
|
gr.Dataframe(label="Cell Statistics") |
|
], |
|
title="Bioengineering Image Analysis Tool", |
|
description="Upload an image to detect and analyze bioengineering cells using YOLOv10." |
|
) |
|
|
|
app.launch() |