import streamlit as st
import numpy as np
from PIL import Image
from ultralytics import YOLO
import time
# Set page title and layout
st.set_page_config(page_title="Blood Component Detection", page_icon="🩸", layout="wide")
# Styling the app
st.markdown("""
""", unsafe_allow_html=True)
# Set title and subtitle
st.markdown('
Blood Component Detection
', unsafe_allow_html=True)
st.markdown('Detect and classify blood cells in uploaded images
', unsafe_allow_html=True)
# Load the YOLO model
model = YOLO('blood_detection_model.pt')
# Image uploader with custom style
with st.container():
st.markdown('', unsafe_allow_html=True)
uploaded_image = st.file_uploader("Upload Image (JPG, JPEG, PNG)", type=['jpg', 'jpeg', 'png'])
st.markdown('
', unsafe_allow_html=True)
if uploaded_image is not None:
# Display the uploaded image
image = Image.open(uploaded_image)
image = image.convert("RGB")
st.image(image, caption="Uploaded Image", use_container_width=True)
# Prediction button
with st.container():
if st.button('Classify Image', key="predict", help="Click to start classification", use_container_width=True):
st.write("Classifying... Please wait.")
# Display a progress bar while the model processes
progress_bar = st.progress(0)
for i in range(100):
time.sleep(0.01)
progress_bar.progress(i+1)
# Convert the image for prediction
### img_array = np.array(image)
# Perform prediction with a low confidence threshold
results = model.predict(source=image, imgsz=640, conf=0.1)
# Display results
for result in results:
print(result.boxes.xyxy, result.boxes.cls, result.boxes.conf)
# Print bounding boxes, class IDs, and confidence
# Show results
for i in range (1, 25):
if len(result[0].boxes) > 0:
# Extract detection results
st.write("Bounding Boxes:", result[0].boxes.xyxy)
st.write("Class IDs:", result[0].boxes.cls)
st.write("Confidence Scores:", result[0].boxes.conf)
# Plot the results (image with bounding boxes)
output_image = result[i].plot() # This should plot bounding boxes
st.image(output_image, caption="Predicted Image with Bounding Boxes", use_container_width=True)
else:
st.write("No objects detected.")
st.write("Raw Prediction Results:", results)
st.write("Boxes:", results[0].boxes.xyxy)
st.write("Classes:", results[0].boxes.cls)
st.write("Confidences:", results[0].boxes.conf)