-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_captioner.py
31 lines (24 loc) · 1.08 KB
/
image_captioner.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
27
28
29
30
31
from io import BytesIO
from PIL import Image
import requests
from transformers import BlipProcessor, BlipForConditionalGeneration
# Initialize the BLIP processor and model
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def generate_caption(image_url):
"""
Generate a caption for the given image URL using the BLIP model.
"""
try:
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
if image.size[0] * image.size[1] < 400: # Skip small images
return None
image = image.convert('RGB') # Ensure the image is in RGB format
inputs = processor(image, return_tensors="pt", padding=True)
out = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(out[0], skip_special_tokens=True)
return caption
except Exception as e:
print(f"Error processing image {image_url}: {e}")
return f"Error processing image {image_url}"