Created
March 16, 2023 20:47
-
-
Save schappim/477125ff58f021641434d602e9875a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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