File size: 4,844 Bytes
8882732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Import required libraries
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

# Set page configuration FIRST
st.set_page_config(page_title="Blood Group Detection", layout="wide")

# Title of the app
st.title("๐Ÿฉธ Blood Group Detection using LeNet Model")

# Sidebar navigation
st.sidebar.header("Navigation")
selected_option = st.sidebar.selectbox(
    "Select an option:",
    ["Home", "EDA", "Predict Blood Group"]
)

# Load the trained model
@st.cache_resource
def load_trained_model():
    model = load_model('best_model.h5')  # Ensure correct path
    return model

model = load_trained_model()

# Define class labels
class_names = ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-']

# Dataset directory (you must adjust this if needed)
DATASET_DIR = "dataset"  # Example path

# Home page
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)")

# EDA page
elif selected_option == "EDA":
    st.subheader("Exploratory Data Analysis (EDA)")

    # Check if dataset exists
    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)

        # Bar Chart
        st.bar_chart(df_counts.set_index('Blood Group'))

        st.write("### ๐Ÿ–ผ๏ธ Sample Images from Each Class:")

        cols = st.columns(4)  # create 4 columns

        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))  # resized
                with cols[idx % 4]:  # arrange in 4 columns
                    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.")

# Prediction page
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:
        # Display uploaded image
        st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)

        # Ensure temp directory exists
        if not os.path.exists('temp'):
            os.makedirs('temp')

        # Save uploaded file temporarily
        temp_file_path = os.path.join("temp", uploaded_file.name)
        with open(temp_file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())

        # Preprocess the image
        img = image.load_img(temp_file_path, target_size=(224, 224))  # Adjust size if needed
        img_array = image.img_to_array(img)
        img_array = np.expand_dims(img_array, axis=0)
        img_array = img_array / 255.0  # Normalize pixel values

        # Predict the blood group
        with st.spinner('Predicting...'):
            prediction = model.predict(img_array)
            predicted_class = class_names[np.argmax(prediction)]

        # Show result
        st.success(f"๐Ÿงฌ Predicted Blood Group: **{predicted_class}**")

        # Remove temporary file
        os.remove(temp_file_path)