brxerq commited on
Commit
f7db520
·
verified ·
1 Parent(s): 7e55223

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -5,29 +5,32 @@ 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
 
30
- # Make predictions
31
  predictions = model.predict(img[np.newaxis, ...])[0]
32
  top_classes = np.argsort(predictions)[-3:][::-1] # Indices of top 3 predictions
33
  top_class = top_classes[0] # Index of the highest probability class
 
5
  import tensorflow_hub as hub
6
  import cv2
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
+ # Construct a simple mock model using the Functional API
14
+ def create_mock_model():
15
+ inputs = tf.keras.Input(shape=(224, 224, 3))
16
+ # A simple convolutional base (for demonstration purposes)
17
+ x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(inputs)
18
+ x = tf.keras.layers.MaxPooling2D((2, 2))(x)
19
+ x = tf.keras.layers.Flatten()(x)
20
+ outputs = tf.keras.layers.Dense(len(train_info), activation='softmax')(x)
21
+ model = tf.keras.Model(inputs, outputs)
22
+ return model
23
+
24
+ # Initialize the mock model
25
+ model = create_mock_model()
26
+
27
  # Function to preprocess the image and make predictions
28
  def predict_image(image):
29
+ # Resize the image to 224x224 and normalize
30
  img = cv2.resize(image, (224, 224))
31
  img = img / 255.0 # Normalize to [0, 1] range
32
 
33
+ # Make predictions using the mock 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