Sa-m's picture
Update app.py
72a21b5
raw
history blame
2.06 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow_addons as tfa
import os
import numpy as np
HEIGHT,WIDTH=224,224
IMG_SIZE=224
model=load_model('Models/best_model1.h5')
# def classify_image(inp):
# np.random.seed(143)
# inp = inp.reshape((-1, HEIGHT,WIDTH, 3))
# inp = tf.keras.applications.nasnet.preprocess_input(inp)
# prediction = model.predict(inp)
# ###label = dict((v,k) for k,v in labels.items())
# predicted_class_indices=np.argmax(prediction,axis=1)
# result = {}
# for i in range(len(predicted_class_indices)):
# if predicted_class_indices[i] < NUM_CLASSES:
# result[labels[predicted_class_indices[i]]]= float(predicted_class_indices[i])
# return result
# def classify_image(inp):
# np.random.seed(143)
# labels = {'Cat': 0, 'Dog': 1}
# NUM_CLASSES = 2
# #inp = inp.reshape((-1, HEIGHT, WIDTH, 3))
# #inp = tf.keras.applications.nasnet.preprocess_input(inp)
# prediction = model.predict(inp)
# predicted_class_indices = np.argmax(prediction, axis=1)
# label_order = ["Cat","Dog"]
# result = {label: float(f"{prediction[0][labels[label]]:.6f}") for label in label_order}
# return result
def classify_image(inp):
NUM_CLASSES=2
# Resize the image to the required size
labels = ['Cat','Dog']
inp = tf.image.resize(inp, [IMG_SIZE, IMG_SIZE])
inp = inp.numpy()
inp = inp.reshape((-1, IMG_SIZE, IMG_SIZE, 3))
inp = tf.keras.applications.vgg16.preprocess_input(inp)
prediction = model.predict(inp).flatten()
return {labels[i]: f"{prediction[i]:.6f}" for i in range(NUM_CLASSES)}
image = gr.Image(height=HEIGHT,width=WIDTH,label='Input')
label = gr.Label(num_top_classes=2)
examples = [
["Examples/img1.png"],
["Examples/img2.png"],
["Examples/img3.png"],
["Examples/img4.png"]
]
gr.Interface(
fn=classify_image,
inputs=image,
outputs=label,
title='Smart Pet Classifier',
examples=examples
).launch(debug=False)