Spaces:
Runtime error
Runtime error
File size: 1,346 Bytes
58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df e7a9c69 58902df |
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 |
import os
import glob
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration #Blip2 models
# Load the pretrained processor and model
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
# Specify the directory where your images are
image_dir = "/"
image_exts = ["jpg", "jpeg", "png"] # specify the image file extensions to search for
# Open a file to write the captions
with open("captions.txt", "w") as caption_file:
# Iterate over each image file in the directory
for image_ext in image_exts:
for img_path in glob.glob(os.path.join(image_dir, f"*.{image_ext}")):
# Load your image
raw_image = Image.open(img_path).convert('RGB')
# You do not need a question for image captioning
inputs = processor(raw_image, return_tensors="pt")
# Generate a caption for the image
out = model.generate(**inputs, max_new_tokens=50)
# Decode the generated tokens to text
caption = processor.decode(out[0], skip_special_tokens=True)
# Write the caption to the file, prepended by the image file name
caption_file.write(f"{os.path.basename(img_path)}: {caption}\n") |