Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing import image | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# Load the model | |
model = tf.keras.models.load_model('dogcat_model_bak.h5') | |
def classify_image(input_image): | |
# Load and preprocess the image | |
img1 = image.load_img(input_image.name, target_size=(64, 64)) | |
img2 = image.load_img(input_image.name) | |
img = image.img_to_array(img1) | |
img = img / 255.0 | |
img = np.expand_dims(img, axis=0) | |
# Make prediction | |
prediction = model.predict(img) | |
# Display prediction result on the image | |
if prediction[0][0] > 0.5: | |
value = 'Dog: %1.2f' % prediction[0][0] | |
plt.text(20, 62, value, color='red', fontsize=18, bbox=dict(facecolor='white', alpha=0.8)) | |
else: | |
value = 'Cat: %1.2f' % (1.0 - prediction[0][0]) | |
plt.text(20, 62, value, color='red', fontsize=18, bbox=dict(facecolor='white', alpha=0.8)) | |
plt.imshow(img2) | |
plt.axis('off') # Hide axis for better visualization | |
plt.show() | |
return img2 # Return the image with prediction annotations | |
# Interface creation using Gradio | |
inputs = gr.inputs.Image() | |
outputs = gr.outputs.Image() | |
interface = gr.Interface(classify_image, inputs, outputs, capture_session=True) | |
# Launch the Gradio app | |
interface.launch() |