File size: 1,476 Bytes
a93dd97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- 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()