osamaifti commited on
Commit
a93dd97
·
1 Parent(s): dd4ebba

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Bird_Species_Interface.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1phGfuDAxvDjzxX7jYYCg92VjPhua9u1_
8
+ """
9
+
10
+
11
+ import gradio as gr
12
+ import numpy as np
13
+ import tensorflow_hub as hub
14
+ import tensorflow as tf
15
+ from tensorflow.keras.models import load_model
16
+ import cv2
17
+
18
+ import gradio as gr
19
+ import tensorflow as tf
20
+ import cv2
21
+
22
+ # Define a dictionary to map the custom layer to its implementation
23
+ custom_objects = {'KerasLayer': hub.KerasLayer}
24
+
25
+ # Load your model (ensure the path is correct) and provide the custom_objects dictionary
26
+ model = tf.keras.models.load_model('model.h5', custom_objects=custom_objects)
27
+
28
+ # Define a function to preprocess the image
29
+ def preprocess_image(image):
30
+ img = cv2.resize(image, (224, 224))
31
+ img = img / 255.0 # Normalize pixel values to [0, 1]
32
+ return img
33
+
34
+ # Define the prediction function
35
+ def predict_image(image):
36
+ img = preprocess_image(image)
37
+ img = img[np.newaxis, ...] # Add batch dimension
38
+ prediction = model.predict(img)
39
+ predicted_class = tf.argmax(prediction, axis=1).numpy()[0]
40
+ confidence = tf.reduce_max(prediction).numpy()
41
+ return f"Class: {predicted_class}, Confidence: {confidence:.4f}"
42
+
43
+ # Define Gradio interface
44
+ input_image = gr.inputs.Image(shape=(224, 224))
45
+ output_label = gr.outputs.Label()
46
+
47
+ gr.Interface(
48
+ fn=predict_image,
49
+ inputs=input_image,
50
+ outputs=output_label,
51
+ live=True
52
+ ).launch()