{ "cells": [ { "cell_type": "markdown", "id": "c0", "metadata": {}, "source": [ "# Indra OCR quickstart\n", "\n", "Extract text from an image file, and from an image already in memory. The first run downloads the models from Hugging Face (about 73 MB)." ] }, { "cell_type": "code", "id": "c1", "metadata": {}, "source": [ "%pip install -q indra-ocr" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "id": "c2", "metadata": {}, "source": [ "import indra_ocr\n", "\n", "conv = indra_ocr.load(threads=4)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "c3", "metadata": {}, "source": [ "## An image file\n", "\n", "`convert` reads images (PNG, JPEG, TIFF, BMP, WebP) and PDFs. It yields `(page index, Page)`; a multi-page TIFF gives one page per frame." ] }, { "cell_type": "code", "id": "c4", "metadata": {}, "source": [ "for i, page in conv.convert(\"scan.png\"):\n", " print(page.markdown) # headings, lists, tables as HTML\n", " print(page.text) # plain text, one line per line" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "c5", "metadata": {}, "source": [ "## Lines, boxes and layout\n", "\n", "Every line has a box in image pixels, its text and a confidence, in reading order. `page.blocks` holds the layout blocks (label, box, text)." ] }, { "cell_type": "code", "id": "c6", "metadata": {}, "source": [ "for ln in page.lines[:10]:\n", " print(ln.box, round(ln.conf, 2), ln.text)\n", "\n", "for b in page.blocks[:5]:\n", " print(b[\"label\"], b[\"bbox\"])" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "c7", "metadata": {}, "source": [ "## An image in memory\n", "\n", "Pass a grayscale `uint8` numpy array to `conv.ocr`, for example a camera frame, a crop or a decoded upload." ] }, { "cell_type": "code", "id": "c8", "metadata": {}, "source": [ "import cv2 # installed with indra-ocr\n", "\n", "color = cv2.imread(\"scan.png\") # or any uint8 BGR / RGB array\n", "gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)\n", "page = conv.ocr(gray)\n", "print(page.text)\n", "print(\"turned\", page.rotation, \"degrees, straightened\", page.skew, \"degrees\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "c9", "metadata": {}, "source": [ "## Math\n", "\n", "With `math=True`, lines with math come out as LaTeX (slower; the math models download on first use)." ] }, { "cell_type": "code", "id": "c10", "metadata": {}, "source": [ "conv_math = indra_ocr.load(threads=4, math=True)\n", "for i, page in conv_math.convert(\"equations.png\"):\n", " print(page.markdown)" ], "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }