Skip to content

Instantly share code, notes, and snippets.

@schappim
Created March 16, 2023 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schappim/477125ff58f021641434d602e9875a7b to your computer and use it in GitHub Desktop.
Save schappim/477125ff58f021641434d602e9875a7b to your computer and use it in GitHub Desktop.
import Foundation
import Vision
import AppKit
func performOCR(on image: NSImage, completion: @escaping ([String: Any]) -> Void) {
guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
print("Error: Unable to get CGImage from NSImage.")
exit(1)
}
let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])
let request = VNRecognizeTextRequest { (request, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
exit(1)
}
guard let observations = request.results as? [VNRecognizedTextObservation] else {
print("Error: Unable to get recognized text.")
exit(1)
}
var jsonArray: [[String: Any]] = []
for observation in observations {
if let topCandidate = observation.topCandidates(1).first {
let text = topCandidate.string
let boundingBox = observation.boundingBox
let position = [
"x": boundingBox.origin.x,
"y": boundingBox.origin.y,
"width": boundingBox.size.width,
"height": boundingBox.size.height
]
jsonArray.append([
"text": text,
"position": position
])
}
}
completion(["result": jsonArray])
}
request.recognitionLevel = .accurate
request.usesLanguageCorrection = true
try? requestHandler.perform([request])
}
func loadImage(at path: String) -> NSImage? {
let url = URL(fileURLWithPath: path)
return NSImage(contentsOf: url)
}
guard CommandLine.argc == 2 else {
print("Usage: \(CommandLine.arguments[0]) path/to/image")
exit(1)
}
let imagePath = CommandLine.arguments[1]
guard let image = loadImage(at: imagePath) else {
print("Error: Unable to load image at path: \(imagePath)")
exit(1)
}
performOCR(on: image) { json in
let jsonData = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
exit(0)
}
RunLoop.main.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment