Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitignore +0 -0
- captioning/__init__.py +0 -0
- captioning/image_caption.py +14 -0
- captioning/url_caption.py +0 -0
.gitignore
ADDED
|
File without changes
|
captioning/__init__.py
ADDED
|
File without changes
|
captioning/image_caption.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import numpy as np
|
| 3 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 4 |
+
|
| 5 |
+
# Load pretrained processor and model (shared for all functions)
|
| 6 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
+
|
| 9 |
+
def caption_image(input_image: np.ndarray):
|
| 10 |
+
raw_image = Image.fromarray(input_image).convert('RGB')
|
| 11 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 12 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 13 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 14 |
+
return caption
|
captioning/url_caption.py
ADDED
|
File without changes
|