Instantly share code, notes, and snippets.

@pnc /ocr-shot.sh
Last active Nov 22, 2018

Embed
What would you like to do?
#!/bin/bash
set -e
CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ "$1" stdout -l eng | xml esc)
hex=$((cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>$CONTENTS</string>
</plist>
EOF
) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$1"
mdimport "$1"
@pnc

This comment has been minimized.

Owner

pnc commented Mar 17, 2018

Probably needs brew install tesseract xmlstarlet.

@pnc

This comment has been minimized.

Owner

pnc commented Mar 17, 2018

To index, say, your whole Desktop in parallel:

chmod a+x ocr-shot.sh
find ~/Desktop -name "Screen Shot*" -print0 | xargs -0 -P 4 -n 1 ./ocr-shot.sh
@pnc

This comment has been minimized.

Owner

pnc commented Mar 17, 2018

You can run this automatically for screenshots by enabling this Folder Action with Automator. The only tweak is to fully qualify the paths to tesseract and xml, since Automator doesn't know about your user's PATH.

Workflow is here (don't forget to brew install tesseract xmlstarlet first!): https://philcalvin.s3.amazonaws.com/downloads/ocr-screenshots.workflow.zip

screen shot 2018-03-17 at 13 36 02

@amake

This comment has been minimized.

amake commented Mar 18, 2018

If you screenshot individual windows, the alpha channel prevents Tesseract from scanning properly. Also a lot of UI text is too small to accurately scan. To solve this I preprocessed with ImageMagick like so:

CONTENTS=$(convert "$1" -magnify -alpha remove - | tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ stdin stdout -l eng | xml esc)

Testing with a screenshot of my Terminal, I got better results with -magnify than -adaptive-resize '200%x200%', but YMMV.

@loretoparisi

This comment has been minimized.

loretoparisi commented Mar 18, 2018

@pnc @amake this is awesome!!! Just one thing, the search keyword must be localized for every language, so in Italian it would be "Schermata*", then it works!

schermata 2018-03-18 alle 23 56 46

@sclarke

This comment has been minimized.

sclarke commented Mar 19, 2018

This is wonderful. Is there any reason not to run this on all image files on my machine? Are there drawbacks to carrying the extra extended attributes?

@berkus

This comment has been minimized.

berkus commented Mar 19, 2018

@sclarke shouldn't be any downsides, only larger indexes (but not too daunting).

@jk

This comment has been minimized.

jk commented Mar 19, 2018

For @amake tip you have to brew install imagemagick. And if you need other languages than englisch, you need to install brew install --with-all-languages tesseract and change the -l argument on tesseract. For example if you want to recognize english and german use l eng+deu.

@crystoneme

This comment has been minimized.

crystoneme commented Mar 31, 2018

any plan to support ocr pdf files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment