import gradio as gr import tensorflow as tf from tensorflow.keras.preprocessing import image import matplotlib.pyplot as plt import numpy as np from PIL import Image as PIL_Image # Load the model model = tf.keras.models.load_model('dogcat_model_bak.h5') def classify_image(input_image): pil_img = PIL_Image.fromarray(input_image, 'RGB') # Preprocess the image img1 = pil_img.resize((64, 64)) img2 = pil_img 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.Image() interface = gr.Interface(fn = classify_image, inputs=gr.Image(),outputs=classify_image(inputs)) # Load and pre, capture_session=True) # Launch the Gradio app interface.launch()