-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_embeddings.py
33 lines (28 loc) · 1.02 KB
/
generate_embeddings.py
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
import numpy as np
import pickle
import torch
import torchvision.transforms as transforms
from PIL import Image
from facenet_pytorch import InceptionResnetV1
# Initialize FaceNet model
model = InceptionResnetV1(pretrained='vggface2').eval() # Load pretrained model
# Define function to generate face embeddings
def generate_face_embedding(image_path):
img = Image.open(image_path)
img = transforms.ToTensor()(img).unsqueeze(0)
with torch.no_grad():
embedding = model(img)[0].numpy()
return embedding
# Example characters and their image paths
character_images = {
"character_id_1": "images\character_id_1.jpg",
# Add more characters as needed
}
# Generate embeddings for each character
character_embeddings = {}
for character_id, image_path in character_images.items():
embedding = generate_face_embedding(image_path)
character_embeddings[character_id] = embedding
# Save embeddings for later use
with open('embeddings\character_embeddings.pkl', 'wb') as f:
pickle.dump(character_embeddings, f)