File size: 1,214 Bytes
feeed5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from tensorflow.keras.models import load_model
import numpy as np
import gradio as gr
import tensorflow as tf
from io import StringIO
from PIL import Image
import requests

url = "https://drive.usercontent.google.com/download?id=1T5HGnk9Mxlb5G6FTxp26BWSrTpzbjtP2&export=download&authuser=0"
open("models.h5", "wb").write(requests.get(url).content)
labels = []
model = load_model('/content/models.h5')
with open("/content/name of the animals.txt") as f:
    for line in f:
        labels.append(line.replace('\n', ''))

def classify_image(inp):
    # Create a copy of the input array to avoid reference issues
    inp_copy = np.copy(inp)
    # Resize the input image to the expected shape (224, 224)
    inp_copy = Image.fromarray(inp_copy)
    inp_copy = inp_copy.resize((224, 224))
    inp_copy = np.array(inp_copy)
    inp_copy = inp_copy.reshape((-1, 224, 224, 3))
    inp_copy = tf.keras.applications.efficientnet.preprocess_input(inp_copy)
    prediction = model.predict(inp_copy).flatten()
    confidences = {labels[i]: float(prediction[i]) for i in range(90)}
    return confidences

demo = gr.Interface(classify_image, gr.Image(), gr.Label(num_top_classes=3))
if __name__ == "__main__":
    demo.launch()