Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,28 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
|
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
with open('labelwithspace.txt', 'r') as file:
|
| 10 |
-
train_info = [line.strip() for line in file.readlines()]
|
| 11 |
|
| 12 |
-
#
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
except Exception as e:
|
| 17 |
print("Error loading the model:", e)
|
| 18 |
exit()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Function to preprocess the image and make predictions
|
| 21 |
def predict_image(image):
|
| 22 |
-
# Resize and normalize
|
| 23 |
img = cv2.resize(image, (224, 224))
|
| 24 |
img = img / 255.0 # Normalize to [0, 1] range
|
| 25 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
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 in a custom object scope
|
| 9 |
+
custom_objects = {'KerasLayer': hub.KerasLayer}
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load the model and ensure KerasLayer is registered
|
| 12 |
try:
|
| 13 |
+
with tf.keras.utils.custom_object_scope(custom_objects):
|
| 14 |
+
model = tf.keras.models.load_model('bird_model4.h5')
|
| 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('labelwithspace.txt', 'r') as file:
|
| 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 the image to 224x224 and normalize pixel values
|
| 27 |
img = cv2.resize(image, (224, 224))
|
| 28 |
img = img / 255.0 # Normalize to [0, 1] range
|
| 29 |
|