File size: 1,291 Bytes
0cb8d44
 
 
 
 
 
7c8ef8f
0cb8d44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10e834
0cb8d44
 
 
 
 
d10e834
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
import tensorflow as tf
import numpy as np
from PIL import Image
import gradio as gr

# CNN+LSTM model loaded
model = tf.keras.models.load_model("model.h5")

# Define the preprocessing function for the image
def preprocess_image(image):
    # Resize and normalize image to match model's expected input shape
    image = image.resize((224, 224))  # Modify size based on your model
    image_array = np.array(image) / 255.0  # Normalize
    image_array = np.expand_dims(image_array, axis=0)  # Add batch dimension
    return image_array

# Define the function that generates a caption from the image
def generate_caption(image):
    preprocessed_image = preprocess_image(image)
    
    # Generate a caption from the model
    # Note: Adjust this if your model requires a sequence start token or has a decoding loop
    caption_tokens = model.predict(preprocessed_image)

    return caption_tokens

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_caption,          # The function that generates captions
    inputs=gr.Image(type="pil"),  # Accept an image input
    outputs="text",                # Output a text caption
    title="Image Captioning Model",
    description="Upload an image, and the model will generate a caption describing it."
)

iface.launch(share=True)