Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 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 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
#
|
| 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 |
-
st.error(f"An error occurred: {str(e)}")
|
| 63 |
-
|
| 64 |
-
if os.path.exists(temp_filename):
|
| 65 |
-
os.remove(temp_filename)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
| 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)
|