Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating test for image captioning using data from the database #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions backend/app/image_captioning/caption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration



def caption1(img):
processor = BlipProcessor.from_pretrained("salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("salesforce/blip-image-captioning-base")


img_input = Image.fromarray(img)
inputs = processor(img_input, return_tensors="pt")
out = model.generate(**inputs)
caption = processor.decode(out[0], skip_special_token=True)
return caption
27 changes: 27 additions & 0 deletions backend/app/image_captioning/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import sqlite3
import numpy as np
from PIL import Image
from app.image_captioning.caption import caption1
from app.config.settings import IMAGES_DATABASE_PATH


def captioning_images():
conn = sqlite3.connect(IMAGES_DATABASE_PATH)
cursor = conn.cursor()

cursor.execute("SELECT path FROM image_id_mapping;")
paths = [row[0] for row in cursor.fetchall()]

conn.close()
captions = []
for i in paths:
img = Image.open(os.path.abspath(i))
img = np.array(img)
caption = caption1(img)
print(f"{os.path.abspath(i)}:{caption}")
captions.append(caption)
print(captions)
print(paths)

captioning_images()