Update README.md
Browse files
README.md
CHANGED
|
@@ -2,4 +2,27 @@
|
|
| 2 |
license: mit
|
| 3 |
library_name: transformers
|
| 4 |
pipeline_tag: image-to-text
|
| 5 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: mit
|
| 3 |
library_name: transformers
|
| 4 |
pipeline_tag: image-to-text
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 9 |
+
|
| 10 |
+
processor = AutoProcessor.from_pretrained("trunks/blip-image-captioning-base")
|
| 11 |
+
model = BlipForConditionalGeneration.from_pretrained("trunks/blip-image-captioning-base")
|
| 12 |
+
|
| 13 |
+
#prepare image for model
|
| 14 |
+
from PIL import Image
|
| 15 |
+
from IPython.display import display
|
| 16 |
+
|
| 17 |
+
img1 = Image.open("imagepath/img.jpeg")
|
| 18 |
+
width, height = img1.size
|
| 19 |
+
img1_resized = img1.resize((int(0.3 * width), int(0.3 * height))
|
| 20 |
+
display(img1_resized)
|
| 21 |
+
|
| 22 |
+
# testing image
|
| 23 |
+
inputs = processor(images=img1, return_tensors="pt")
|
| 24 |
+
pixel_values = inputs.pixel_values
|
| 25 |
+
|
| 26 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
| 27 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 28 |
+
print(generated_caption)
|