-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_cap.py
26 lines (19 loc) · 891 Bytes
/
image_cap.py
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
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, DONT FORGET TO WRITE YOUR IMAGE NAME
img_path = "313218468_6263500497010521_7500648017534655591_n.jpg"
# 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)