File size: 910 Bytes
b155593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image

# Load the pre-trained BLIP model and processor
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def generate_blip_caption(image_path):
    """
    Generates a caption for a given image using the BLIP model.

    Args:
        image_path (str): The path to the image file.

    Returns:
        str: The generated caption.
    """
    # Open the image
    image = Image.open(image_path).convert("RGB")
    
    # Preprocess the image and generate the caption
    inputs = processor(images=image, return_tensors="pt")
    outputs = model.generate(**inputs)
    
    # Decode the generated caption
    caption = processor.decode(outputs[0], skip_special_tokens=True)
    
    return caption