multi-modal processing natively - it could process both text and image with the same programming model and observe in the same user flow(同じプログラミングモデルでテキストと画像の両方を処理し、同じユーザフローで観察する) ( ) ココインデックス ココナッツ ココインデックス ココナッツ このブログでは、CocoIndexを使用してスケーラブルな顔認識パイプラインを構築するための包括的な例を参照します. We will show how to extract and embed faces from images, structure the data relationally, and export everything into a vector database for real-time querying. 制限ボックスに基づいて画像の特定のセクションを視覚化し、AI抽出を理解し、評価しやすくする - 構造化されていないビジュアルデータの文脈でコンピュータ化された機能をシームレスに付加します。 ココナッツ ココナッツ If you find this tutorial helpful, we’d greatly appreciate it if you could ⭐ star . CocoIndex on GitHub CocoIndex on GitHub 使用ケース 写真検索 Face Based Access Control and Surveillance(顔ベースのアクセス制御と監視) Visual Deduplication and Identity Detection(ビジュアルデダプライクションとアイデンティティ検出) 人や顔のアイデンティティを含む多様な検索 写真からのソーシャルグラフ分析 われわれが達成するもの この会議の参加者の写真は時には , for its depiction of the world's leading physicists gathered together in one shot ( 世界の主要な物理学者たちが1つのショットで集まったその描写のために ) ) 「The Most Intelligent Picture Ever Taken」 Wikipedia について 以下、我々が達成したいこと: 画像のすべての顔を検出し、その境界ボックスを抽出 Crop and encode each face image into a 128-dimensional face embedding. 各顔画像を128次元の顔埋め込みにコードする。 構造化されたインデックスにメタデータとベクターを格納して、「この人と似たすべての顔を見つける」または「この人を含む画像を検索」などのクエリをサポートします。 あなたは完全なコードを見つけることができます . ここ インデックスフロー 画像のリストを追加しました。 For each image, we: Extract faces from the image. Compute embeddings for each face. We export the following fields to a table in Postgres with PGVector: Filename, rect, embedding for each face. コア部品 画像入力 We monitor an built-in を使用するディレクトリ すべての新しく追加されたファイルは自動的に処理され、インデックスされます。 images/ LocalFile python CopyEdit @cocoindex.flow_def(name="FaceRecognition") def face_recognition_flow(flow_builder, data_scope): data_scope["images"] = flow_builder.add_source( cocoindex.sources.LocalFile(path="images", binary=True), refresh_interval=datetime.timedelta(seconds=10), ) これでテーブルを作るのは、 そして フィールド filename content あなたはそれをあなたの (SQSの統合は、 )または . S3バケット 例 Azure Blob ストア 顔の検出と抽出 We use the キャップの下にある図書館は、dlibのCNNベースの顔検出器によって動作します。モデルが大きな画像で遅いので、検出前に幅広い画像をスケールダウンします。 face_recognition @cocoindex.op.function( cache=True, behavior_version=1, gpu=True, arg_relationship=(cocoindex.op.ArgRelationship.RECTS_BASE_IMAGE, "content"), ) def extract_faces(content: bytes) -> list[FaceBase]: orig_img = Image.open(io.BytesIO(content)).convert("RGB") # The model is too slow on large images, so we resize them if too large. if orig_img.width > MAX_IMAGE_WIDTH: ratio = orig_img.width * 1.0 / MAX_IMAGE_WIDTH img = orig_img.resize( (MAX_IMAGE_WIDTH, int(orig_img.height / ratio)), resample=Image.Resampling.BICUBIC, ) else: ratio = 1.0 img = orig_img # Extract face locations. locs = face_recognition.face_locations(np.array(img), model="cnn") faces: list[FaceBase] = [] for min_y, max_x, max_y, min_x in locs: rect = ImageRect( min_x=int(min_x * ratio), min_y=int(min_y * ratio), max_x=int(max_x * ratio), max_y=int(max_y * ratio), ) # Crop the face and save it as a PNG. buf = io.BytesIO() orig_img.crop((rect.min_x, rect.min_y, rect.max_x, rect.max_y)).save( buf, format="PNG" ) face = buf.getvalue() faces.append(FaceBase(rect, face)) return faces 画像のコンテンツを変換する: with data_scope["images"].row() as image: image["faces"] = image["content"].transform(extract_faces) このステップの後、各画像には検出された顔と境界ボックスのリストがあります。 検出されたすべての顔は、オリジナル画像から切り取られ、PNGとして保存されます。 サンプル抽出: サンプル抽出: コンピュータFace Embeddings 同じライブラリを使用して、それぞれのカットされた顔をコードします. This generates a 128-dimensional vector representation per face. @cocoindex.op.function(cache=True, behavior_version=1, gpu=True) def extract_face_embedding( face: bytes, ) -> cocoindex.Vector[cocoindex.Float32, typing.Literal[128]]: """Extract the embedding of a face.""" img = Image.open(io.BytesIO(face)).convert("RGB") embedding = face_recognition.face_encodings( np.array(img), known_face_locations=[(0, img.width - 1, img.height - 1, 0)], )[0] return embedding We plug the embedding function into the flow: 挿入機能をフローに接続します。 with image["faces"].row() as face: face["embedding"] = face["image"].transform(extract_face_embedding) このステップの後、私たちはインデックスする準備ができています! 収集および輸出 Embeddings 今では、各顔のための構造化されたデータを収集します:ファイル名、境界ボックス、埋め込み。 face_embeddings = data_scope.add_collector() face_embeddings.collect( id=cocoindex.GeneratedField.UUID, filename=image["filename"], rect=face["rect"], embedding=face["embedding"], ) Qdrant コレクションに輸出する: face_embeddings.export( QDRANT_COLLECTION, cocoindex.targets.Qdrant( collection_name=QDRANT_COLLECTION ), primary_key_fields=["id"], ) 今では、顔ベクターの上にコジン類似性クエリを実行できます。 CocoIndex は、他のベクトルデータベースと 1 行スイッチをサポートします。 . ポスター QUERY INDEX Face Search アプリやダッシュボードを構築できます。 新しい顔を埋め込むと、最も似た顔を見つける 写真のセットに表示されるすべての顔画像を見つける Cluster embeddings to group visually similar people. 視覚的に似た人々をグループ化する クエリインストールの際は、チェックアウト . Image Search プロジェクト 画像に一致するクエリパス上の完全な例を表示したい場合は、画面で叫びます。 . 私たちのグループ 応援 我々は絶えずより多くの例を追加し、我々のランタイムを改善しています. あなたがこの役に立つと感じた場合は, ⭐星 それを他者と共有する。 CocoIndex on GitHub 読んでくれてありがとう! あなたがどのようなパイプラインを建設しているか教えてください - 私たちはそれらを表示するのが好きです。