-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
59 lines (48 loc) · 1.72 KB
/
app.py
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
import streamlit as st
import tensorflow as tf
import cv2
from PIL import Image, ImageOps
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
st.title("Cat-Dog Classification")
st.header("Please input an image to be classified:")
st.text("Created by Saksham Gulati")
@st.cache(allow_output_mutation=True)
def teachable_machine_classification(img, weights_file):
# Load the model
model = keras.models.load_model(weights_file)
# Create the array of the right shape to feed into the keras model
data = np.ndarray(shape=(1, 200, 200, 3), dtype=np.float32)
image = img
#image sizing
size = (200, 200)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
#turn the image into a numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 255)
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction_percentage = model.predict(data)
prediction=prediction_percentage.round()
return prediction,prediction_percentage
uploaded_file = st.file_uploader("Choose an Cat or Dog Image...", type="jpg")
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded file', use_column_width=True)
st.write("")
st.write("Classifying...")
label,perc = teachable_machine_classification(image, 'catdog.h5')
st.write(label)
if label == 1:
st.write("Its a Dog, confidence level:",perc)
else:
st.write("Its a Cat, confidence level:",1-perc)