Spaces:
Runtime error
Runtime error
File size: 890 Bytes
0575765 ff9a698 b860d7b 6306010 b860d7b c0c295c b860d7b c0c295c b860d7b |
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 |
# installing required libraries in my_env
import requests
from PIL import Image
from transformers import AutoProcessor, BlipForConditionalGeneration
# Load the pretrained processor and model
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
# Load your image, DON'T FORGET TO WRITE YOUR IMAGE NAME
img_path = "demo.jpeg"
# convert it into an RGB format
image = Image.open(img_path).convert('RGB')
# You do not need a question for image captioning
text = "the image of"
inputs = processor(images=image, text=text, return_tensors="pt")
# Generate a caption for the image
outputs = model.generate(**inputs, max_length=50)
# Decode the generated tokens to text
caption = processor.decode(outputs[0], skip_special_tokens=True)
# Print the caption
print(caption) |