ImageFileToDocument

ImageFileToDocument

이미지 파일 참조를 연관 메타데이터가 있는 빈 Document 객체로 변환해요.

출처: 문서

본문

ImageFileToDocument는 이미지 파일 소스를 연관 메타데이터가 있는 빈 Document 객체로 변환해요. 이 컴포넌트는 이미지 파일 경로를 Document 객체로 감싸서 SentenceTransformersDocumentImageEmbedder나 LLMDocumentContentExtractor 같은 다운스트림 컴포넌트가 처리하게 해야 하는 파이프라인에서 유용해요.

이미지 파일에서 콘텐츠를 추출하지 않고, 콘텐츠가 None인 Document 객체를 만들고 파일 경로와 사용자 제공 값 같은 메타데이터를 첨부해요. 각 소스는 다음 중 하나가 될 수 있어요:

  • 파일 경로(string 또는 Path)
  • ByteStream 객체

선택적으로 meta 파라미터로 메타데이터를 제공할 수 있어요. 이것은 단일 딕셔너리(모든 문서에 적용) 또는 sources 길이와 맞는 리스트일 수 있어요.

  • 대표적인 파이프라인 위치: SentenceTransformersDocumentImageEmbedder나 LLMDocumentContentExtractor 같은 이미지를 처리하는 컴포넌트 앞
  • 필수 run 변수: sources — 이미지 파일 경로 또는 ByteStream 리스트
  • 출력 변수: documents — 연관 메타데이터가 있는 빈 Document 객체 리스트
  • API reference: Image Converters
  • 패키지명: haystack-ai

Usage ​

On its own ​

이 컴포넌트는 주로 파이프라인에서 사용하기 위한 것이에요.

from haystack.components.converters.image import ImageFileToDocument

converter = ImageFileToDocument()
sources = ["image.jpg", "another_image.png"]
result = converter.run(sources=sources)
documents = result["documents"]
print(documents)
# [Document(id=..., content=None, meta={'file_path': 'image.jpg'}),
# Document(id=..., content=None, meta={'file_path': 'another_image.png'})]

In a pipeline ​

다음 파이프라인에서 ImageFileToDocument 컴포넌트로 이미지 문서를 만들고, 이미지 임베딩으로 풍부하게 만든 뒤 Document Store에 저장해요. 이 페이지의 예시는 sentence-transformers-haystack 패키지의 Sentence Transformers 임베더를 사용해요. 예시를 실행하려면 설치하세요:

pip install sentence-transformers-haystack
from haystack import Pipeline
from haystack.components.converters.image import ImageFileToDocument
from haystack_integrations.components.embedders.sentence_transformers import (
    SentenceTransformersDocumentImageEmbedder,
)
from haystack.components.writers.document_writer import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore

# Create our document store
doc_store = InMemoryDocumentStore()

# Define pipeline with components
indexing_pipe = Pipeline()
indexing_pipe.add_component(
    "image_converter",
    ImageFileToDocument(store_full_path=True),
)
indexing_pipe.add_component(
    "image_doc_embedder",
    SentenceTransformersDocumentImageEmbedder(),
)
indexing_pipe.add_component("document_writer", DocumentWriter(doc_store))
indexing_pipe.connect("image_converter.documents", "image_doc_embedder.documents")
indexing_pipe.connect("image_doc_embedder.documents", "document_writer.documents")
indexing_result = indexing_pipe.run(
    data={"image_converter": {"sources": ["apple.jpg", "kiwi.png"]}},
)
indexed_documents = doc_store.filter_documents()
print(f"Indexed {len(indexed_documents)} documents")
# Indexed 2 documents

더 알아보기 (Learn more)

🧑‍🍳 Cookbook: Introduction to Multimodality