Extracts, embeds, and stores multimodal PDF elements — text with SentenceTransformers and images with CLIP — in vector database for unified semantic search.Extracts, embeds, and stores multimodal PDF elements — text with SentenceTransformers and images with CLIP — in vector database for unified semantic search.

How to Extract and Embed Text and Images from PDFs for Unified Semantic Search

2025/10/27 12:43

PDFs are rich with both text and visual content — from descriptive paragraphs to illustrations and tables. This example builds an end-to-end flow that parses, embeds, and indexes both, with full traceability to the original page.

In this example, we splits out both text and images, linking them back to page metadata, and enabling unified semantic search. We’ll use CocoIndex to define the flow, SentenceTransformers for text embeddings, and CLIP for image embeddings — all stored in Qdrant for retrieval.

\

🔍 What It Does

This flow automatically:

  • Extracts page texts and images from PDF files
  • Skips tiny or redundant images
  • Creates consistent thumbnails (up to 512×512)
  • Chunks and embeds text with SentenceTransformers (all-MiniLM-L6-v2)
  • Embeds images with CLIP (openai/clip-vit-large-patch14)
  • Stores both embeddings and metadata (e.g., where the text or image came from) in Qdrant, enabling unified semantic search across text and image modalities

📦 Prerequisite:

Run Qdrant

If you don’t have Qdrant running locally, start it via Docker:

docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant

📁 Input Data

We’ll use a few sample PDFs (board game manuals). Download them into the source_files directory:

./fetch_manual_urls.sh

Or, feel free to drop in any of your own PDFs.

⚙️ Run the Flow

Install dependencies:

pip install -e .

Then build your index (sets up tables automatically on first run):

cocoindex update --setup main

Or Run in CocoInsight

cocoindex server -ci main

Define the flow

Flow definition

Let’s break down what happens inside the PdfElementsEmbedding flow

@cocoindex.flow_def(name="PdfElementsEmbedding") def multi_format_indexing_flow( flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope ) -> None: data_scope["documents"] = flow_builder.add_source( cocoindex.sources.LocalFile( path="source_files", included_patterns=["*.pdf"], binary=True ) ) text_output = data_scope.add_collector() image_output = data_scope.add_collector()

We defines the flow, added source and added data collectors.

For flow definition, the decorator

@cocoindex.flow_def(name="PdfElementsEmbedding")

\ marks the function as a CocoIndex flow definition, registering it as part of the data indexing system. When executed via CocoIndex runtime, it orchestrates data ingestion, transformation, and collection.When started, CocoIndex’s runtime executes the flow in either one-time update or live update mode, and incrementally embedding only what changed.

Process Each Document

Extract PDF documents

We iterate each document row and run a custom transformation which extract pdf elements.

with data_scope["documents"].row() as doc: doc["pages"] = doc["content"].transform(extract_pdf_elements)

Extract PDF Elements

Define dataclass for structured extraction - we want to extract a list of PdfPage each has page number, text, and list of images.

@dataclass class PdfImage: name: str data: bytes @dataclass class PdfPage: page_number: int text: str images: list[PdfImage]

Next, define a CocoIndex function called extract_pdf_elements that extracts both text and images from a PDF file, returning them as structured, page-wise data objects.

@cocoindex.op.function() def extract_pdf_elements(content: bytes) -> list[PdfPage]: """ Extract texts and images from a PDF file. """ reader = PdfReader(io.BytesIO(content)) result = [] for i, page in enumerate(reader.pages): text = page.extract_text() images = [] for image in page.images: img = image.image if img is None: continue # Skip very small images. if img.width < 16 or img.height < 16: continue thumbnail = io.BytesIO() img.thumbnail(IMG_THUMBNAIL_SIZE) img.save(thumbnail, img.format or "PNG") images.append(PdfImage(name=image.name, data=thumbnail.getvalue())) result.append(PdfPage(page_number=i + 1, text=text, images=images)) return result

The extract_pdf_elements function reads a PDF file from bytes and extracts both text and images from each page in a structured way. Using pypdf, it parses every page to retrieve text content and any embedded images, skipping empty or very small ones to avoid noise.

Each image is resized to a consistent thumbnail size (up to 512×512) and converted into bytes for downstream use. The result is a clean, per-page data structure (PdfPage) that contains the page number, extracted text, and processed images — making it easy to embed and index PDFs for multimodal search.

\

Process Each Page

Once we have the pages, we will process each page

  1. Chunk the text

Takes each PDF page’s text and splits it into smaller, overlapping chunks

with doc["pages"].row() as page: page["chunks"] = page["text"].transform( cocoindex.functions.SplitRecursively( custom_languages=[ cocoindex.functions.CustomLanguageSpec( language_name="text", separators_regex=[ r"\n(\s*\n)+", r"[\.!\?]\s+", r"\n", r"\s+", ], ) ] ), language="text", chunk_size=600, chunk_overlap=100, )

  1. Process each chunk

