2

Recently I've found on my Mac that I can easily highlight text in an image that wasn't possible before.

Is there also a built-in CLI option or an AppleScript option to address the OCR program with which I can achieve the same results as tesseract?

Thanks

2

2 Answers 2

3

Shortcuts.app has an "Extract Text from Image" action.

enter image description here

There's also a shell command shortcuts to run a Shortcut.

0
0

This is core of a simple cli tool to extract text from an image:

 let img = NSImage(byReferencing: url)
    guard let imgRef = img.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
        fputs("Error: failed to convert NSImage to CGImage for '\(url)'\n", stderr)
        return 1
    }

    let request = VNRecognizeTextRequest { (request, error) in
        let observations = request.results as? [VNRecognizedTextObservation] ?? []
        let obs : [String] = observations.map { $0.topCandidates(1).first?.string ?? ""}
        try? obs.joined(separator: "\n").write(to: url.appendingPathExtension("md"), atomically: true, encoding: String.Encoding.utf8)

        fputs("got page obs is \(obs)", stderr)
    }
    request.recognitionLevel = VNRequestTextRecognitionLevel.accurate // or .fast
    request.usesLanguageCorrection = true
    request.revision = VNRecognizeTextRequestRevision2
    request.recognitionLanguages = ["de"]
    request.customWords = ["more", "Worte", "Wort"]

    try? VNImageRequestHandler(cgImage: imgRef, options: [:]).perform([request])

from this full working project

You must log in to answer this question.