|
import streamlit as st |
|
from ultralyticsplus import YOLO, render_result |
|
import PIL.Image as Image |
|
import numpy as np |
|
import pandas as pd |
|
import requests |
|
from io import BytesIO |
|
from fastai.vision.all import load_learner |
|
|
|
|
|
st.title("Blood Cell Detection with YOLOv8") |
|
|
|
|
|
model = YOLO('keremberke/yolov8m-blood-cell-detection') |
|
|
|
|
|
model.overrides['conf'] = 0.25 |
|
model.overrides['iou'] = 0.45 |
|
model.overrides['agnostic_nms'] = False |
|
model.overrides['max_det'] = 1000 |
|
|
|
|
|
fastai_model = load_learner('model1.pkl') |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image for detection", type=["jpg", "png"]) |
|
|
|
if uploaded_file: |
|
|
|
image = Image.open(uploaded_file) |
|
|
|
|
|
results = model.predict(np.array(image)) |
|
|
|
|
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
rendered_image = render_result(model=model, image=image, result=results[0]) |
|
|
|
|
|
st.image(rendered_image, caption="Detection Results", use_column_width=True) |
|
|
|
|
|
cell_counts = {"RBC": 0, "WBC": 0, "Platelets": 0} |
|
|
|
|
|
has_wbc = False |
|
|
|
st.write("Detection Results:") |
|
for box in results[0].boxes: |
|
class_index = int(box.cls) |
|
if class_index == 1: |
|
cell_counts["RBC"] += 1 |
|
elif class_index == 2: |
|
cell_counts["WBC"] += 1 |
|
has_wbc = True |
|
elif class_index == 0: |
|
cell_counts["Platelets"] += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write("Cell Type Counts:") |
|
st.write(pd.DataFrame.from_dict(cell_counts, orient='index', columns=['Count'])) |
|
|
|
if has_wbc: |
|
|
|
pred, idx, probs = fastai_model.predict(image) |
|
st.write("White Blood Cell Classification:") |
|
categories = ('EOSINOPHIL', 'LYMPHOCYTE', 'MONOCYTE', 'NEUTROPHIL') |
|
results_dict = dict(zip(categories, map(float, probs))) |
|
st.write(results_dict) |
|
else: |
|
st.write("Upload an image to start detection.") |
|
|