-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaption_model.py
More file actions
17 lines (14 loc) · 791 Bytes
/
caption_model.py
File metadata and controls
17 lines (14 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import torch
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def generate_caption(image_path):
image = Image.open(image_path).convert('RGB')
inputs = processor(image, return_tensors="pt")
out = model.generate(**inputs, num_return_sequences=1, output_scores=True, return_dict_in_generate=True)
caption = processor.decode(out.sequences[0], skip_special_tokens=True)
# Confidence estimation (approx.)
probs = torch.nn.functional.softmax(out.scores[0], dim=-1)
confidence = torch.max(probs).item()
return caption, round(confidence * 100, 2)