|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
from collections import Counter |
|
from ultralytics import YOLO |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
MODEL_PATH = hf_hub_download( |
|
repo_id="ibrahim313/Bioengineering_Query_Tool_image_based", |
|
filename="best.pt" |
|
) |
|
|
|
|
|
model = YOLO(MODEL_PATH) |
|
|
|
def predict(image): |
|
|
|
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 [] |
|
class_names = [model.names[int(cls)] for cls in detections[:, 5]] if len(detections) > 0 else [] |
|
count = Counter(class_names) |
|
|
|
|
|
detection_str = ', '.join([f"{name}: {count}" for name, count in count.items()]) if class_names else "No detections" |
|
|
|
return annotated_img, detection_str |
|
|
|
app = gr.Interface( |
|
predict, |
|
inputs=gr.Image(type="numpy", label="Upload an Image"), |
|
outputs=[gr.Image(type="numpy", label="Annotated Image"), gr.Textbox(label="Detection Counts")], |
|
title="Blood Cell Count", |
|
description="Upload an image and YOLOv10 will detect blood cells." |
|
) |
|
|
|
app.launch() |
|
|