VinayakMane47 commited on
Commit
523a6d5
1 Parent(s): 11a28bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
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
- # 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()
 
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()