MCPcopy
hub / github.com/serengil/deepface / insert_embeddings

Method insert_embeddings

deepface/modules/database/pgvector.py:168–252  ·  view source on GitHub ↗

Insert multiple embeddings into PostgreSQL. Args: embeddings (List[Dict[str, Any]]): List of embeddings to insert. batch_size (int): Number of embeddings to insert per batch. Returns: int: Number of embeddings inserted.

(self, embeddings: List[Dict[str, Any]], batch_size: int = 100)

Source from the content-addressed store, hash-verified

166 _SCHEMA_CHECKED[table_name] = True
167
168 def insert_embeddings(self, embeddings: List[Dict[str, Any]], batch_size: int = 100) -> int:
169 """
170 Insert multiple embeddings into PostgreSQL.
171 Args:
172 embeddings (List[Dict[str, Any]]): List of embeddings to insert.
173 batch_size (int): Number of embeddings to insert per batch.
174 Returns:
175 int: Number of embeddings inserted.
176 """
177 if not embeddings:
178 raise ValueError("No embeddings to insert.")
179
180 self.initialize_database(
181 model_name=embeddings[0]["model_name"],
182 detector_backend=embeddings[0]["detector_backend"],
183 aligned=embeddings[0]["aligned"],
184 l2_normalized=embeddings[0]["l2_normalized"],
185 )
186
187 table_name = self.__generate_table_name(
188 model_name=embeddings[0]["model_name"],
189 detector_backend=embeddings[0]["detector_backend"],
190 aligned=embeddings[0]["aligned"],
191 l2_normalized=embeddings[0]["l2_normalized"],
192 )
193
194 query = f"""
195 INSERT INTO {table_name} (
196 img_name,
197 face,
198 face_shape,
199 model_name,
200 detector_backend,
201 aligned,
202 l2_normalized,
203 embedding,
204 face_hash,
205 embedding_hash
206 )
207 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
208 """
209
210 values = []
211 for e in embeddings:
212 face = e["face"]
213 face_shape = list(face.shape)
214 face_bytes = face.astype(np.float32).tobytes()
215 face_json = json.dumps(face.tolist())
216
217 embedding_bytes = struct.pack(f'{len(e["embedding"])}d', *e["embedding"])
218
219 # uniqueness is guaranteed by face hash and embedding hash
220 face_hash = hashlib.sha256(face_json.encode()).hexdigest()
221 embedding_hash = hashlib.sha256(embedding_bytes).hexdigest()
222
223 values.append(
224 (
225 e["img_name"],

Callers

nothing calls this directly

Calls 4

initialize_databaseMethod · 0.95
__generate_table_nameMethod · 0.95
DuplicateEntryErrorClass · 0.90
warnMethod · 0.80

Tested by

no test coverage detected