|
import gradio as gr
|
|
import numpy as np
|
|
from tensorflow.keras.models import load_model
|
|
from tensorflow.keras.preprocessing import image as keras_image
|
|
from tensorflow.image import resize
|
|
|
|
|
|
model = load_model("trained_model_10.h5")
|
|
|
|
|
|
label_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
|
|
|
|
|
|
def recognize_image(image):
|
|
|
|
img = keras_image.img_to_array(image)
|
|
img = resize(img, (32, 32))
|
|
img = np.expand_dims(img, axis=0)
|
|
img = img / 255.0
|
|
|
|
|
|
pred = model.predict(img)
|
|
final_pred = np.argmax(pred, axis=1)
|
|
|
|
|
|
result = {label_names[i]: float(pred[0][i]) for i in range(len(label_names))}
|
|
|
|
return result
|
|
|
|
|
|
image_input = gr.Image()
|
|
label_output = gr.Label(num_top_classes=5)
|
|
|
|
|
|
examples = [
|
|
'image_1.jpeg',
|
|
'image_2.jpg',
|
|
'image_4.jpeg',
|
|
'image_5.jpg',
|
|
'image_7.jpg',
|
|
'image_8.jpeg'
|
|
]
|
|
|
|
|
|
iface = gr.Interface(fn=recognize_image, inputs=image_input, outputs=label_output, examples=examples)
|
|
|
|
|
|
iface.launch(inline=False)
|
|
|