Skip to main content

Add a script for getting Live Text

ID
4fb1744
date
2022-12-31 11:29:45+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
af80b5f
message
Add a script for getting Live Text
changed files
1 file, 89 additions

Changed files

get_live_text (0) → get_live_text (2716)

diff --git a/get_live_text b/get_live_text
new file mode 100755
index 0000000..f868e9e
--- /dev/null
+++ b/get_live_text
@@ -0,0 +1,89 @@
+#!/usr/bin/env swift
+// Get OCR'd text from an image using Live Text in macOS.
+//
+// If you're using Preview, you can use Live Text to copy and paste text
+// that's in an image.  This script allows you to access that text
+// programatically, which is useful if you want to do bulk analysis of
+// the text in your images.
+//
+// You can search text in the Photos app, but this is useful if you:
+//
+//    - want to search images that aren't in your Photos library
+//    - want to do analysis which isn't just searching
+//
+// This is based on https://developer.apple.com/documentation/vision/recognizing_text_in_images#3601255
+//
+// == Usage ==
+//
+// Pass the path to your image as a single command-line argument.  Any text
+// in the image will be returned as a JSON list:
+//
+//      $ get_live_text koa-logger-redactions.jpg
+//      ["koa is","a species","of","tree in the","family","It is",
+//      "endemic to the Hawaiian","Islands, where it","tree."]
+//
+// If the image doesn't contain any text, it returns an empty list:
+//
+//      $ get_live_text marquee-rocket.jpg
+//      []
+//
+
+import Vision
+
+// Process the results of the text-recognition request.
+//
+// This is based on the code in Apple's documentation, and prints the
+// recognized text as a list of JSON strings.
+//
+// See https://developer.apple.com/documentation/vision/recognizing_text_in_images#3601255
+func recognizeTextHandler(request: VNRequest, error: Error?) {
+  guard
+    let observations =
+      request.results as? [VNRecognizedTextObservation]
+  else {
+    return
+  }
+  let recognizedStrings = observations.compactMap { observation in
+    // Return the string of the top VNRecognizedText instance.
+    return observation.topCandidates(1).first?.string
+  }
+
+  // Serialise to JSON
+  do {
+    let jsonData = try JSONSerialization.data(withJSONObject: recognizedStrings)
+
+    if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
+      print(JSONString)
+    }
+  } catch {
+    fputs("Unable to serialise the result as JSON: \(error).\n", stderr)
+    exit(1)
+  }
+}
+
+// Given the path to an image, print a JSON array of text it contains.
+func printTextInImage(imagePath: String) {
+  let requestHandler = VNImageRequestHandler(
+    url: URL(fileURLWithPath: imagePath),
+    options: [:]
+  )
+
+  let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
+
+  do {
+    // Perform the text-recognition request.
+    try requestHandler.perform([request])
+  } catch {
+    fputs("Unable to recognise text: \(error).\n", stderr)
+    exit(1)
+  }
+}
+
+let arguments = CommandLine.arguments
+
+if arguments.count != 2 {
+  fputs("Usage: \(arguments[0]) PATH\n", stderr)
+  exit(1)
+}
+
+printTextInImage(imagePath: arguments[1])