Views
No views yet
Salesforce/blip-image-captioning-large, adapted for image captioning in Arabic using the Flickr8K Arabic dataset. It takes an input image and generates a relevant caption in Arabic, describing the image content.1from transformers import BlipProcessor, BlipForConditionalGeneration
2from PIL import Image
3import torch
4import matplotlib.pyplot as plt
5
6# Load model and processor
7processor = BlipProcessor.from_pretrained("omarsabri8756/blip-Arabic-flickr-8k")
8model = BlipForConditionalGeneration.from_pretrained("omarsabri8756/blip-Arabic-flickr-8k")
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model = model.to(device)
11
12# Load an image from local path
13image_path = "path/to/your/image.jpg"
14image = Image.open(image_path).convert("RGB")
15
16# Show image
17plt.imshow(image)
18plt.axis('off')
19plt.title("Input Image")
20plt.show()
21
22# Generate enhanced Arabic caption with better parameters
23model.eval()
24with torch.no_grad():
25 pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
26 generated_output = model.generate(
27 pixel_values=pixel_values,
28 max_length=75,
29 min_length=20,
30 num_beams=5,
31 repetition_penalty=1.5,
32 length_penalty=1.0,
33 no_repeat_ngram_size=3,
34 early_stopping=True
35 )
36 caption = processor.batch_decode(generated_output, skip_special_tokens=True)[0]
37 print(caption) # Prints Arabic caption