Spaces:
Sleeping
Sleeping
File size: 1,109 Bytes
c924d23 69ea569 c924d23 69ea569 c924d23 48ff5df de16072 48ff5df c924d23 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import gradio as gr
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
# Load your pre-trained model
model_path = 'my_model.h5'
model = load_model(model_path)
# Function to make predictions
def predict_image_to_text(image):
# Preprocess the input image (adjust based on your model's requirements)
img = Image.open(image)
# Resize the image to (224, 224)
img = img.resize((224, 224))
# Convert the image to a NumPy array
img_array = np.array(img)
# Ensure the image has 3 channels (R, G, B)
if img_array.shape[-1] == 4:
img_array = img_array[:, :, :3]
# Normalize the image data if needed
img_array = img_array / 255.0
# Add batch dimension
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
prediction = model.predict(img_array)[0]
return prediction
# Gradio Interface
iface = gr.Interface(
fn=predict_image_to_text,
inputs=gr.Image(), # Gradio will automatically handle image input
outputs="text",
live=True,
)
# Launch the Gradio interface
iface.launch()
|