cat-dog-classifier / alter.py
zerishdorelser's picture
uploaded 8 files
a37d47e verified
raw
history blame
1.99 kB
import streamlit as st
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import numpy as np
#from process import process
model = tf.keras.models.load_model(r'C:\Users\Souvik Chand\Documents\python_my\apps\stream lits\cats and dogs\model3.h5')
labels = ['Cat', 'Dog']
def preprocess_image(image):
image = image.resize((256, 256))
image = np.array(image) / 255.0
image = np.expand_dims(image, axis=0)
return image
def process(image):
image= tf.cast(image/255, tf.float32)
return image
st.title('Dogs and cats')
st.write('Upload an image and the model will predict its class.')
st.sidebar.title('upload a photo')
uploaded_file = st.sidebar.file_uploader('choose image',accept_multiple_files=False, type=['jpg'])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True,width=100)
st.write(f'Original Image Shape: {image.size}')
#image = image.resize((256, 256)) # Adjust based on your model's input size
#image = np.array(image) / 255.0 # Normalize the image
#image = np.expand_dims(image, axis=0)
preprocessed_image = preprocess_image(image)
test_input = preprocessed_image.reshape((1,256,256,3))
predictions = model.predict(test_input)[0][0]
confidence= round((abs(predictions-0.5)/0.5)*100)
st.write(predictions)
if predictions<0.2:
st.write(f'Predicted class: Cat')
elif predictions>0.8:
st.write('Prediction class: Dog')
elif (predictions <0.7) or (predictions >0.6):
st.write('i feel you are trying to trick me!')
else:
st.write("looks like neither")
st.write(f'confidence: {confidence}%')
#predicted_class = np.argmax(predictions)
#confidence = predictions[0][predicted_class]
#st.write(f'Predicted class: {predicted_class[0]}')
#st.write(f'Predicted Class: {labels[predicted_class]}')
#st.write(f'Confidence: {confidence:.2%}')