Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
!pip freeze > requirements.txt
|
11 |
+
|
12 |
+
!pip install gradio transformers
|
13 |
+
|
14 |
+
import gradio as gr
|
15 |
+
import numpy as np
|
16 |
+
import tensorflow_hub as hub
|
17 |
+
import tensorflow as tf
|
18 |
+
from tensorflow.keras.models import load_model
|
19 |
+
import cv2
|
20 |
+
|
21 |
+
import gradio as gr
|
22 |
+
import tensorflow as tf
|
23 |
+
import cv2
|
24 |
+
|
25 |
+
# Define a dictionary to map the custom layer to its implementation
|
26 |
+
custom_objects = {'KerasLayer': hub.KerasLayer}
|
27 |
+
|
28 |
+
# Load your model (ensure the path is correct) and provide the custom_objects dictionary
|
29 |
+
model = tf.keras.models.load_model('model.h5', custom_objects=custom_objects)
|
30 |
+
|
31 |
+
# Define a function to preprocess the image
|
32 |
+
def preprocess_image(image):
|
33 |
+
img = cv2.resize(image, (224, 224))
|
34 |
+
img = img / 255.0 # Normalize pixel values to [0, 1]
|
35 |
+
return img
|
36 |
+
|
37 |
+
# Define the prediction function
|
38 |
+
def predict_image(image):
|
39 |
+
img = preprocess_image(image)
|
40 |
+
img = img[np.newaxis, ...] # Add batch dimension
|
41 |
+
prediction = model.predict(img)
|
42 |
+
predicted_class = tf.argmax(prediction, axis=1).numpy()[0]
|
43 |
+
confidence = tf.reduce_max(prediction).numpy()
|
44 |
+
return f"Class: {predicted_class}, Confidence: {confidence:.4f}"
|
45 |
+
|
46 |
+
# Define Gradio interface
|
47 |
+
input_image = gr.inputs.Image(shape=(224, 224))
|
48 |
+
output_label = gr.outputs.Label()
|
49 |
+
|
50 |
+
gr.Interface(
|
51 |
+
fn=predict_image,
|
52 |
+
inputs=input_image,
|
53 |
+
outputs=output_label,
|
54 |
+
live=True
|
55 |
+
).launch()
|