Spaces:
Sleeping
Sleeping
File size: 998 Bytes
41776fc 690eb2b 20aca7b 690eb2b 41776fc 690eb2b 41776fc 690eb2b 41776fc 4a72caf 690eb2b 20aca7b 41776fc 690eb2b 4a72caf |
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 |
import gradio as gr
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from PIL import Image
import numpy as np
# Load your model
model = load_model('cats_and_dogs_classifier.h5')
# Define prediction function
def predict(image):
# Ensure the image is a PIL Image object
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Resize image to match model's expected sizing (150x150 pixels)
img = image.resize((150, 150))
# Convert image to array and expand dimensions to fit model input requirements
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
# Predict using loaded model
prediction = model.predict(img_array)
# Convert prediction to label
class_label = 'Cat' if prediction[0][0] < 0.5 else 'Dog'
return class_label
# Create a Gradio interface
interface = gr.Interface(fn=predict, inputs="image", outputs="text")
interface.launch()
|