File size: 2,233 Bytes
f765816
8312ddd
 
0d5b33e
f765816
 
 
 
0d5b33e
 
 
 
 
7702060
0d5b33e
 
f765816
0d5b33e
 
 
f765816
 
0d5b33e
f765816
 
0d5b33e
f765816
 
 
 
0d5b33e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f765816
0d5b33e
f765816
0d5b33e
f765816
0d5b33e
 
 
 
 
 
 
1cd6596
 
0d5b33e
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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

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

# Load the model
model = YOLO("best.pt")

def process_image(image):
    """Detect cells in the image, extract attributes, and return results."""
    # Convert image to RGB
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Perform detection
    results = model.predict(source=image_rgb, imgsz=640, conf=0.25)
    
    # Get annotated image
    annotated_img = results[0].plot()
    
    # Extract detection data
    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])
        
        # Extract cell attributes (position, size, etc.)
        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

# Create Gradio interface
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()