Basic OCR in Obsidian

what exact error does it give?

The environment variables are a pain in the butt, too. On the windows machine I tested with, there were two Path variables. I added to both…

(i had trouble initially because of the way windows forms the paths, backslashes/forwardslashes. Do you have the target.path.replace pattern correct in the template?)

Hiya, I’m not a techie but I’ve been trying to get this to work on Windows 10. When I tried to run the term tesseract in powershell, it gave me an error that looked something likes this (btw I’m aware of the misspelling - it’s just a representation).

Seemingly the powershell had a similar error to Obsidian(?) So I tried to figure out how to run it first there.

Aferwards, I found this medium article which explains how to add System Variables. It looks like it’s working in poweshell for me now! But not in Obsidian (the red image is the error I am still getting).

Anyway, not sure if this was the obvious thing to do, just thought I’d throw in what little I found in case it helps someone :woman_shrugging:. Would be really cool to get it to work in the future.

@joschmit, seems like it could be what you are having issue with as well.

1 Like

This is weird. I successfully installed & used this on one machine (running Xubuntu 18.04). Synced up (including plugins & settings) on my laptop running Mint 20 & I when I try to create a new note from the OCR template I get the following error in the Obsidian console:


main.ts:213 Error with User Template ocr Error: Command failed: /usr/bin/tesseract “$ocr_input” -
/bin/sh: line 1: /usr/bin/tesseract: No such file or directory

at ChildProcess.exithandler (child_process.js:317)
at ChildProcess.emit (events.js:315)
at maybeClose (internal/child_process.js:1048)
at Socket.<anonymous> (internal/child_process.js:439)
at Socket.emit (events.js:315)
at Pipe.<anonymous> (net.js:673)

I’ve verified by copying the tesseract path & pasting into a terminal that it /usr/bin/tesseract is correct. Any suggestions about why it can’t be found?

For anyone here, I would love to get a real plugin created that’s more usable: Searchable OCR

I ended up needing to set the User Function path to

/opt/local/bin/tesseract "$ocr_input" -

If you’re not sure where your tesseract got installed, you can run

type -a tesseract

and the terminal will return the path.

The OCR works pretty well with printed material, completely unusable for handwritten notes, IMO.

I am getting a parse error and the console says
Templater Error: Template parsing error, aborting.
tp.user.ocr is not a function

Any help appreciated
Thanks in advance

Hello, I am getting the same parse error with version 1.12.0 of Templater on Obsidian 0.15.9.
The error message I get on the Console is Templater Error: Template parsing error, aborting. tp.user.ocr is not a function

The template used is the following:

---
creation date: <% tp.file.creation_date() %>
tags: [OCR]
---

<%*
const supportedFileTypes = ["jpeg", "jpg", "png"];
const images = this.app.vault.getFiles().filter((item) => supportedFileTypes.indexOf(item.extension) >= 0)
const target = await tp.system.suggester((item) => item.path, images, true);
const out = await tp.user.ocr({ocr_input: target.path});
%><%* tR += out %>

Interestingly enough, on the same machine (Macbook Air with mas OS Monterey) with the same version of Obsidian, but in a different Vault where the version of Templater is 1.7.1 the same script as above is working fine.

Is there anything I should do differently or a workaround for this behavior, or do I have to do the OCR in the vault that works and copy the results in the other one?

Many thanks for your help with this issue.
Best regards :wink:

@pika78 @gtewary

Sorry for the inconvenience. It looks like a templater update may have made the script no longer work. I don’t have time to troubleshoot, so I left a notice on the original post.

Thank you very much for your quick response. I will continue working with the two vault until a solution can be found.
Many thanks for a very useful script.
Best regards

1 Like

hi,
same boat as you.
i snapshot MCQs on phone,
and wanna OCR them and edit/study on obsidian.
time flies, there are already plugins like “text extractor” and “taskbone ocr”.
the latter may need paid if heavily used.
text extractor, claim to use tesseract, dont function well

but there is a STANDALONE foss called capture2text, which also use tesseract but somehow they modified and function very very well.

capture2text support CLI command line mode. e.g. capture2text -i input.jpg -o output.jpg (or to clipboard)

the only remaining thing is how to make it work smoothly instead of ocr files 1 by 1. thanks

ps: i saw this OP used templater for this /w tesseract, so this may also be possible.

It looks like they’ve repaired it, as it works again. I’m on Ubuntu, if it matters.

In my case…

  1. templater user function
    key : cmd_exec
    value : cmd /C %Cmd%

  2. Make Template like this.

---
creation date: <% tp.file.creation_date() %>
tags: [OCR]
---

<%*
const root = tp.file.path().replace(tp.file.path(true),"").replace("\/","\\");
const supportedFileTypes = ["jpeg", "jpg", "png"];
const images = this.app.vault.getFiles().filter((item) => supportedFileTypes.indexOf(item.extension) >= 0)
const target = await tp.system.suggester((item) => item.path, images, true);
const inputFile = root+target.path.replace("/", "\\");
console.log(inputFile);
const cmd_text = "tesseract -l kor+eng --oem 1 --psm 4 \"" + inputFile + "\" stdout";
console.log(cmd_text);
const out = await tp.user.cmd_exec({Cmd: cmd_text});
%><%* tR += out %>

it works well. for your information.