File size: 2,072 Bytes
6dc2347
7290790
 
 
 
 
 
 
 
 
 
 
 
76baae0
7290790
76baae0
7290790
 
76baae0
7290790
 
76baae0
 
7290790
 
 
 
 
 
76baae0
 
7290790
 
 
 
 
2bb42d6
847d2ae
 
 
 
 
 
76baae0
 
7290790
 
 
76baae0
8924508
 
 
 
 
 
 
76baae0
 
7290790
 
 
 
76baae0
7290790
 
 
 
76baae0
7290790
 
76baae0
7290790
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# import streamlit as st
# import train
# # import tr
# # import prd
# x = st.slider('Select a value')
# st.write(x, 'squared is', x * 86)

import streamlit as st
import cv2
import numpy as np
from tensorflow.keras.preprocessing import image
import tensorflow as tf
from huggingface_hub import from_pretrained_keras

model = from_pretrained_keras("okeowo1014/catsanddogsmodel")

# Load the saved model (replace with your model filename)
# model = tf.keras.models.load_model('cat_dog_classifier.keras')

# Image dimensions for the model
img_width, img_height = 224, 224


def preprocess_image(img):
  """Preprocesses an image for prediction."""
  img = cv2.resize(img, (img_width, img_height))
  img = img.astype('float32') / 255.0
  img = np.expand_dims(img, axis=0)
  return img


def predict_class(image):
  """Predicts image class and probabilities."""
  preprocessed_img = preprocess_image(image)
  prediction = model.predict(preprocessed_img)
  class_names = ['cat', 'dog']  # Adjust class names according to your model
  print(np.argmax(prediction), np.max(prediction))
  p=np.max(prediction)
  if p >0.5:
      cn=1
  else:
      cn=0
  return class_names[cn], p


def display_results(class_name, probability):
  """Displays prediction results in a progress bar style."""
  st.write(f"**Predicted Class:** {class_name}")

  # # Create a progress bar using st.progress
  # progress = st.progress(0)
  # for i in range(100):
  #   progress.progress(i + 1)
  #   if i == int(probability * 100):
  #     break
  # st.write(f"**Probability:** {probability:.2f}")


def main():
  """Main app function."""
  st.title("Image Classifier")
  st.write("Upload an image to classify it as cat or dog.")

  uploaded_file = st.file_uploader("Choose an image...", type="jpg")
  if uploaded_file is not None:
    image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    predicted_class, probability = predict_class(image)
    display_results(predicted_class, probability)

main()