Spaces:
Runtime error
Runtime error
Commit ·
11a28bc
1
Parent(s): 24686cf
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from keras.preprocessing import image
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# Define a dictionary of classes
|
| 7 |
+
classes = {'french_bulldog': 0,
|
| 8 |
+
'german_shepherd': 1,
|
| 9 |
+
'golden_retriever': 2,
|
| 10 |
+
'poodle': 3,
|
| 11 |
+
'yorkshire_terrier': 4}
|
| 12 |
+
|
| 13 |
+
# Load the saved model from the disk
|
| 14 |
+
model = load_model('best_model.h5')
|
| 15 |
+
|
| 16 |
+
# Define the function for predicting the dog breed
|
| 17 |
+
def predict_dog_breed(image_path):
|
| 18 |
+
# Load the image from the specified path and preprocess it
|
| 19 |
+
img = image.load_img(image_path, target_size=(256, 256))
|
| 20 |
+
x = image.img_to_array(img)
|
| 21 |
+
x = np.expand_dims(x, axis=0)
|
| 22 |
+
x = x / 255.
|
| 23 |
+
|
| 24 |
+
# Make the prediction using the trained model
|
| 25 |
+
preds = model.predict(x)
|
| 26 |
+
class_idx = np.argmax(preds[0])
|
| 27 |
+
predicted_class = [k for k, v in classes.items() if v == class_idx][0]
|
| 28 |
+
|
| 29 |
+
# Return the predicted dog breed
|
| 30 |
+
return predicted_class
|
| 31 |
+
|
| 32 |
+
# Define the Streamlit app
|
| 33 |
+
def app():
|
| 34 |
+
st.title("Dog Breed Classification App")
|
| 35 |
+
st.write("Upload an image of a dog to predict its breed.")
|
| 36 |
+
|
| 37 |
+
# Allow the user to upload an image file
|
| 38 |
+
uploaded_file = st.file_uploader("Choose a dog image...", type=["jpg", "jpeg", "png"])
|
| 39 |
+
|
| 40 |
+
# Make the prediction when the user clicks the "Predict" button
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
image_path = f"tmp/{uploaded_file.name}"
|
| 43 |
+
with open(image_path, "wb") as f:
|
| 44 |
+
f.write(uploaded_file.getbuffer())
|
| 45 |
+
predicted_class = predict_dog_breed(image_path)
|
| 46 |
+
st.write(f"Predicted dog breed: {predicted_class}")
|
| 47 |
+
|
| 48 |
+
# Run the Streamlit app
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
app()
|