|
""" |
|
Author: Rajanikanta Mohapatra |
|
Date: 01-04-2024 |
|
|
|
Description: This Streamlit app generates captions for uploaded images using a finetuned BLIP model. |
|
""" |
|
|
|
|
|
import streamlit as st |
|
from transformers import AutoProcessor, BlipForConditionalGeneration |
|
from PIL import Image |
|
import os |
|
|
|
|
|
st.set_page_config(page_title="Caption Generator App", page_icon="📷") |
|
|
|
|
|
saved_folder_path = "saved_model" |
|
if not os.path.exists(saved_folder_path): |
|
os.mkdir(saved_folder_path) |
|
|
|
|
|
processor = AutoProcessor.from_pretrained(saved_folder_path) |
|
model = BlipForConditionalGeneration.from_pretrained(saved_folder_path) |
|
|
|
|
|
|
|
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. |
|
""" |
|
|
|
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 |
|
|
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
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}") |
|
|
|
|
|
|