Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Set page title and layout
|
| 8 |
+
st.set_page_config(page_title="Blood Component Detection", page_icon="🩸", layout="wide")
|
| 9 |
+
|
| 10 |
+
# Styling the app
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.stApp {
|
| 14 |
+
background-color: #f4f6f9;
|
| 15 |
+
}
|
| 16 |
+
.title {
|
| 17 |
+
color: #000;
|
| 18 |
+
font-size: 40px;
|
| 19 |
+
font-weight: bold;
|
| 20 |
+
margin-top: 20px;
|
| 21 |
+
text-align: center;
|
| 22 |
+
}
|
| 23 |
+
.subtitle {
|
| 24 |
+
color: #555;
|
| 25 |
+
font-size: 20px;
|
| 26 |
+
text-align: center;
|
| 27 |
+
}
|
| 28 |
+
.upload-container {
|
| 29 |
+
background-color: #fff;
|
| 30 |
+
padding: 20px;
|
| 31 |
+
border-radius: 10px;
|
| 32 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
| 33 |
+
margin-bottom: 20px;
|
| 34 |
+
}
|
| 35 |
+
.predict-btn {
|
| 36 |
+
background-color: #4CAF50;
|
| 37 |
+
color: white;
|
| 38 |
+
padding: 10px 20px;
|
| 39 |
+
border-radius: 5px;
|
| 40 |
+
font-size: 18px;
|
| 41 |
+
width: 100%;
|
| 42 |
+
cursor: pointer;
|
| 43 |
+
}
|
| 44 |
+
.predict-btn:hover {
|
| 45 |
+
background-color: #45a049;
|
| 46 |
+
}
|
| 47 |
+
.stImage {
|
| 48 |
+
border-radius: 15px;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
""", unsafe_allow_html=True)
|
| 52 |
+
|
| 53 |
+
# Set title and subtitle
|
| 54 |
+
st.markdown('<div class="title">Blood Component Detection</div>', unsafe_allow_html=True)
|
| 55 |
+
st.markdown('<div class="subtitle">Detect and classify blood cells in uploaded images</div>', unsafe_allow_html=True)
|
| 56 |
+
|
| 57 |
+
# Load the YOLO model
|
| 58 |
+
model = YOLO('/Users/abhinavyadav/Downloads/final_best(blood_detection).pt')
|
| 59 |
+
|
| 60 |
+
# Image uploader with custom style
|
| 61 |
+
with st.container():
|
| 62 |
+
st.markdown('<div class="upload-container">', unsafe_allow_html=True)
|
| 63 |
+
uploaded_image = st.file_uploader("Upload Image (JPG, JPEG, PNG)", type=['jpg', 'jpeg', 'png'])
|
| 64 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
if uploaded_image is not None:
|
| 67 |
+
# Display the uploaded image
|
| 68 |
+
image = Image.open(uploaded_image)
|
| 69 |
+
image = image.convert("RGB")
|
| 70 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 71 |
+
|
| 72 |
+
# Prediction button
|
| 73 |
+
with st.container():
|
| 74 |
+
if st.button('Classify Image', key="predict", help="Click to start classification", use_container_width=True):
|
| 75 |
+
st.write("Classifying... Please wait.")
|
| 76 |
+
|
| 77 |
+
# Display a progress bar while the model processes
|
| 78 |
+
progress_bar = st.progress(0)
|
| 79 |
+
for i in range(100):
|
| 80 |
+
time.sleep(0.01)
|
| 81 |
+
progress_bar.progress(i+1)
|
| 82 |
+
|
| 83 |
+
# Convert the image for prediction
|
| 84 |
+
### img_array = np.array(image)
|
| 85 |
+
|
| 86 |
+
# Perform prediction with a low confidence threshold
|
| 87 |
+
results = model.predict(source=image, imgsz=640, conf=0.1)
|
| 88 |
+
|
| 89 |
+
# Display results
|
| 90 |
+
for result in results:
|
| 91 |
+
print(result.boxes.xyxy, result.boxes.cls, result.boxes.conf)
|
| 92 |
+
# Print bounding boxes, class IDs, and confidence
|
| 93 |
+
|
| 94 |
+
# Show results
|
| 95 |
+
for i in range (1, 25):
|
| 96 |
+
if len(result[0].boxes) > 0:
|
| 97 |
+
# Extract detection results
|
| 98 |
+
st.write("Bounding Boxes:", result[0].boxes.xyxy)
|
| 99 |
+
st.write("Class IDs:", result[0].boxes.cls)
|
| 100 |
+
st.write("Confidence Scores:", result[0].boxes.conf)
|
| 101 |
+
|
| 102 |
+
# Plot the results (image with bounding boxes)
|
| 103 |
+
output_image = result[i].plot() # This should plot bounding boxes
|
| 104 |
+
st.image(output_image, caption="Predicted Image with Bounding Boxes", use_container_width=True)
|
| 105 |
+
else:
|
| 106 |
+
st.write("No objects detected.")
|
| 107 |
+
st.write("Raw Prediction Results:", results)
|
| 108 |
+
st.write("Boxes:", results[0].boxes.xyxy)
|
| 109 |
+
st.write("Classes:", results[0].boxes.cls)
|
| 110 |
+
st.write("Confidences:", results[0].boxes.conf)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|