brxerq commited on
Commit
ac55eb6
1 Parent(s): 28d3614

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ import gradio as gr
5
+ import numpy as np
6
+ import tensorflow_hub as hub
7
+ from tensorflow.keras.models import load_model
8
+ import cv2
9
+
10
+ # Define a dictionary to map the custom layer to its implementation
11
+ custom_objects = {'KerasLayer': hub.KerasLayer}
12
+
13
+ # Load your model (ensure the path is correct)
14
+ model = load_model('bird_model.h5', custom_objects=custom_objects)
15
+
16
+ # Define your class labels or categories for predictions
17
+ train_info = [] # Replace with your actual class labels
18
+
19
+ # Read image names from the text file
20
+ with open('label.txt', 'r') as file:
21
+ train_info = [line.strip() for line in file.read().splitlines()]
22
+
23
+
24
+ def predict_image(image):
25
+ img = cv2.resize(image, (224, 224))
26
+ img = img / 255.0
27
+ predictions = model.predict(img[np.newaxis, ...])[0]
28
+ top_classes = np.argsort(predictions)[-3:][::-1]
29
+ top_class = top_classes[0] # Get the index of the top prediction
30
+ label = train_info[top_class] # Use the index to retrieve the label
31
+ return label
32
+
33
+
34
+ # Define Gradio interface
35
+ input_image = gr.inputs.Image(shape=(224, 224))
36
+ output_label = gr.outputs.Label()
37
+
38
+ gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label, capture_session=True).launch()