Possible same
Browse files- utils/caption_utils.py +4 -13
utils/caption_utils.py
CHANGED
@@ -2,27 +2,18 @@ from PIL import Image
|
|
2 |
import io
|
3 |
import torch
|
4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
-
from utils.image_utils import load_image
|
6 |
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
-
|
10 |
class ImageCaptioning:
|
11 |
|
12 |
def __init__(self):
|
13 |
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
14 |
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
15 |
|
16 |
-
def get_caption(self,
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
img = self.processor(img, return_tensors="pt").to(device)
|
21 |
-
|
22 |
-
# Generating captions
|
23 |
-
output = self.model.generate(**img)
|
24 |
-
|
25 |
-
# decode the output
|
26 |
caption = self.processor.batch_decode(output, skip_special_tokens=True)[0]
|
27 |
-
|
28 |
return caption
|
|
|
2 |
import io
|
3 |
import torch
|
4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
5 |
|
6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
|
|
|
8 |
class ImageCaptioning:
|
9 |
|
10 |
def __init__(self):
|
11 |
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
12 |
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
13 |
|
14 |
+
def get_caption(self, image_bytes):
|
15 |
+
img = Image.open(io.BytesIO(image_bytes))
|
16 |
+
img_tensors = self.processor(img, return_tensors="pt").to(device)
|
17 |
+
output = self.model.generate(**img_tensors)
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
caption = self.processor.batch_decode(output, skip_special_tokens=True)[0]
|
|
|
19 |
return caption
|