summaryrefslogtreecommitdiff
path: root/OCRImage/main.swift
diff options
context:
space:
mode:
Diffstat (limited to 'OCRImage/main.swift')
-rw-r--r--OCRImage/main.swift57
1 files changed, 53 insertions, 4 deletions
diff --git a/OCRImage/main.swift b/OCRImage/main.swift
index 9eff185..3e5d08a 100644
--- a/OCRImage/main.swift
+++ b/OCRImage/main.swift
@@ -10,6 +10,8 @@ import ArgumentParser
import Vision
import AppKit
+
+
class TextPiece {
var text:String?;
var topLeft:CGPoint;
@@ -17,6 +19,14 @@ class TextPiece {
var bottomLeft:CGPoint;
var bottomRIght:CGPoint;
+ init() {
+ self.text = "Empty";
+ self.topLeft = CGPoint(x:0,y:0);
+ self.topRight = CGPoint(x:0,y:0);
+ self.bottomLeft = CGPoint(x:0,y:0);
+ self.bottomRIght = CGPoint(x:0,y:0);
+ }
+
init(_ t: String, _ tl:CGPoint, _ tr:CGPoint, _ bl:CGPoint, _ br: CGPoint ) {
self.text = t;
self.topLeft = tl;
@@ -24,6 +34,14 @@ class TextPiece {
self.bottomLeft = bl;
self.bottomRIght = br;
}
+
+ func set(_ t: String, _ tl:CGPoint, _ tr:CGPoint, _ bl:CGPoint, _ br: CGPoint) {
+ self.text = t;
+ self.topLeft = tl;
+ self.topRight = tr;
+ self.bottomLeft = bl;
+ self.bottomRIght = br;
+ }
}
class TextOCR {
@@ -34,12 +52,41 @@ class TextOCR {
}
func recognizeImageUrl(_ url:URL, _ error: Error?) {
+ var pieces:[TextPiece] = []
+ var err:NSError?
+
+ //guard let cgImage =
+
+ //let requestHandler = VNImageRequestHandler(url: url)
+ var textRequest = VNRecognizeTextRequest(completionHandler: recognizeTextHandler(request:error:))
+
+ //let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler);
+ var handler = VNImageRequestHandler(url: url)
+ do {
+ try handler.perform([textRequest])
+ } catch {
+ print("Cannot perfom request error:\(error)")
+ }
}
func handleTextRequest(_ request: VNRequest, error: Error?) {
}
+
+ func recognizeTextHandler(request: VNRequest, error: Error?) {
+ print("Start recognize handler")
+ 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
+ }
+ print("\(recognizedStrings)")
+ }
}
print("Start program")
@@ -77,18 +124,20 @@ var fm = FileManager.default.homeDirectoryForCurrentUser
print(fm)
let path:String = options.inputFile
fm.appendPathComponent(path)
-let ocrImateUrl = fm.absoluteURL
+let ocrImageUrl = fm.absoluteURL
print(fm)
//load image
-let image = NSImage(contentsOf: ocrImateUrl)
+let image = NSImage(contentsOf: ocrImageUrl)
if (image == nil) {
- stderr.write("Cannot load image properly \(ocrImateUrl)".data(using: .utf8)!)
+ stderr.write("Cannot load image properly \(ocrImageUrl)".data(using: .utf8)!)
exit(0)
}
//Start text recognition request
-
+var ocr = TextOCR()
+var err:NSError?
+ocr.recognizeImageUrl(ocrImageUrl, err)