Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,30 @@ import numpy as np
|
|
| 4 |
import tensorflow as tf
|
| 5 |
import tensorflow_hub as hub
|
| 6 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Register KerasLayer from TensorFlow Hub
|
| 9 |
tf.keras.utils.get_custom_objects().update({'KerasLayer': hub.KerasLayer})
|
| 10 |
|
| 11 |
-
# Load the model
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
model = tf.keras.models.load_model('bird_model4.h5', custom_objects={'KerasLayer': hub.KerasLayer})
|
| 15 |
except Exception as e:
|
| 16 |
print("Error loading the model:", e)
|
| 17 |
exit()
|
| 18 |
|
| 19 |
# Load class labels from the text file
|
| 20 |
train_info = []
|
| 21 |
-
with open(
|
| 22 |
train_info = [line.strip() for line in file.readlines()]
|
| 23 |
|
| 24 |
# Function to preprocess the image and make predictions
|
| 25 |
def predict_image(image):
|
| 26 |
-
# Resize and normalize
|
| 27 |
img = cv2.resize(image, (224, 224))
|
| 28 |
img = img / 255.0
|
| 29 |
|
|
|
|
| 4 |
import tensorflow as tf
|
| 5 |
import tensorflow_hub as hub
|
| 6 |
import cv2
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Path to the model file and label file
|
| 10 |
+
model_path = os.path.join(os.getcwd(), 'bird_model4.h5')
|
| 11 |
+
label_file_path = os.path.join(os.getcwd(), 'labelwithspace.txt')
|
| 12 |
|
| 13 |
# Register KerasLayer from TensorFlow Hub
|
| 14 |
tf.keras.utils.get_custom_objects().update({'KerasLayer': hub.KerasLayer})
|
| 15 |
|
| 16 |
+
# Load the model with custom objects
|
| 17 |
try:
|
| 18 |
+
model = tf.keras.models.load_model(model_path, custom_objects={'KerasLayer': hub.KerasLayer})
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
print("Error loading the model:", e)
|
| 21 |
exit()
|
| 22 |
|
| 23 |
# Load class labels from the text file
|
| 24 |
train_info = []
|
| 25 |
+
with open(label_file_path, 'r') as file:
|
| 26 |
train_info = [line.strip() for line in file.readlines()]
|
| 27 |
|
| 28 |
# Function to preprocess the image and make predictions
|
| 29 |
def predict_image(image):
|
| 30 |
+
# Resize the image to 224x224 and normalize pixel values
|
| 31 |
img = cv2.resize(image, (224, 224))
|
| 32 |
img = img / 255.0
|
| 33 |
|