File size: 1,433 Bytes
f765816
8312ddd
 
f765816
 
 
 
 
 
 
 
 
7702060
f765816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd6596
 
f765816
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
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

# Download model from Hugging Face repo
MODEL_PATH = hf_hub_download(
    repo_id="ibrahim313/Bioengineering_Query_Tool_image_based",
    filename="best.pt"
)

# Load the YOLOv10 model
model = YOLO(MODEL_PATH)

def predict(image):
    # Convert the image from BGR to RGB
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Perform prediction
    results = model.predict(source=image_rgb, imgsz=640, conf=0.25)
    
    # Get the annotated image
    annotated_img = results[0].plot()
    
    # Extract detection data
    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)

    # Create a string representation of the detections
    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()