Embed and collect the metadata we need, each chunk has embedding, original text, and reference the filename and page in where it came from.

with page["chunks"].row() as chunk: chunk["embedding"] = chunk["text"].call(embed_text) text_output.collect( id=cocoindex.GeneratedField.UUID, filename=doc["filename"], page=page["page_number"], text=chunk["text"], embedding=chunk["embedding"], )

  1. Process each image

We use CLIP to embed the image and collects the data, embedding and metadata filename, page_number.

with page["images"].row() as image: image["embedding"] = image["data"].transform(clip_embed_image) image_output.collect( id=cocoindex.GeneratedField.UUID, filename=doc["filename"], page=page["page_number"], image_data=image["data"], embedding=image["embedding"], )

When we collect image outputs, we also want to preserve relevant metadata alongside the embedding. For each image, we not only collect the image embedding and binary data, but also metadata such as the original file name and page number. This association makes it possible to trace embedded images back to their source document and page during retrieval or exploration.

Export to Qdrant

Finally we export the collected info to the target store

text_output.export( "text_embeddings", cocoindex.targets.Qdrant( connection=qdrant_connection, collection_name=QDRANT_COLLECTION_TEXT, ), primary_key_fields=["id"], ) image_output.export( "image_embeddings", cocoindex.targets.Qdrant( connection=qdrant_connection, collection_name=QDRANT_COLLECTION_IMAGE, ), primary_key_fields=["id"], )

🧭 Explore with CocoInsight (Free Beta)

Use CocoInsight to visually trace your data lineage and debug the flow.

It connects locally with zero data retention.

Start your local server:

cocoindex server -ci main

Then open the UI 👉 https://cocoindex.io/cocoinsight

💡 Why This Matters

Traditional document search only scratches the surface — it’s text-only and often brittle across document layouts. This flow gives you multimodal recall, meaning you can:

  • Search PDFs by text or image similarity
  • Retrieve related figures, diagrams, or captions
  • Build next-generation retrieval systems across rich content formats

Compare with ColPali Vision Model (OCR Free)

We also had an example for ColPali

To compare two approaches:

| Aspect | ColPali Multi-Vector Image Grids | Separate Text and Image Embeddings | |----|----|----| | Input | Whole page as an image grid (multi-vector patches) | Text extracted via OCR + images processed separately | | Embedding | Multi-vector patch-level embeddings preserving spatial context | Independent text and image vectors | | Query Matching | Late interaction between text token embeddings and image patches | Separate embedding similarity / fusion | | Document Structure Handling | Maintains layout and visual cues implicitly | Layout structure inferred by heuristics | | OCR Dependence | Minimal to none; model reads text visually | Heavy dependence on OCR (for scanned PDF) and text extraction | | Use Case Strength | Document-heavy, visual-rich formats | General image and text data, simpler layouts | | Complexity | Higher computational cost, more complex storage | Simpler, fewer compute resources needed |

In essence, ColPali excels in deep, integrated vision-text understanding, ideal for visually complex documents, while splitting text and images for separate embeddings is more modular but prone to losing spatial and semantic cohesion. The choice depends on your document complexity, precision needs, and resource constraints.

Support us

⭐ Star CocoIndex on GitHub and share with your community if you find it useful!

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.
Share Insights

You May Also Like

Tom Lee verwacht recordstijging S&P 500 door perfecte storm van marktfactoren

Tom Lee verwacht recordstijging S&P 500 door perfecte storm van marktfactoren

