Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from keras.models import load_model # TensorFlow is required for Keras to work
|
2 |
+
from PIL import Image, ImageOps # Install pillow instead of PIL
|
3 |
+
import numpy as np
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
# Disable scientific notation for clarity
|
7 |
+
np.set_printoptions(suppress=True)
|
8 |
+
|
9 |
+
# Load the model
|
10 |
+
model = load_model("keras_model.h5", compile=False)
|
11 |
+
|
12 |
+
# Load the labels
|
13 |
+
class_names = open("labels.txt", "r").readlines()
|
14 |
+
|
15 |
+
st.title("Cat Breed Identifier")
|
16 |
+
st.header("Upload an Image to classify")
|
17 |
+
|
18 |
+
uploaded_file = st.file_uploader("Choose the image...", type=['jpg','jpeg', 'png'])
|
19 |
+
if uploaded_file is not None:
|
20 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
21 |
+
|
22 |
+
# Replace this with the path to your image
|
23 |
+
image = Image.open(uploaded_file).convert("RGB")
|
24 |
+
st.image(image, caption="Uploaded Image...",use_column_width=True)
|
25 |
+
|
26 |
+
# resizing the image to be at least 224x224 and then cropping from the center
|
27 |
+
size = (224, 224)
|
28 |
+
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
|
29 |
+
|
30 |
+
# turn the image into a numpy array
|
31 |
+
image_array = np.asarray(image)
|
32 |
+
|
33 |
+
# Normalize the image
|
34 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
|
35 |
+
|
36 |
+
# Load the image into the array
|
37 |
+
data[0] = normalized_image_array
|
38 |
+
|
39 |
+
# Predicts the model
|
40 |
+
prediction = model.predict(data)
|
41 |
+
index = np.argmax(prediction)
|
42 |
+
class_name = class_names[index]
|
43 |
+
confidence_score = prediction[0][index]
|
44 |
+
|
45 |
+
# Print prediction and confidence score
|
46 |
+
st.write(f"Class: {class_name[2:].strip()}")
|
47 |
+
st.write(f"Confidence Score: {confidence_score}")
|