-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
54 lines (44 loc) · 1.53 KB
/
Copy pathtest.py
File metadata and controls
54 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
import requests
print(torch.__version__)
print(torch.cuda.is_available())
from sentence_transformers import SentenceTransformer
from tqdm import tqdm
# from PIL import Image
# image_url= "https://upload.wikimedia.org/wikipedia/commons/9/9a/Cape_may.jpg"
# # download and show image
# # img = PIL.Image.open(image_url)
# data=requests.get(image_url).content
# f = open('img.jpg','wb')
# f.write(data)
# f.close()
# img = Image.open(r"img.jpg")
# img.show()
#
import clip
import os
from PIL import Image
import numpy as np
model = SentenceTransformer('clip-ViT-B-32')
device = "cuda" if torch.cuda.is_available() else "cpu"
# model, preprocess = clip.load('ViT-B/32', device)
# Define the directory path containing the images
image_directory = 'flickr30k_images/flickr30k_images'
# Initialize an empty list to store image embeddings and metadata
image_data = []
# Loop through the files in the directory
for filename in tqdm(os.listdir(image_directory), desc="Processing images", unit="image"):
if filename.endswith(".jpg"):
img_path = os.path.join(image_directory, filename)
img = Image.open(img_path)
# Encode the image using the model
img_emb = model.encode(img)
# img_emb /= img_emb.norm(dim=-1, keepdim=True)
# Create a tuple with identifier, vector, and metadata
image_tuple = (
filename,
img_emb,
{"type": "jpg"}
)
# Append the tuple to the image_data list
image_data.append(image_tuple)