File size: 2,242 Bytes
0d33338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Author: Rajanikanta Mohapatra
Date: 01-04-2024

Description: This Streamlit app generates captions for uploaded images using a finetuned BLIP model.
"""

# Import necessary libraries
import streamlit as st
from transformers import AutoProcessor, BlipForConditionalGeneration
from PIL import Image
import os

# Set custom web page title and icon
st.set_page_config(page_title="Caption Generator App", page_icon="📷")

# Create a folder to save the model if it doesn't exist
saved_folder_path = "saved_model"
if not os.path.exists(saved_folder_path):
    os.mkdir(saved_folder_path)

# Load  processor and model
processor = AutoProcessor.from_pretrained(saved_folder_path)
model = BlipForConditionalGeneration.from_pretrained(saved_folder_path)


# Function to generate caption for the provided image
def generate_caption(image_path, target_size=(224, 224)):
    """
    Generates a caption for the provided image.

    Parameters:
    - image_path (str): Path to the image file.
    - target_size (tuple): Desired size for the image (default is (224, 224)).

    Returns:
    - generated_caption (str): Generated caption for the image.
    """
    # Process the image
    image = Image.open(image_path)
    image = image.resize(target_size)
    inputs = processor(images=image, return_tensors="pt")
    pixel_values = inputs.pixel_values
    generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
    generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_caption


# Streamlit app
st.title("Image Caption Generator Using BLIP")
st.markdown(
    "Upload an image, and this app will generate a caption for it using a finetuned BLIP model."
)

# Upload image
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])

# Process uploaded image and generate caption
if uploaded_image is not None:
    st.subheader("Uploaded Image")
    st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)
    st.subheader("Generated Caption")
    try:
        generated_caption = generate_caption(uploaded_image)
        st.write("Generated Caption:", generated_caption)
    except Exception as e:
        st.error(f"Error occurred: {e}")