robitalhazmi commited on
Commit
250f34d
·
1 Parent(s): ca8b057

Add application file

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import keras
5
+
6
+ # Load pre-trained model
7
+ model = keras.models.load_model('./image_classification_model.keras')
8
+ image_size = (180, 180)
9
+
10
+ # Function to make prediction
11
+ def predict(image):
12
+ image_size = (180, 180)
13
+ img = keras.utils.load_img(image, target_size=image_size)
14
+
15
+ img_array = keras.utils.img_to_array(img)
16
+ img_array = np.expand_dims(img_array, 0) # Create batch axis
17
+
18
+ predictions = model.predict(img_array)
19
+ score = float(keras.activations.sigmoid(predictions[0][0]))
20
+ return score
21
+
22
+ # Streamlit app
23
+ def main():
24
+ st.title("Image Classification from Scratch")
25
+ st.write("Upload an image to predict whether the image contains a cat or a dog.")
26
+
27
+ uploaded_image = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
28
+
29
+ if uploaded_image is not None:
30
+ image = Image.open(uploaded_image)
31
+ st.image(image, caption='Uploaded Image', use_column_width=True)
32
+
33
+ if st.button('Predict'):
34
+ score = predict(uploaded_image)
35
+ if (1 - score) > score:
36
+ st.write('Prediction Result: {:.2f}% Cat'.format(100 * (1 - score)))
37
+ else:
38
+ st.write('Prediction Result: {:.2f}% Dog'.format(100 * score))
39
+
40
+ if __name__ == '__main__':
41
+ main()