Bird_Species / app.py
osamaifti's picture
Upload app.py
a93dd97
# -*- coding: utf-8 -*-
"""Bird_Species_Interface.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1phGfuDAxvDjzxX7jYYCg92VjPhua9u1_
"""
import gradio as gr
import numpy as np
import tensorflow_hub as hub
import tensorflow as tf
from tensorflow.keras.models import load_model
import cv2
import gradio as gr
import tensorflow as tf
import cv2
# Define a dictionary to map the custom layer to its implementation
custom_objects = {'KerasLayer': hub.KerasLayer}
# Load your model (ensure the path is correct) and provide the custom_objects dictionary
model = tf.keras.models.load_model('model.h5', custom_objects=custom_objects)
# Define a function to preprocess the image
def preprocess_image(image):
img = cv2.resize(image, (224, 224))
img = img / 255.0 # Normalize pixel values to [0, 1]
return img
# Define the prediction function
def predict_image(image):
img = preprocess_image(image)
img = img[np.newaxis, ...] # Add batch dimension
prediction = model.predict(img)
predicted_class = tf.argmax(prediction, axis=1).numpy()[0]
confidence = tf.reduce_max(prediction).numpy()
return f"Class: {predicted_class}, Confidence: {confidence:.4f}"
# Define Gradio interface
input_image = gr.inputs.Image(shape=(224, 224))
output_label = gr.outputs.Label()
gr.Interface(
fn=predict_image,
inputs=input_image,
outputs=output_label,
live=True
).launch()