Install and Run macOS OCR Script in the Terminal

Apple has a very good, proprietary text recognition API. Evan Hahn wrote and shared a little swift script to use it on the command line.

Example 1838 xkcd:

xkcd comic to explain machine learning. This comic compares a machine learning system to a compost pile. Composting is the process of taking organic matter, such as food and yard waste, and allowing it to decompose into a form that serves as fertilizer. A common method of composting is to mound the organic matter in a pile with a certain amount of moisture, then "stirring" the pile occasionally to move the less-decomposed material from the top to the interior of the pile, where it will decompose faster.

In large-scale composting operations, the raw organic matter added to the pile is referred to as "input". This cartoon implies a play on the term "input", comparing a compost input to a data input.

Result:

terminal view of script run: ocr ml.png and resulted OCRed text: THIS IS YOUR MACHINE LEARNING SYSTEM?
YUP! YOU POUR THE DATA INTO THIS BIG
PILE OF LINEAR ALGEBRA, THEN COLLECT
THE ANSWERS ON THE OTHER SIDE.
WHAT IF THE ANSWERS ARE WRONG?
JUST STIR THE PILE UNTIL
THEY START LOOKING RIGHT.
БАТА



This is very useful, to OCR an image fast on the command line and – even better – combine it with other commands or include it in other scripts.

But: His post doesn’t contain a clear installation guide. If adding user-installed command-line programs on a Mac is not your daily business, these steps might help:

  • Save the script (here or here) to /usr/local/bin and change into the folder with cd /usr/local/bin
  • Maybe your code editor added the swift extension. If that is the case, your file is named ocr.swift, remove it with mv ocr.swift ocr.
  • Make it executable chmod +x ocr
  • use it ocr path/to/image.png

Schreibe einen Kommentar