Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,26 @@ import gradio as gr
|
|
3 |
import numpy as np
|
4 |
import tensorflow as tf
|
5 |
import cv2
|
|
|
6 |
|
7 |
# Load class labels from the text file
|
8 |
train_info = []
|
9 |
with open('labelwithspace.txt', 'r') as file:
|
10 |
train_info = [line.strip() for line in file.readlines()]
|
11 |
|
12 |
-
#
|
13 |
-
def
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
return model
|
22 |
|
23 |
-
# Initialize the
|
24 |
-
model =
|
25 |
|
26 |
# Function to preprocess the image and make predictions
|
27 |
def predict_image(image):
|
@@ -29,16 +30,16 @@ def predict_image(image):
|
|
29 |
img = cv2.resize(image, (224, 224))
|
30 |
img = img / 255.0 # Normalize to [0, 1] range
|
31 |
|
32 |
-
# Make predictions using the
|
33 |
predictions = model.predict(img[np.newaxis, ...])[0]
|
34 |
top_classes = np.argsort(predictions)[-3:][::-1] # Indices of top 3 predictions
|
35 |
top_class = top_classes[0] # Index of the highest probability class
|
36 |
label = train_info[top_class] # Get the corresponding label
|
37 |
return label
|
38 |
|
39 |
-
# Define the Gradio interface
|
40 |
-
input_image = gr.Image()
|
41 |
output_label = gr.Label()
|
42 |
|
43 |
-
# Launch the Gradio interface
|
44 |
-
gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label
|
|
|
3 |
import numpy as np
|
4 |
import tensorflow as tf
|
5 |
import cv2
|
6 |
+
import tensorflow_hub as hub
|
7 |
|
8 |
# Load class labels from the text file
|
9 |
train_info = []
|
10 |
with open('labelwithspace.txt', 'r') as file:
|
11 |
train_info = [line.strip() for line in file.readlines()]
|
12 |
|
13 |
+
# Load your actual model from the .h5 file
|
14 |
+
def load_real_model():
|
15 |
+
try:
|
16 |
+
# Register KerasLayer from TensorFlow Hub if used
|
17 |
+
custom_objects = {'KerasLayer': hub.KerasLayer}
|
18 |
+
model = tf.keras.models.load_model('bird_model4.h5', custom_objects=custom_objects)
|
19 |
+
except Exception as e:
|
20 |
+
print("Error loading the model:", e)
|
21 |
+
exit()
|
22 |
return model
|
23 |
|
24 |
+
# Initialize the real model
|
25 |
+
model = load_real_model()
|
26 |
|
27 |
# Function to preprocess the image and make predictions
|
28 |
def predict_image(image):
|
|
|
30 |
img = cv2.resize(image, (224, 224))
|
31 |
img = img / 255.0 # Normalize to [0, 1] range
|
32 |
|
33 |
+
# Make predictions using the loaded model
|
34 |
predictions = model.predict(img[np.newaxis, ...])[0]
|
35 |
top_classes = np.argsort(predictions)[-3:][::-1] # Indices of top 3 predictions
|
36 |
top_class = top_classes[0] # Index of the highest probability class
|
37 |
label = train_info[top_class] # Get the corresponding label
|
38 |
return label
|
39 |
|
40 |
+
# Define the Gradio interface
|
41 |
+
input_image = gr.Image()
|
42 |
output_label = gr.Label()
|
43 |
|
44 |
+
# Launch the Gradio interface for Hugging Face deployment
|
45 |
+
gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label).launch()
|