Commit ·
f1eba80
1
Parent(s): cd35e45
Upload 2 files
Browse files- app.py +95 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import io
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Function to load the Keras model
|
| 11 |
+
def load_keras_model():
|
| 12 |
+
model = load_model('model_bigger_app.h5')
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
# Function to download an image
|
| 16 |
+
def download_image(filename):
|
| 17 |
+
with open(filename, 'rb') as file:
|
| 18 |
+
image_data = file.read()
|
| 19 |
+
return image_data
|
| 20 |
+
|
| 21 |
+
# Main Streamlit app
|
| 22 |
+
def main():
|
| 23 |
+
|
| 24 |
+
# Sidebar for additional information
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
st.title('About This Classifier')
|
| 27 |
+
st.markdown('''
|
| 28 |
+
|
| 29 |
+
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.
|
| 30 |
+
- [Kaggle Dataset](https://www.kaggle.com/datasets/shaunthesheep/microsoft-catsvsdogs-dataset)
|
| 31 |
+
- [GitHub Project Repo](https://github.com/your_username/your_repo)
|
| 32 |
+
- [Paper](https://github.com/your_username/your_repo)
|
| 33 |
+
''')
|
| 34 |
+
|
| 35 |
+
st.title(' Cat or Dog Classifier :cat: :dog: ')
|
| 36 |
+
|
| 37 |
+
# Load Keras model
|
| 38 |
+
model = load_keras_model()
|
| 39 |
+
|
| 40 |
+
# Sidebar for downloading images
|
| 41 |
+
with st.sidebar:
|
| 42 |
+
st.title('Download Exampke Images')
|
| 43 |
+
|
| 44 |
+
# List of images to select from; replace this with your list of image names
|
| 45 |
+
image_names = ['img1.jpeg', 'img2.png', 'img3.jpeg']
|
| 46 |
+
selected_image = st.selectbox('Select an image:', image_names)
|
| 47 |
+
|
| 48 |
+
if selected_image:
|
| 49 |
+
directory_path = 'images' # Replace with your image directory
|
| 50 |
+
image_path = os.path.join(directory_path, selected_image)
|
| 51 |
+
image_data = download_image(image_path)
|
| 52 |
+
st.image(image_data, caption=selected_image, use_column_width=True)
|
| 53 |
+
|
| 54 |
+
download_button_label = 'Download ' + selected_image
|
| 55 |
+
st.download_button(label=download_button_label, data=image_data, file_name=selected_image, mime='image/jpeg')
|
| 56 |
+
|
| 57 |
+
# File uploader for image classification
|
| 58 |
+
upload = st.file_uploader(label='Upload an image:', type=['png', 'jpg', 'jpeg'])
|
| 59 |
+
|
| 60 |
+
if upload:
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with Image.open(upload).convert('RGB') as img:
|
| 64 |
+
original_img = img.copy()
|
| 65 |
+
img = img.resize((128, 128))
|
| 66 |
+
img_array = img_to_array(img)
|
| 67 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 68 |
+
img_array /= 255.0
|
| 69 |
+
|
| 70 |
+
prediction = model.predict(img_array)
|
| 71 |
+
|
| 72 |
+
confidence = prediction[0][0]
|
| 73 |
+
|
| 74 |
+
if confidence > 0.6:
|
| 75 |
+
label = "Dog"
|
| 76 |
+
elif confidence < 0.4:
|
| 77 |
+
label = "Cat"
|
| 78 |
+
else:
|
| 79 |
+
label = "Unknown"
|
| 80 |
+
|
| 81 |
+
fig, ax = plt.subplots()
|
| 82 |
+
ax.imshow(original_img)
|
| 83 |
+
ax.axis('off')
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
buf = io.BytesIO()
|
| 87 |
+
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
| 88 |
+
buf.seek(0)
|
| 89 |
+
|
| 90 |
+
col1, col2, col3 = st.columns([1,2,1])
|
| 91 |
+
with col2:
|
| 92 |
+
st.image(buf, caption=f"This is a {label}", use_column_width=True)
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
main()
|
requirements.txt
ADDED
|
File without changes
|