De bekende beursstrateeg Tom Lee van onderzoeksbureau Fundstrat verwacht dat de Amerikaanse aandelenmarkt zich opmaakt voor een stevige eindsprint in 2025. Volgens hem creëren vier belangrijke factoren de perfecte omstandigheden voor een grote uitbraak van de S&P 500 nog vóór het einde van het jaar. Check onze Discord Connect met "like-minded" crypto enthousiastelingen Leer gratis de basis van Bitcoin & trading - stap voor stap, zonder voorkennis. Krijg duidelijke uitleg & charts van ervaren analisten. Sluit je aan bij een community die samen groeit. Nu naar Discord Komende 10 weken cruciaal In een interview met CNBC zegt Lee dat de komende tien weken cruciaal zijn. “We bevinden ons in de laatste fase van het jaar, op een moment dat niet alleen de winstcijfers sterk zijn, maar de Amerikaanse centrale bank ook op het punt staat om te versoepelen,” aldus Lee. “Daarbij komt nog dat de zorgen over een mogelijke overheidssluiting waarschijnlijk zullen afnemen en dat de markt recent al een flinke correctie heeft verwerkt.” Volgens de strateeg zorgt die combinatie van factoren voor een ideale voedingsbodem voor een zogenoemde year-end chase, een periode waarin beleggers massaal terug de markt in stappen uit angst om verdere stijgingen te missen. “Ik denk dat de S&P 500 tegen het einde van het jaar minstens op 7.000 punten kan sluiten. En eerlijk gezegd vind ik dat nog een voorzichtige inschatting,” zegt Lee. De S&P 500, de toonaangevende index van de grootste Amerikaanse beursbedrijven, staat momenteel rond de 6.800 punten. Een stijging naar 7.000 zou neerkomen op een winst van ongeveer 3 procent in slechts enkele weken tijd. Vier pijlers voor een beursrally Volgens Lee rust zijn optimisme op vier pijlers: sterke winstcijfers, een nieuw Fed-beleid, afnemende marktonrust en de doorbraak van kunstmatige intelligentie (AI) in het bedrijfsleven. Sterke winstcijfers: Tot nu toe hebben bijna negen op de tien bedrijven in de S&P 500 beter dan verwachte kwartaalresultaten gemeld. Dat sterkt beleggers in hun overtuiging dat de Amerikaanse economie veerkrachtiger is dan gedacht, ondanks hogere rentes en geopolitieke spanningen. Renteverlagingen in zicht: De Federal Reserve heeft de rente sinds 2022 fors verhoogd om de inflatie te bestrijden, maar signalen wijzen nu op een versoepeling van het beleid. Volgens Lee kan dat het sentiment op de markten een flinke impuls geven. Lagere rentes verlichten de druk op huishoudens en bedrijven en maken aandelen aantrekkelijker ten opzichte van obligaties. Minder marktonrust: Na een piek in de volatiliteitsindex VIX begin oktober is de spanning op de markten afgenomen. Lee wijst erop dat een deel van de hefboomposities al is afgebouwd, wat ruimte schept voor nieuwe instroom van kapitaal. De impact van kunstmatige intelligentie: Volgens Lee speelt ook AI een steeds belangrijkere rol in het bedrijfsleven. “De zichtbaarheid rond AI is toegenomen,” zegt hij. “Bedrijven beginnen echte opbrengsten te zien en stellen hun investeringsplannen voor 2026 bij. Dat zal de productiviteit en winstgroei verder aanjagen.” Welke crypto nu kopen?Lees onze uitgebreide gids en leer welke crypto nu kopen verstandig kan zijn! Welke crypto nu kopen? De rentes zijn officieel omlaag voor het eerst sinds 2024, heeft Fed-voorzitter Jerome Powell vorige week aangekondigd, en dus lijkt de markt klaar om te gaan stijgen. Eén vraag komt telkens terug: welke crypto moet je nu kopen? In dit artikel bespreken we de munten die in 2025 écht het verschil kunnen… Continue reading Tom Lee verwacht recordstijging S&P 500 door perfecte storm van marktfactoren document.addEventListener('DOMContentLoaded', function() { var screenWidth = window.innerWidth; var excerpts = document.querySelectorAll('.lees-ook-description'); excerpts.forEach(function(description) { var excerpt = description.getAttribute('data-description'); var wordLimit = screenWidth wordLimit) { var trimmedDescription = excerpt.split(' ').slice(0, wordLimit).join(' ') + '...'; description.textContent = trimmedDescription; } }); }); Optimisme, maar niet zonder risico’s Hoewel Lee bekendstaat om zijn optimistische kijk op de markt, erkennen veel analisten dat zijn scenario niet onrealistisch is. De combinatie van meevallende inflatie, sterke bedrijfsresultaten en technologische vooruitgang vormt een stevige basis voor een stijging. Toch blijft het risico bestaan dat geopolitieke spanningen, tegenvallende economische data of een terugslag in AI-aandelen het sentiment kunnen drukken. Toch blijft Lee positief. “De Amerikaanse economie blijft verbazingwekkend veerkrachtig, en de markt heeft de recente correctie goed verteerd. De ingrediënten voor een rally zijn aanwezig,” besluit hij. Als zijn voorspelling uitkomt, zou dat de S&P 500 op een nieuw recordniveau brengen en mogelijk ook een positief effect hebben op andere markten, waaronder goud, Bitcoin en wereldwijde aandelenindices. Best wallet - betrouwbare en anonieme wallet Best wallet - betrouwbare en anonieme wallet Meer dan 60 chains beschikbaar voor alle crypto Vroege toegang tot nieuwe projecten Hoge staking belongingen Lage transactiekosten Best wallet review Koop nu via Best Wallet Let op: cryptocurrency is een zeer volatiele en ongereguleerde investering. Doe je eigen onderzoek. Het bericht Tom Lee verwacht recordstijging S&P 500 door perfecte storm van marktfactoren is geschreven door Thom Derks en verscheen als eerst op Bitcoinmagazine.nl.
Share
2025/10/27 20:16