trohith89 commited on
Commit
1b7540f
·
verified ·
1 Parent(s): 99947f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -61
app.py CHANGED
@@ -1,68 +1,57 @@
1
- import streamlit as st
2
- import cv2
3
- import numpy as np
4
  from tensorflow.keras.models import load_model
 
 
 
 
 
5
  import pickle
6
- from PIL import Image
7
- import os
8
 
9
- # Load the model and label encoder
10
- @st.cache_resource
11
- def load_resources():
12
- # Custom loading to handle compatibility
13
- try:
14
- model = load_model('captains_cv2_model.keras', compile=False) # Load without compiling first
15
- except Exception as e:
16
- st.error(f"Model loading failed: {str(e)}")
17
- raise
18
- with open('label_encoder.pkl', 'rb') as file:
19
- le = pickle.load(file)
20
- return model, le
21
 
22
- # Preprocess the image
23
- def preprocess_image(image_path):
24
- img1 = cv2.imread(image_path)
25
- img1 = cv2.resize(img1, (64, 64, 3)) # Resize to 64x64
26
- # img1 = np.asarray(img1) # Shape: (64, 64, 3)
27
- img1 = img1[np.newaxis, :, :, :] # Shape: (1, 64, 64, 3)
28
- return img1
29
 
30
- # Main app
31
- def main():
32
- model, le = load_resources()
33
-
34
- st.title("Image Classification App")
35
- st.write("Upload an image to get a prediction")
36
-
37
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
38
 
39
- if uploaded_file is not None:
40
- image = Image.open(uploaded_file)
41
- st.image(image, caption='Uploaded Image', use_column_width=True)
42
-
43
- file_extension = os.path.splitext(uploaded_file.name)[1].lower()
44
- temp_filename = f"temp_image{file_extension}"
45
-
46
- with open(temp_filename, "wb") as f:
47
- f.write(uploaded_file.getvalue())
48
-
49
- try:
50
- processed_img = preprocess_image(temp_filename)
51
- st.write(f"Processed image shape: {processed_img.shape}")
52
-
53
- prediction = model.predict(processed_img)
54
- predicted_class = le.inverse_transform([np.argmax(prediction)])
55
-
56
- st.write("Prediction:", predicted_class[0])
57
- st.write("Prediction Probabilities:")
58
- for class_name, prob in zip(le.classes_, prediction[0]):
59
- st.write(f"{class_name}: {prob:.4f}")
60
-
61
- except Exception as e:
62
- st.error(f"An error occurred: {str(e)}")
63
-
64
- if os.path.exists(temp_filename):
65
- os.remove(temp_filename)
66
 
67
- if __name__ == '__main__':
68
- main()
 
 
 
1
+ # Import necessary libraries
2
+ import tensorflow as tf
 
3
  from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
+ import numpy as np
6
+ from google.colab import files
7
+ import cv2
8
+ from google.colab.patches import cv2_imshow
9
  import pickle
 
 
10
 
11
+ # Upload your model and label encoder files to Colab
12
+ print("Please upload your captains_cv2_model.keras file")
13
+ uploaded_model = files.upload()
14
+ model_filename = list(uploaded_model.keys())[0]
 
 
 
 
 
 
 
 
15
 
16
+ print("Please upload your label_encoder.pkl file")
17
+ uploaded_encoder = files.upload()
18
+ encoder_filename = list(uploaded_encoder.keys())[0]
 
 
 
 
19
 
20
+ # Load the pretrained model
21
+ model = load_model(model_filename)
22
+
23
+ # Load the label encoder
24
+ with open(encoder_filename, 'rb') as file:
25
+ label_encoder = pickle.load(file)
26
+
27
+ # Function to preprocess the image (adjust based on your model's requirements)
28
+ def preprocess_image(img_path, target_size=(64, 64)): # Adjust target_size as per your model
29
 
30
+ img = cv2.imread(img_path)
31
+ img_array = np.asarray(img)
32
+ img_array = img_array[np.newaxis, :, :, :]
33
+ return img_array
34
+
35
+ # Upload an image to predict
36
+ print("Please upload an image to classify")
37
+ uploaded_image = files.upload()
38
+
39
+ # Get the uploaded image filename
40
+ img_filename = list(uploaded_image.keys())[0]
41
+
42
+ # Preprocess the image
43
+ processed_image = preprocess_image(img_filename)
44
+
45
+ # Make prediction
46
+ prediction = model.predict(processed_image)
47
+
48
+ # Get the predicted class index
49
+ predicted_class_index = np.argmax(prediction, axis=1)[0]
50
+
51
+ # Decode the prediction using the label encoder
52
+ predicted_class = label_encoder.inverse_transform([predicted_class_index])[0]
 
 
 
 
53
 
54
+ # Output the prediction
55
+ print("Prediction probabilities:", prediction)
56
+ print("Predicted class index:", predicted_class_index)
57
+ print("Predicted class:", predicted_class)