|
|
|
import streamlit as st |
|
import numpy as np |
|
import pandas as pd |
|
from keras.models import load_model |
|
from keras.preprocessing import image |
|
import os |
|
import matplotlib.pyplot as plt |
|
import random |
|
|
|
|
|
st.set_page_config(page_title="Blood Group Detection", layout="wide") |
|
|
|
|
|
st.title("π©Έ Blood Group Detection using LeNet Model") |
|
|
|
|
|
st.sidebar.header("Navigation") |
|
selected_option = st.sidebar.selectbox( |
|
"Select an option:", |
|
["Home", "EDA", "Predict Blood Group"] |
|
) |
|
|
|
|
|
@st.cache_resource |
|
def load_trained_model(): |
|
model = load_model('best_model.h5') |
|
return model |
|
|
|
model = load_trained_model() |
|
|
|
|
|
class_names = ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-'] |
|
|
|
|
|
DATASET_DIR = "dataset" |
|
|
|
|
|
if selected_option == "Home": |
|
st.subheader("About the Project") |
|
st.write(""" |
|
Welcome to the Blood Group Detection App! |
|
|
|
This application uses a Deep Learning model (LeNet architecture) to detect blood groups from blood sample images. |
|
|
|
### π Technologies Used: |
|
- Streamlit for Web UI |
|
- TensorFlow/Keras for Deep Learning |
|
- Image Processing with Computer Vision |
|
|
|
**Upload a blood sample image and predict the blood group instantly!** |
|
""") |
|
try: |
|
st.image("blood_home.jpg", caption="Blood Sample Analysis", use_column_width=True) |
|
except: |
|
st.warning("Home image not found. (Optional)") |
|
|
|
|
|
elif selected_option == "EDA": |
|
st.subheader("Exploratory Data Analysis (EDA)") |
|
|
|
|
|
if os.path.exists(DATASET_DIR): |
|
st.write("### π Number of Images per Blood Group:") |
|
|
|
counts = {} |
|
for class_name in class_names: |
|
class_path = os.path.join(DATASET_DIR, class_name) |
|
if os.path.exists(class_path): |
|
counts[class_name] = len(os.listdir(class_path)) |
|
else: |
|
counts[class_name] = 0 |
|
|
|
df_counts = pd.DataFrame(list(counts.items()), columns=['Blood Group', 'Number of Images']) |
|
st.dataframe(df_counts) |
|
|
|
|
|
st.bar_chart(df_counts.set_index('Blood Group')) |
|
|
|
st.write("### πΌοΈ Sample Images from Each Class:") |
|
|
|
cols = st.columns(4) |
|
|
|
for idx, class_name in enumerate(class_names): |
|
class_path = os.path.join(DATASET_DIR, class_name) |
|
if os.path.exists(class_path) and len(os.listdir(class_path)) > 0: |
|
img_file = random.choice(os.listdir(class_path)) |
|
img_path = os.path.join(class_path, img_file) |
|
img = image.load_img(img_path, target_size=(64, 64)) |
|
with cols[idx % 4]: |
|
st.image(img, caption=class_name, width=150) |
|
|
|
st.write("### π§© Image Properties:") |
|
sample_class = class_names[0] |
|
sample_path = os.path.join(DATASET_DIR, sample_class, os.listdir(os.path.join(DATASET_DIR, sample_class))[0]) |
|
sample_img = image.load_img(sample_path) |
|
st.write(f"- **Image shape:** {np.array(sample_img).shape}") |
|
st.write(f"- **Color channels:** {np.array(sample_img).shape[-1]} (RGB)") |
|
|
|
else: |
|
st.warning("Dataset not found! Please make sure the 'dataset/train' folder exists.") |
|
|
|
|
|
elif selected_option == "Predict Blood Group": |
|
st.subheader("Upload an Image to Predict Blood Group") |
|
|
|
uploaded_file = st.file_uploader("Choose a blood sample image...", type=["jpg", "jpeg", "png", "bmp"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
if not os.path.exists('temp'): |
|
os.makedirs('temp') |
|
|
|
|
|
temp_file_path = os.path.join("temp", uploaded_file.name) |
|
with open(temp_file_path, "wb") as f: |
|
f.write(uploaded_file.getbuffer()) |
|
|
|
|
|
img = image.load_img(temp_file_path, target_size=(224, 224)) |
|
img_array = image.img_to_array(img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
img_array = img_array / 255.0 |
|
|
|
|
|
with st.spinner('Predicting...'): |
|
prediction = model.predict(img_array) |
|
predicted_class = class_names[np.argmax(prediction)] |
|
|
|
|
|
st.success(f"𧬠Predicted Blood Group: **{predicted_class}**") |
|
|
|
|
|
os.remove(temp_file_path) |
|
|