/** * OCR Search - 🔍 Find files that contain some text with OCR * Copyright (C) 2021 rigwild (https://github.com/rigwild) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import path from 'path'; import { recognize as tesseractRecognize } from 'node-tesseract-ocr'; import dirTree from 'directory-tree'; import type { ModuleThread, Pool } from 'threads'; export declare type ScanOptions = { /** * List of words to search (if one is matched, the file is matched) * * If not provided, every files will get matched (useful to do mass OCR and save the result) */ words?: string[]; /** Should the OCR scanned content of each file be saved to a txt file (e.g. "file.png.txt") */ saveOcr?: boolean; /** Should the logs be printed to the console? (default = false) */ shouldConsoleLog?: boolean; /** Should the matches file content be printed to the console? (default = true) */ shouldConsoleLogMatches?: boolean; /** * If provided, the progress will be saved to a file * * When stopped, the process will start from where it stopped last time by looking there */ progressFile?: string; /** If provided, every file path and their text content that were matched are logged to this file */ matchesLogFile?: string; /** File extensions to ignore when looking for files (e.g. `new Set(['.pdf', '.jpg'])`) */ ignoreExt?: Set; pdfExtractFirst?: number; pdfExtractLast?: number; /** * Amount of worker threads to use (default = your total CPU cores - 2) * * Note: Using all your available cores may slow down the process! */ workerPoolSize?: number; /** * Tesseract OCR config, will default `{ lang: 'eng', oem: 1, psm: 1 }` * * @see https://github.com/tesseract-ocr/tesseract/blob/main/doc/tesseract.1.asc */ tesseractConfig?: TesseractConfig; }; export declare type Progress = { visited: Set; matched: Map; }; export declare type ProgressJson = { visited: string[]; matched: { [path: string]: { text: string; matches: string[]; }; }; }; /** @see https://github.com/tesseract-ocr/tesseract/blob/main/doc/tesseract.1.asc */ export declare type TesseractConfig = Parameters[1]; export declare type WorkerMethods = { scanFile: typeof scanFile; pdfToImages: typeof pdfToImages; }; export declare type WorkerPool = Pool>; export declare const cleanStr: (str: string) => string; export declare const logProgress: (visitedCount: number, totalFilesCount: number, str: string) => void; /** * @param filePath Path to the image to extract text from * @param tesseractConfig Tesseract configuration * @param shouldCleanStr Should the string be normalized (lowercase, accents removed, whitespace removed) * @returns Text content */ export declare const ocr: (filePath: string, tesseractConfig?: TesseractConfig, shouldCleanStr?: boolean) => Promise; /** * Does the file `file.pdf-1.png` exist? * @param filePath Path to the PDF file * @returns The PDF is already extracted */ export declare const isPdfAlreadyExtractedToImages: (filePath: string) => Promise; /** * Given a PDF file, find its extracted pages images path * @param filePath Path to the PDF file * @returns Extracted pages path */ export declare const getPdfExtractedImages: (filePath: string) => Promise>; /** * Extract all the pages of a PDF to PDF images * * @param filePath Path to the PDF to be converted * @returns List of generated output images path */ export declare const pdfToImages: (filePath: string, firstPage?: number | undefined, lastPage?: number | undefined) => Promise>; /** * Find all words that were matched in text * * If words is `['MATCH_ALL']`, it will just skip the search as it will match every files * @param input * @param words * @returns List of matched words */ export declare const findMatches: (input: string, words: ScanOptions['words']) => string[]; export declare const isSupportedExtension: (ext: string) => boolean; export declare const scanFile: (file: dirTree.DirectoryTree, words: ScanOptions['words'], tesseractConfig?: TesseractConfig) => Promise<{ text: string; matches: string[]; }>; export declare const loadProgress: (progressFile?: string | undefined) => Promise; export declare const saveProgress: (progressFile: string, progress: Progress) => Promise; export declare const getTreeFilesCount: (tree: dirTree.DirectoryTree) => number; export declare const getTree: (scannedDir: string) => Promise; //# sourceMappingURL=utils.d.ts.map