2023-02-15 03:55:46 +01:00
|
|
|
import torch
|
2023-02-23 18:41:42 +01:00
|
|
|
from transformers import BlipForConditionalGeneration, BlipProcessor
|
2023-02-15 00:38:21 +01:00
|
|
|
|
2023-02-15 03:55:46 +01:00
|
|
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
2023-02-15 04:03:19 +01:00
|
|
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base", torch_dtype=torch.float32).to("cpu")
|
2023-02-15 03:55:46 +01:00
|
|
|
|
|
|
|
def caption_image(raw_image):
|
2023-02-15 04:03:19 +01:00
|
|
|
inputs = processor(raw_image.convert('RGB'), return_tensors="pt").to("cpu", torch.float32)
|
2023-02-15 03:55:46 +01:00
|
|
|
out = model.generate(**inputs, max_new_tokens=100)
|
|
|
|
return processor.decode(out[0], skip_special_tokens=True)
|