Spaces:
Running
Running
Create image_captioning.py
Browse files
application/utils/image_captioning.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
class ImageCaptioning:
|
4 |
+
def __init__(self):
|
5 |
+
self.captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
6 |
+
|
7 |
+
def generate_caption(self, image_data):
|
8 |
+
try:
|
9 |
+
caption = self.captioner(image_data)[0]['generated_text']
|
10 |
+
return caption
|
11 |
+
except Exception as e:
|
12 |
+
print(f"Error during image captioning: {e}")
|
13 |
+
return "Error processing image."
|