-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_embedding_loader_kg.py
More file actions
63 lines (49 loc) · 1.87 KB
/
image_embedding_loader_kg.py
File metadata and controls
63 lines (49 loc) · 1.87 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
55
56
57
58
59
60
61
62
63
import json
from neo4j import GraphDatabase
from dotenv import load_dotenv
import os
from tqdm import tqdm
load_dotenv()
def connect_to_neo4j():
uri = os.getenv("NEO4J_URI")
user = os.getenv("NEO4J_USER")
password = os.getenv("NEO4J_PASSWORD")
try:
driver = GraphDatabase.driver(uri, auth=(user, password))
return driver
except Exception as e:
print(f"Errore di connessione a Neo4j: {e}")
return None
if __name__ == "__main__":
driver = connect_to_neo4j()
if driver is None:
exit(1)
with driver.session(database=os.getenv("NEO4J_DATABASE")) as session:
print("Caricamento delle immagini e dei relativi embedding in Neo4j...")
with open('image_embeddings_avg.json', 'r', encoding='utf-8') as f:
image_embeddings = json.load(f)
query = """
UNWIND $rows AS row
// Troviamo il giocatore corrispondente in base a playerId
MATCH (p:Player {playerId: row.player_id})
// Creiamo o aggiorniamo la proprietà imageEmbedding
SET p.imageEmbedding = row.embedding,
p.imageFolder = row.image_folder
RETURN count(p) AS count
"""
rows_to_insert = [
{
"player_id": player_id,
"embedding": embedding_data["embedding"],
"image_folder": embedding_data["image_folder"]
}
for player_id, embedding_data in image_embeddings.items()
]
with driver.session(database=os.getenv("NEO4J_DATABASE")) as session:
batch_size = 200
for i in tqdm(range(0, len(rows_to_insert), batch_size), desc="Inserimento in batch"):
batch = rows_to_insert[i:i + batch_size]
result = session.run(query, rows=batch)
print("Caricamento completato.")
driver.close()
print("Connessione a Neo4j chiusa.")