-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexer.py
More file actions
35 lines (27 loc) · 1.16 KB
/
Indexer.py
File metadata and controls
35 lines (27 loc) · 1.16 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
import torch
import clip
from PIL import Image
import os
import pickle
class Indexer:
def __init__(self):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model, self.preprocess = clip.load("ViT-B/32", device=self.device)
def load_and_preprocess_image(self, image_path):
image = Image.open(image_path).convert("RGB")
return self.preprocess(image).unsqueeze(0).to(self.device)
def create_image_embeddings(self, directory):
image_paths = [os.path.join(directory, filename) for filename in os.listdir(directory) if filename.endswith(('.png', '.jpg', '.jpeg'))]
image_embeddings = []
names = []
for path in image_paths:
image = self.load_and_preprocess_image(path)
with torch.no_grad():
embedding = self.model.encode_image(image)
image_embeddings.append(embedding)
names.append(path)
# save both the embeddings and paths
with open('image_embeddings.pkl', 'wb') as f:
pickle.dump({'embeddings': torch.cat(image_embeddings), 'paths': names}, f)
#index = Indexer()
#index.create_image_embeddings("img/")