OCR bases

This commit is contained in:
Simon Cambier
2022-10-30 20:45:55 +01:00
parent 10019513ba
commit 1cd151b1fd
3 changed files with 50 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ import {
extractHeadingsFromCache, extractHeadingsFromCache,
getAliasesFromMetadata, getAliasesFromMetadata,
getTagsFromMetadata, getTagsFromMetadata,
isFileImage,
isFilePlaintext, isFilePlaintext,
removeDiacritics, removeDiacritics,
} from './tools/utils' } from './tools/utils'
@@ -10,7 +11,7 @@ import * as NotesIndex from './notes-index'
import type { TFile } from 'obsidian' import type { TFile } from 'obsidian'
import type { IndexedDocument } from './globals' import type { IndexedDocument } from './globals'
import { getNonExistingNotes } from './tools/notes' import { getNonExistingNotes } from './tools/notes'
import { getPdfText } from 'obsidian-text-extract' import { getImageText, getPdfText } from 'obsidian-text-extract'
/** /**
* Return all plaintext files as IndexedDocuments * Return all plaintext files as IndexedDocuments
@@ -48,6 +49,35 @@ export async function getPDFFiles(): Promise<IndexedDocument[]> {
return data return data
} }
/**
* Return all Image files as IndexedDocuments.
* If a PDF isn't cached, it will be read from the disk and added to the IndexedDB
*/
export async function getImageFiles(): Promise<IndexedDocument[]> {
const allFiles = app.vault
.getFiles()
.filter(
f =>
f.path.endsWith('.png') ||
f.path.endsWith('.jpg') ||
f.path.endsWith('.jpeg')
)
const data: IndexedDocument[] = []
const input = []
for (const file of allFiles) {
input.push(
NotesIndex.processQueue(async () => {
const doc = await fileToIndexedDocument(file)
await cacheManager.updateDocument(file.path, doc)
data.push(doc)
})
)
}
await Promise.all(input)
return data
}
/** /**
* Convert a file into an IndexedDocument. * Convert a file into an IndexedDocument.
* Will use the cache if possible. * Will use the cache if possible.
@@ -61,6 +91,8 @@ export async function fileToIndexedDocument(
content = removeDiacritics(await app.vault.cachedRead(file)) content = removeDiacritics(await app.vault.cachedRead(file))
} else if (file.path.endsWith('.pdf')) { } else if (file.path.endsWith('.pdf')) {
content = removeDiacritics(await getPdfText(file)) content = removeDiacritics(await getPdfText(file))
} else if (isFileImage(file.path)) {
content = removeDiacritics(await getImageText(file))
} else { } else {
throw new Error('Invalid file: ' + file.path) throw new Error('Invalid file: ' + file.path)
} }

View File

@@ -128,6 +128,16 @@ async function populateIndex(): Promise<void> {
console.timeEnd('Omnisearch - Timing') console.timeEnd('Omnisearch - Timing')
} }
// Load Images
// if (settings.PDFIndexing) {
console.time('Omnisearch - Timing')
const images = await FileLoader.getImageFiles()
// Index them
await tmpEngine.addAllToMinisearch(images)
console.log(`Omnisearch - Indexed ${images.length} Images`)
console.timeEnd('Omnisearch - Timing')
// }
// Load PDFs into the main search engine, and write cache // Load PDFs into the main search engine, and write cache
SearchEngine.loadTmpDataIntoMain() SearchEngine.loadTmpDataIntoMain()
SearchEngine.isIndexing.set(false) SearchEngine.isIndexing.set(false)

View File

@@ -188,6 +188,12 @@ export function isFileIndexable(path: string): boolean {
) )
} }
export function isFileImage(path: string): boolean {
return (
path.endsWith('.png') || path.endsWith('.jpg') || path.endsWith('.jpeg')
)
}
export function isFilePlaintext(path: string): boolean { export function isFilePlaintext(path: string): boolean {
return getPlaintextExtensions().some(t => path.endsWith(`.${t}`)) return getPlaintextExtensions().some(t => path.endsWith(`.${t}`))
} }