brxerq commited on
Commit
10e053b
·
verified ·
1 Parent(s): 8b024f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -2,29 +2,26 @@
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
9
- tf.keras.utils.get_custom_objects().update({'KerasLayer': hub.KerasLayer})
 
 
10
 
11
- # Attempt to load the model with custom objects
12
  try:
13
- model = tf.keras.models.load_model('bird_model4.h5', custom_objects={'KerasLayer': hub.KerasLayer})
 
14
  except Exception as e:
15
  print("Error loading the model:", e)
16
  exit()
17
 
18
- # Load class labels from the text file
19
- train_info = []
20
- with open('labelwithspace.txt', 'r') as file:
21
- train_info = [line.strip() for line in file.readlines()]
22
-
23
  # Function to preprocess the image and make predictions
24
  def predict_image(image):
25
- # Resize the image to 224x224 and normalize pixel values
26
  img = cv2.resize(image, (224, 224))
27
- img = img / 255.0
28
 
29
  # Make predictions
30
  predictions = model.predict(img[np.newaxis, ...])[0]
 
2
  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
+ # Attempt to load the model
13
  try:
14
+ # Load the model from the .h5 file
15
+ model = tf.keras.models.load_model('bird_model4.h5')
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 the image
23
  img = cv2.resize(image, (224, 224))
24
+ img = img / 255.0 # Normalize to [0, 1] range
25
 
26
  # Make predictions
27
  predictions = model.predict(img[np.newaxis, ...])[0]