brxerq commited on
Commit
34d63c2
·
verified ·
1 Parent(s): 015d171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -5,12 +5,20 @@ import numpy as np
5
  import tensorflow_hub as hub
6
  from tensorflow.keras.models import load_model
7
  import cv2
 
 
 
 
8
 
9
  # Define a dictionary to map the custom layer to its implementation
10
  custom_objects = {'KerasLayer': hub.KerasLayer}
11
 
12
  # Load your model (ensure the path is correct)
13
- model = load_model('bird_model.h5', custom_objects=custom_objects)
 
 
 
 
14
 
15
  # Define your class labels or categories for predictions
16
  train_info = [] # Replace with your actual class labels
@@ -19,19 +27,19 @@ train_info = [] # Replace with your actual class labels
19
  with open('labelwithspace.txt', 'r') as file:
20
  train_info = [line.strip() for line in file.read().splitlines()]
21
 
22
-
23
  def predict_image(image):
24
- img = cv2.resize(image, (224, 224))
25
- img = img / 255.0
26
- predictions = model.predict(img[np.newaxis, ...])[0]
27
- top_classes = np.argsort(predictions)[-3:][::-1]
28
- top_class = top_classes[0] # Get the index of the top prediction
29
  label = train_info[top_class] # Use the index to retrieve the label
30
  return label
31
 
32
-
33
  # Define Gradio interface
34
- input_image = gr.inputs.Image(shape=(224, 224))
35
- output_label = gr.outputs.Label()
36
 
37
- gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label, capture_session=True).launch()
 
 
5
  import tensorflow_hub as hub
6
  from tensorflow.keras.models import load_model
7
  import cv2
8
+ import os
9
+
10
+ # Disable GPU (optional, for CPU-only execution)
11
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
12
 
13
  # Define a dictionary to map the custom layer to its implementation
14
  custom_objects = {'KerasLayer': hub.KerasLayer}
15
 
16
  # Load your model (ensure the path is correct)
17
+ try:
18
+ model = load_model('bird_model.h5', custom_objects=custom_objects)
19
+ except Exception as e:
20
+ print(f"Model loading failed with error: {e}")
21
+ exit(1)
22
 
23
  # Define your class labels or categories for predictions
24
  train_info = [] # Replace with your actual class labels
 
27
  with open('labelwithspace.txt', 'r') as file:
28
  train_info = [line.strip() for line in file.read().splitlines()]
29
 
30
+ # Preprocess and predict function
31
  def predict_image(image):
32
+ img = cv2.resize(image, (224, 224)) # Resize to match model input size
33
+ img = img / 255.0 # Normalize pixel values
34
+ img = img[np.newaxis, ...] # Add batch dimension
35
+ predictions = model.predict(img)[0] # Get predictions
36
+ top_class = np.argmax(predictions) # Get the index of the top prediction
37
  label = train_info[top_class] # Use the index to retrieve the label
38
  return label
39
 
 
40
  # Define Gradio interface
41
+ input_image = gr.Image(shape=(224, 224), label="Input Image")
42
+ output_label = gr.Label(label="Predicted Bird Species")
43
 
44
+ # Launch Gradio
45
+ gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label).launch()