|
from tensorflow.keras.models import load_model |
|
|
|
|
|
from tensorflow.keras.applications.xception import preprocess_input |
|
|
|
import numpy as np |
|
from tensorflow.keras.preprocessing.image import load_img, img_to_array |
|
import streamlit as st |
|
import cv2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write('# Cat and Dog Classifier') |
|
st.markdown( |
|
''' |
|
This app uses transfer learning on the Xception model to predict images of cats and dogs. |
|
It achieved an accuracy of approx. 99 percent on the validation set. |
|
|
|
*View on [Github](https://github.com/eskayML/cat-and-dogs-classification)* |
|
|
|
> ### Enter an image of either a cat or a dog for the model to predict. |
|
''' |
|
) |
|
|
|
|
|
|
|
model = load_model('Pikachu_and_Raichu.h5') |
|
|
|
|
|
def test_image(object_image): |
|
|
|
file_bytes = np.asarray(bytearray(object_image.read()), dtype=np.uint8) |
|
opencv_image = cv2.imdecode(file_bytes, 1) |
|
opencv_image = cv2.resize(opencv_image, (160, 160)) |
|
opencv_image.shape = (1, 160, 160, 3) |
|
opencv_image = preprocess_input(opencv_image) |
|
predictions = model.predict(opencv_image) |
|
|
|
if predictions[0, 0] >= 0.5: |
|
result = 'Pikachu' |
|
confidence = predictions[0, 0] * 100 |
|
else: |
|
result = 'Raichu' |
|
confidence = 100 - (predictions[0, 0] * 100) |
|
|
|
return result, round(confidence, 2) |
|
|
|
|
|
|
|
object_image = st.file_uploader("Upload an image...", type=[ |
|
'png', 'jpg', 'webp', 'jpeg']) |
|
submit = st.button('Predict') |
|
|
|
if submit: |
|
if object_image is not None: |
|
output = test_image(object_image) |
|
|
|
|
|
st.image(object_image, channels="BGR") |
|
st.markdown(f"""## This is an image of a: {output[0]} """) |
|
st.write(f'# Confidence: ${ output[1]}$ %') |
|
|
|
|
|
|