File size: 3,689 Bytes
f1eba80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
import matplotlib.pyplot as plt
import io
import os

# Function to load the Keras model
def load_keras_model():
    model = load_model('model_bigger_app.h5')
    return model

# Function to download an image
def download_image(filename):
    with open(filename, 'rb') as file:
        image_data = file.read()
    return image_data

# Main Streamlit app
def main():

     # Sidebar for additional information
    with st.sidebar:
        st.title('About This Classifier')
        st.markdown('''
        
        This Cat or Dog Classifier utilizes a Convolutional Neural Network (CNN) to carry out binary image classification tasks. Engineered by Barry Rerecich, the architecture incorporates multiple convolutional layers, each followed by max-pooling layers, and concludes with a fully connected dense layer activated by a sigmoid function for binary outcomes. The model was trained on a Kaggle dataset featuring labeled images of cats and dogs, and it employs binary cross-entropy as the loss function. The project stands as a comprehensive study on the applicability and efficacy of CNNs in high-precision image classification.
        - [Kaggle Dataset](https://www.kaggle.com/datasets/shaunthesheep/microsoft-catsvsdogs-dataset)
        - [GitHub Project Repo](https://github.com/your_username/your_repo) 
        - [Paper](https://github.com/your_username/your_repo) 
        ''')

    st.title(' Cat or Dog Classifier :cat: :dog: ')
    
    # Load Keras model
    model = load_keras_model()

    # Sidebar for downloading images
    with st.sidebar:
        st.title('Download Exampke Images')
        
        # List of images to select from; replace this with your list of image names
        image_names = ['img1.jpeg', 'img2.png', 'img3.jpeg']
        selected_image = st.selectbox('Select an image:', image_names)
        
        if selected_image:
            directory_path = 'images'  # Replace with your image directory
            image_path = os.path.join(directory_path, selected_image)
            image_data = download_image(image_path)
            st.image(image_data, caption=selected_image, use_column_width=True)
            
            download_button_label = 'Download ' + selected_image
            st.download_button(label=download_button_label, data=image_data, file_name=selected_image, mime='image/jpeg')
    
    # File uploader for image classification
    upload = st.file_uploader(label='Upload an image:', type=['png', 'jpg', 'jpeg'])
    
    if upload:
        

        with Image.open(upload).convert('RGB') as img:
            original_img = img.copy()
            img = img.resize((128, 128))
            img_array = img_to_array(img)
            img_array = np.expand_dims(img_array, axis=0)
            img_array /= 255.0

            prediction = model.predict(img_array)
            
            confidence = prediction[0][0]
            
            if confidence > 0.6:
                label = "Dog"
            elif confidence < 0.4:
                label = "Cat"
            else:
                label = "Unknown"

            fig, ax = plt.subplots()
            ax.imshow(original_img)
            ax.axis('off')
            

            buf = io.BytesIO()
            plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
            buf.seek(0)

            col1, col2, col3 = st.columns([1,2,1])
            with col2:
                st.image(buf, caption=f"This is a {label}", use_column_width=True)

if __name__ == '__main__':
    main()