okeowo1014 commited on
Commit
76baae0
·
verified ·
1 Parent(s): cf92fe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -5
app.py CHANGED
@@ -1,6 +1,65 @@
 
 
 
 
 
 
1
  import streamlit as st
2
- # import train
3
- # import tr
4
- import prd
5
- x = st.slider('Select a value')
6
- st.write(x, 'squared is', x * 86)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import streamlit as st
2
+ # # import train
3
+ # # import tr
4
+ # # import prd
5
+ # x = st.slider('Select a value')
6
+ # st.write(x, 'squared is', x * 86)
7
  import streamlit as st
8
+ import cv2
9
+ import numpy as np
10
+ from tensorflow.keras.preprocessing import image
11
+ import tensorflow as tf
12
+ from huggingface_hub import from_pretrained_keras
13
+
14
+ model = from_pretrained_keras("okeowo1014/catsanddogs")
15
+
16
+ # Load the saved model (replace with your model filename)
17
+ # model = tf.keras.models.load_model('cat_dog_classifier.keras')
18
+
19
+ # Image dimensions for the model
20
+ img_width, img_height = 224, 224
21
+
22
+
23
+ def preprocess_image(img):
24
+ """Preprocesses an image for prediction."""
25
+ img = cv2.resize(img, (img_width, img_height))
26
+ img = img.astype('float32') / 255.0
27
+ img = np.expand_dims(img, axis=0)
28
+ return img
29
+
30
+
31
+ def predict_class(image):
32
+ """Predicts image class and probabilities."""
33
+ preprocessed_img = preprocess_image(image)
34
+ prediction = model.predict(preprocessed_img)
35
+ class_names = ['cat', 'dog'] # Adjust class names according to your model
36
+ return class_names[np.argmax(prediction)], np.max(prediction)
37
+
38
+
39
+ def display_results(class_name, probability):
40
+ """Displays prediction results in a progress bar style."""
41
+ st.write(f"**Predicted Class:** {class_name}")
42
+
43
+ # Create a progress bar using st.progress
44
+ progress = st.progress(0)
45
+ for i in range(100):
46
+ progress.progress(i + 1)
47
+ if i == int(probability * 100):
48
+ break
49
+ st.write(f"**Probability:** {probability:.2f}")
50
+
51
+
52
+ def main():
53
+ """Main app function."""
54
+ st.title("Image Classifier")
55
+ st.write("Upload an image to classify it as cat or dog.")
56
+
57
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
58
+ if uploaded_file is not None:
59
+ image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR)
60
+ st.image(image, caption="Uploaded Image", use_column_width=True)
61
+
62
+ predicted_class, probability = predict_class(image)
63
+ display_results(predicted_class, probability)
64
+
65
+ main()