Spaces:
Runtime error
Runtime error
Commit ·
523a6d5
1
Parent(s): 11a28bc
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,48 +3,43 @@ 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 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
if uploaded_file is not None:
|
| 42 |
-
|
| 43 |
-
with open(
|
| 44 |
f.write(uploaded_file.getbuffer())
|
| 45 |
-
predicted_class =
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
if __name__ == '__main__':
|
| 50 |
-
|
|
|
|
| 3 |
from keras.preprocessing import image
|
| 4 |
from keras.models import load_model
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Define the dictionary of classes and load the model
|
| 8 |
+
CLASSES = {
|
| 9 |
+
'french_bulldog': 0,
|
| 10 |
+
'german_shepherd': 1,
|
| 11 |
+
'golden_retriever': 2,
|
| 12 |
+
'poodle': 3,
|
| 13 |
+
'yorkshire_terrier': 4
|
| 14 |
+
}
|
| 15 |
+
MODEL_PATH = 'best_model.h5'
|
| 16 |
+
model = load_model(MODEL_PATH)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Define a function to make predictions on a given image
|
| 20 |
+
def predict_breed(image_file):
|
| 21 |
+
img = image.load_img(image_file, target_size=(256, 256))
|
| 22 |
x = image.img_to_array(img)
|
| 23 |
x = np.expand_dims(x, axis=0)
|
| 24 |
x = x / 255.
|
|
|
|
|
|
|
| 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 |
return predicted_class
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Create the Streamlit app
|
| 32 |
+
def main():
|
| 33 |
+
st.title('Dog Breed Classification')
|
| 34 |
+
uploaded_file = st.file_uploader('Choose an image of a dog', type=['jpg', 'jpeg', 'png'])
|
| 35 |
+
|
| 36 |
if uploaded_file is not None:
|
| 37 |
+
image_file = uploaded_file.name
|
| 38 |
+
with open(image_file, 'wb') as f:
|
| 39 |
f.write(uploaded_file.getbuffer())
|
| 40 |
+
predicted_class = predict_breed(image_file)
|
| 41 |
+
st.image(uploaded_file, caption=f'Predicted class: {predicted_class}', use_column_width=True)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
if __name__ == '__main__':
|
| 45 |
+
main()
|