#106 - Basic, English-only OCR

This commit is contained in:
Simon Cambier
2022-11-06 17:04:46 +01:00
parent 40f9df6a47
commit f0b2de4316
8 changed files with 95 additions and 48 deletions

View File

@@ -124,15 +124,12 @@ See [styles.css](./assets/styles.css) for more information.
**Omnisearch makes Obsidian sluggish at startup.**
- You may have _big_ documents. Huge notes (like novels) can freeze the interface for a short time when being indexed. While Omnisearch uses a cache between sessions, it's still rebuilt at startup to keep it up-to-date.
- While Omnisearch does its best to work smoothly in the background, bigger vaults can cause some hiccups at startup because of the search index size.
**I have thousands of notes, and at startup I have to wait a few seconds before Omnisearch gives me the context of a result.**
**Omnisearch gives inconsistent/invalid results, there are errors in the developer console**
- Omnisearch refreshes its index at startup. During this time, you can still find notes, but Omnisearch is not able to show you the excerpts.
**Omnisearch gives inconsistent/invalid results, or there are errors in the developer console.**
- Restart Obsidian to force a reindex of Omnisearch
- Restart Obsidian to force a reindex of Omnisearch.
- The cache can be corrupted; you can clear it at the bottom of the settings page, then restart Obsidian.
**A query should return a result that does not appear.**
@@ -153,6 +150,8 @@ You can write your issue [here](https://github.com/scambier/obsidian-omnisearch/
Omnisearch is licensed under [GPL-3](https://tldrlegal.com/license/gnu-general-public-license-v3-(gpl-3)).
## Sponsors
## Thanks
To all people who donate through [Ko-Fi](https://ko-fi.com/scambier) or [Github Sponsors](https://github.com/sponsors/scambier) ❤
![JetBrains Logo (Main) logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)

View File

@@ -44,7 +44,7 @@
"dexie": "^3.2.2",
"lodash-es": "4.17.21",
"minisearch": "github:scambier/minisearch#callback_desync",
"obsidian-text-extract": "1.0.1",
"obsidian-text-extract": "1.0.2",
"pure-md5": "^0.1.14"
},
"pnpm": {

8
pnpm-lock.yaml generated
View File

@@ -23,7 +23,7 @@ specifiers:
lodash-es: 4.17.21
minisearch: github:scambier/minisearch#callback_desync
obsidian: latest
obsidian-text-extract: 1.0.1
obsidian-text-extract: 1.0.2
prettier: ^2.7.1
prettier-plugin-svelte: ^2.8.0
pure-md5: ^0.1.14
@@ -40,7 +40,7 @@ dependencies:
dexie: 3.2.2
lodash-es: 4.17.21
minisearch: github.com/scambier/minisearch/adf11cab46d851220a41c9ad95ed986b630f0f3c
obsidian-text-extract: 1.0.1
obsidian-text-extract: 1.0.2
pure-md5: 0.1.14
devDependencies:
@@ -4326,8 +4326,8 @@ packages:
object-keys: 1.1.1
dev: true
/obsidian-text-extract/1.0.1:
resolution: {integrity: sha512-IJlxbZi/WxzWKwnyruFP/7KUTed6MZW+74OGI5ovZVfikkWx7lfBe1rn23La2kq/BrwETIAugO5Ke4ppvp53gA==}
/obsidian-text-extract/1.0.2:
resolution: {integrity: sha512-OOnV1B0kTED46vxPRLOHdHeev6CqcXs6A39DE3IJaEV9PNZKPF3/f6d3t7/zFLgOVMNiVD9Uj+YziAhXeKt4lw==}
dependencies:
dexie: 3.2.2
p-limit: 4.0.0

View File

@@ -18,7 +18,6 @@
$: {
imagePath = null
if (isFileImage(note.path)) {
console.log(note.path)
// @ts-ignore
const file = app.vault.getFiles().find(f => f.path === note.path)
if (file) {

View File

@@ -3,6 +3,7 @@ import {
extractHeadingsFromCache,
getAliasesFromMetadata,
getTagsFromMetadata,
isFileImage,
isFilePlaintext,
removeDiacritics,
} from './tools/utils'
@@ -10,7 +11,7 @@ import * as NotesIndex from './notes-index'
import type { TFile } from 'obsidian'
import type { IndexedDocument } from './globals'
import { getNonExistingNotes } from './tools/notes'
import { getPdfText } from 'obsidian-text-extract'
import { getPdfText, getImageText } from 'obsidian-text-extract'
/**
* Return all plaintext files as IndexedDocuments
@@ -27,20 +28,33 @@ export async function getPlainTextFiles(): Promise<IndexedDocument[]> {
}
/**
* Return all PDF files as IndexedDocuments.
* If a PDF isn't cached, it will be read from the disk and added to the IndexedDB
* Return all PDFs as IndexedDocuments.
*/
export async function getPDFFiles(): Promise<IndexedDocument[]> {
export async function getPDFAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => f.path.endsWith('.pdf'))
return await getBinaryFiles(files)
}
/**
* Return all imageas as IndexedDocuments.
*/
export async function getImagesAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => isFileImage(f.path))
return await getBinaryFiles(files)
}
async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
const data: IndexedDocument[] = []
const input = []
for (const file of files) {
input.push(new Promise(async (resolve, reject) => {
input.push(
new Promise(async (resolve, reject) => {
const doc = await fileToIndexedDocument(file)
await cacheManager.updateLiveDocument(file.path, doc)
data.push(doc)
return resolve(null)
}))
})
)
}
await Promise.all(input)
return data
@@ -51,13 +65,15 @@ export async function getPDFFiles(): Promise<IndexedDocument[]> {
* Will use the cache if possible.
*/
export async function fileToIndexedDocument(
file: TFile,
file: TFile
): Promise<IndexedDocument> {
let content: string
if (isFilePlaintext(file.path)) {
content = await app.vault.cachedRead(file)
} else if (file.path.endsWith('.pdf')) {
content = await getPdfText(file)
} else if (isFileImage(file.path)) {
content = await getImageText(file)
} else {
throw new Error('Invalid file: ' + file.path)
}

View File

@@ -84,9 +84,7 @@ export default class OmnisearchPlugin extends Plugin {
showWelcomeNotice(this)
}
onunload(): void {
}
onunload(): void {}
addRibbonButton(): void {
this.ribbonButton = this.addRibbonIcon('search', 'Omnisearch', _evt => {
@@ -110,13 +108,13 @@ async function populateIndex(): Promise<void> {
// Initialize minisearch
let engine = SearchEngine.getEngine()
// No cache for iOS
// if not iOS, load data from cache
if (!Platform.isIosApp) {
engine = await SearchEngine.initFromCache()
}
// Load plaintext files
console.log('Omnisearch - Fetching notes')
console.log('Omnisearch - Reading notes')
const plainTextFiles = await FileLoader.getPlainTextFiles()
let allFiles = [...plainTextFiles]
// iOS: since there's no cache, directly index the documents
@@ -127,15 +125,28 @@ async function populateIndex(): Promise<void> {
// Load PDFs
if (settings.PDFIndexing) {
console.log('Omnisearch - Fetching PDFs')
const pdfs = await FileLoader.getPDFFiles()
console.log('Omnisearch - Reading PDFs')
const pdfDocuments = await FileLoader.getPDFAsDocuments()
// iOS: since there's no cache, just index the documents
if (Platform.isIosApp) {
await wait(1000)
await engine.addAllToMinisearch(pdfs)
await engine.addAllToMinisearch(pdfDocuments)
}
// Add PDFs to the files list
allFiles = [...allFiles, ...pdfs]
allFiles = [...allFiles, ...pdfDocuments]
}
// Load Images
if (settings.imagesIndexing) {
console.log('Omnisearch - Reading Images')
const imagesDocuments = await FileLoader.getImagesAsDocuments()
// iOS: since there's no cache, just index the documents
if (Platform.isIosApp) {
await wait(1000)
await engine.addAllToMinisearch(imagesDocuments)
}
// Add Images to the files list
allFiles = [...allFiles, ...imagesDocuments]
}
console.log('Omnisearch - Total number of files: ' + allFiles.length)
@@ -146,6 +157,7 @@ async function populateIndex(): Promise<void> {
console.log('Omnisearch - Checking index cache diff...')
// Check which documents need to be removed/added/updated
const diffDocs = await cacheManager.getDiffDocuments(allFiles)
console.log(`Omnisearch - Files to add/remove/update: ${diffDocs.toAdd.length}/${diffDocs.toDelete.length}/${diffDocs.toUpdate.length}`)
needToUpdateCache = !!(
diffDocs.toAdd.length ||
diffDocs.toDelete.length ||
@@ -154,20 +166,16 @@ async function populateIndex(): Promise<void> {
// Add
await engine.addAllToMinisearch(diffDocs.toAdd)
console.log(`Omnisearch - ${diffDocs.toAdd.length} files to add`)
diffDocs.toAdd.forEach(doc =>
cacheManager.updateLiveDocument(doc.path, doc)
)
// Delete
console.log(`Omnisearch - ${diffDocs.toDelete.length} files to remove`)
diffDocs.toDelete.forEach(d => engine.removeFromMinisearch(d))
diffDocs.toDelete.forEach(doc => cacheManager.deleteLiveDocument(doc.path))
// Update (delete + add)
console.log(`Omnisearch - ${diffDocs.toUpdate.length} files to update`)
diffDocs.toUpdate
.forEach(({ oldDoc, newDoc }) => {
diffDocs.toUpdate.forEach(({ oldDoc, newDoc }) => {
engine.removeFromMinisearch(oldDoc)
cacheManager.updateLiveDocument(oldDoc.path, newDoc)
})
@@ -205,13 +213,13 @@ async function cleanOldCacheFiles() {
}
function showWelcomeNotice(plugin: Plugin) {
const code = '1.7.6'
const code = '1.8.0-beta.3'
if (settings.welcomeMessage !== code) {
const welcome = new DocumentFragment()
welcome.createSpan({}, span => {
span.innerHTML = `<strong>Omnisearch has been updated</strong>
New beta feature: PDF search 🔎📄
<small>Toggle "<i>BETA - Index PDFs</i>" in Omnisearch settings page.</small>`
span.innerHTML = `<strong>Omnisearch BETA has been updated</strong>
You can now enable "Images Indexing" to use Optical Character Recognition on your scanned documents
🔎🖼`
})
new Notice(welcome, 30000)
}

View File

@@ -25,6 +25,8 @@ export interface OmnisearchSettings extends WeightingSettings {
indexedFileTypes: string[]
/** Enable PDF indexing */
PDFIndexing: boolean
/** Enable PDF indexing */
imagesIndexing: boolean
/** Display Omnisearch popup notices over Obsidian */
showIndexingNotices: boolean
/** Activate the small 🔍 button on Obsidian's ribbon */
@@ -147,7 +149,7 @@ export class SettingsTab extends PluginSettingTab {
indexPDFsDesc.createSpan({}, span => {
span.innerHTML = `Omnisearch will include PDFs in search results.
<ul>
<li>⚠️ Depending on their size, PDFs can take anywhere from a few seconds to 2 minutes to be processed.</li>
<li>⚠️ Each PDF can take anywhere from a few seconds to 2 minutes to be processed.</li>
<li>⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.</li>
<li>⚠️ Some PDFs can't be processed correctly and will return an empty text.</li>
<li>This feature is currently a work-in-progress, please report issues that you might experience.</li>
@@ -164,6 +166,26 @@ export class SettingsTab extends PluginSettingTab {
})
)
// PDF Indexing
const indexImagesDesc = new DocumentFragment()
indexImagesDesc.createSpan({}, span => {
span.innerHTML = `Omnisearch will use <a href="https://en.wikipedia.org/wiki/Tesseract_(software)">Tesseract</a> to index images from their text.
<ul>
<li>Only English is supported at the moment.</li>
<li>Not all images can be correctly read by the OCR, this feature works best with scanned documents.</li>
</ul>
<strong style="color: var(--text-accent)">Needs a restart to fully take effect.</strong>`
})
new Setting(containerEl)
.setName('BETA - Images Indexing')
.setDesc(indexImagesDesc)
.addToggle(toggle =>
toggle.setValue(settings.imagesIndexing).onChange(async v => {
settings.imagesIndexing = v
await saveSettings(this.plugin)
})
)
// #endregion Behavior
// #region User Interface
@@ -315,6 +337,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
ignoreDiacritics: true,
indexedFileTypes: [] as string[],
PDFIndexing: false,
imagesIndexing: false,
showIndexingNotices: false,
showShortName: false,

View File

@@ -184,7 +184,9 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
export function isFileIndexable(path: string): boolean {
return (
(settings.PDFIndexing && path.endsWith('.pdf')) || isFilePlaintext(path)
(settings.PDFIndexing && path.endsWith('.pdf')) ||
isFilePlaintext(path) ||
(settings.imagesIndexing && isFileImage(path))
)
}