ソースを参照

[OCR] - OCR优化

liujiajie 1 年間 前
コミット
d2d211cd61

+ 2 - 7
PDF Office/PDF Master/Class/Common/OC/OCR/KMGOCRManager.m

@@ -462,7 +462,6 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
             NSData *data = images[i];
             image = [[NSImage alloc] initWithData:data];
         }
-//        KMGOCROperation *op = [[KMGOCROperation alloc] initWithRecognitionImage:image withImageIndex:i];
         KMGOCROperation *op = [[KMGOCROperation alloc] initWithRecognitionImg:image imgIndex:i];
         op.selectedLanguages = [NSMutableArray arrayWithArray:languages];
         op.operationDelegate = self;
@@ -513,7 +512,6 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
             image = [[NSImage alloc] initWithData:data];
         }
         
-//        KMOCROperation *op = [[KMOCROperation alloc] initWithRecognitionImage:image withImageIndex:i];
         KMOCROperation *op = [[KMOCROperation alloc] initWithRecognitionImg:image imgIndex:i];
         op.fileType = self.fileType;
         op.selectedLanguages = self.languages;
@@ -600,18 +598,16 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
 }
 
 #pragma mark - KMGOCROperationDelegate
-- (void)GOCROperation:(KMGOCROperation *)operation startOCRImageAtIndex:(NSInteger)index {
+- (void)GOCROperation:(KMGOCROperation *)operation startOCRImageAtIndex:(NSInteger)index{
     if ([self.delegate respondsToSelector:@selector(GOCRManager:didStartOCRImageAtIndex:)]) {
         [self.delegate GOCRManager:self didStartOCRImageAtIndex:index];
     }
 }
-
-- (void)GOCROperation:(KMGOCROperation *)operation cancelOCRImageAtIndex:(NSInteger)index {
+- (void)GOCROperation:(KMGOCROperation *)operation cancelOCRImageAtIndex:(NSInteger)index{
     if ([self.delegate respondsToSelector:@selector(GOCRManager:didCancelOCRImageAtIndex:)]) {
         [self.delegate GOCRManager:self didCancelOCRImageAtIndex:index];
     }
 }
-
 - (void)GOCROperation:(KMGOCROperation *)operation finishOCRImageAtIndex:(NSInteger)index results:(NSArray *)results {
     if ([self.delegate respondsToSelector:@selector(GOCRManager:didFinishOCRImageAtIndex:results:)]) {
         [self.delegate GOCRManager:self didFinishOCRImageAtIndex:index results:results];
@@ -623,7 +619,6 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
         }
     }
 }
-
 - (void)GOCROperation:(KMGOCROperation *)operation failureOCRImageAtIndex:(NSInteger)index error:(NSError *)error {
     if ([self.delegate respondsToSelector:@selector(GOCRManager:didFailureOCRImageAtIndex:error:)]) {
         [self.delegate GOCRManager:self didFailureOCRImageAtIndex:index error:error];

+ 4 - 4
PDF Office/PDF Master/Class/PDFTools/OCRNew/Controller/KMOCRPDFWindowController.swift

@@ -627,10 +627,10 @@ class KMOCRPDFWindowController: NSWindowController, NSWindowDelegate, KMGOCRMana
         DispatchQueue.main.async {
             let languages = KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageCodeKey)
             if self.planComboBox.indexOfSelectedItem == 0 {
-//                self.googleOCRManger = KMGOCRManager()
-//                self.googleOCRManger?.ocrType = .google
-//                self.googleOCRManger?.delegate = self
-//                self.googleOCRManger?.recognitionImages((selctPageImages as! [Any]), withLanguages: languages as? [Any])
+                self.googleOCRManger = KMGOCRManager()
+                self.googleOCRManger?.ocrType = .google
+                self.googleOCRManger?.delegate = self
+                self.googleOCRManger?.recognitionImages((selctPageImages as! [Any]), withLanguages: languages as? [Any])
             } else {
                 self.appleOCRManger = KMGOCRManager()
                 self.appleOCRManger?.ocrType = .apple

+ 57 - 61
PDF Office/PDF Master/Class/PDFTools/OCRNew/Model/KMGOCROperation.swift

@@ -9,10 +9,11 @@ import Foundation
 
 @objc(KMGOCROperationDelegate)
 protocol KMGOCROperationDelegate: AnyObject {
-    @objc optional func GOCROperationCancel(_ op: KMGOCROperation, atIndex: Int)
-    @objc optional func GOCROperationStart(_ op: KMGOCROperation, atIndex: Int)
-    @objc optional func GOCROperationFinish(_ op: KMGOCROperation, atIndex: Int, results: Array<Any>)
-    @objc optional func GOCROperationFail(_ op: KMGOCROperation, atIndex: Int, err: Error?)
+    @objc optional func GOCROperation(_ operation: KMGOCROperation, cancelOCRImageAtIndex index: Int)
+    @objc optional func GOCROperation(_ operation: KMGOCROperation, startOCRImageAtIndex index: Int)
+    @objc optional func GOCROperation(_ operation: KMGOCROperation, finishOCRImageAtIndex index: Int, results: Array<Any>)
+    @objc optional func GOCROperation(_ operation: KMGOCROperation, failureOCRImageAtIndex index: Int, error: Error?)
+
 }
 
 let KMGOC_API_URL = "https://vision.googleapis.com/v1/images:annotate"
@@ -26,36 +27,18 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
 
 @objcMembers class KMGOCROperation: Operation{
     @objc var operationDelegate: KMGOCROperationDelegate?
+    var hasCanceled: Bool = false
+    var hasFinished: Bool = false
+    var hasExcuting: Bool = false
     var selectedLanguages: Array<Any>?
     override var isExecuting: Bool{
-        set{
-            self.willChangeValue(forKey: "isExecuting")
-            self.isExecuting = newValue
-            didChangeValue(forKey: "isExecuting")
-        }
-        get{
-            return super.isExecuting
-        }
+        return self.hasExcuting
     }
     override var isFinished: Bool{
-        set{
-            self.willChangeValue(forKey: "isFinished")
-            self.isFinished = newValue
-            didChangeValue(forKey: "isFinished")
-        }
-        get{
-            return super.isFinished
-        }
+        return self.hasFinished
     }
     override var isCancelled: Bool{
-        set{
-            self.willChangeValue(forKey: "isCancelled")
-            self.isCancelled = newValue
-            didChangeValue(forKey: "isCancelled")
-        }
-        get{
-            return super.isCancelled
-        }
+        return self.hasCanceled
     }
 
     var fileName: String = ""
@@ -70,8 +53,8 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
         self.orcImage = recognitionImg
         self.queuePriority = .normal
         self.name = self.fileName
-        self.isExecuting = false
-        self.isFinished = false
+        self.hasExcuting = false
+        self.hasFinished = false
     }
     func fileNameWithDate() -> String {
         let formatter = DateFormatter()
@@ -83,22 +66,24 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
     
     override func start() {
         if p_checkCancelled() { return }
-        isExecuting = true
+        willChangeValue(forKey: "isExecuting")
+        self.hasExcuting = true
+        didChangeValue(forKey: "isExecuting")
         
-        Thread.detachNewThreadSelector(#selector(main), toTarget: self, with: nil)
-    }
-    @objc override func main() {
-        do {
+//        Thread.detachNewThreadSelector(#selector(main), toTarget: self, with: nil)
+//    }
+//    @objc override func main() {
+//        do {
             if p_checkCancelled() { return }
             recognitionImage(orcImage ?? NSImage())
-            while isExecuting {
-                if p_checkCancelled() {
-                    return
-                }
-            }
-        } catch let e {
-            Swift.debugPrint("Exception %@", e)
-        }
+//            while isExecuting {
+//                if p_checkCancelled() {
+//                    return
+//                }
+//            }
+//        } catch let e {
+//            Swift.debugPrint("Exception %@", e)
+//        }
     }
     override func cancel() { 
         super.cancel()
@@ -107,34 +92,40 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
             task = nil
         }
         
-        self.operationDelegate?.GOCROperationCancel?(self, atIndex: self.imageIndex)
-        
+        self.operationDelegate?.GOCROperation?(self, cancelOCRImageAtIndex: self.imageIndex)
         if isExecuting {
-            isExecuting = false
-            isFinished = true
+            willChangeValue(forKey: "isFinished")
+            hasFinished = true
+            didChangeValue(forKey: "isFinished")
         } else {
-            isFinished = false
+            willChangeValue(forKey: "isCancelled")
+            hasCanceled = true
+            didChangeValue(forKey: "isCancelled")
         }
-        isCancelled = true
     }
     func p_done() {
-        isExecuting = false
-        isFinished = true
+        self.willChangeValue(forKey: "isFinished")
+        self.hasFinished = true
+        self.didChangeValue(forKey: "isFinished")
     }
     func p_checkCancelled() -> Bool { 
         if isCancelled {
-            isFinished = true
+            willChangeValue(forKey: "isFinished")
+            willChangeValue(forKey: "isExecuting")
+            hasExcuting = false
+            hasFinished = true
+            didChangeValue(forKey: "isExecuting")
+            didChangeValue(forKey: "isFinished")
             return true
         }
         return false
     }
     func recognitionImage(_ image: NSImage) {
-        self.operationDelegate?.GOCROperationStart?(self, atIndex: self.imageIndex)
-        
+        self.operationDelegate?.GOCROperation?(self, startOCRImageAtIndex: self.imageIndex)
         let binaryImageData = base64EncodeImage(image)
         
         if binaryImageData == nil {
-            self.operationDelegate?.GOCROperationFail?(self, atIndex: self.imageIndex, err: nil)
+            self.operationDelegate?.GOCROperation?(self, failureOCRImageAtIndex: self.imageIndex, error: nil)
             return
         }
         let urlString = "\(KMGOC_API_URL)?key=\(KMGOC_API_KEY)"
@@ -166,9 +157,9 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
             
             DispatchQueue.main.async {
                 if error != nil || results == nil {
-                    self.operationDelegate?.GOCROperationFail?(self, atIndex: self.imageIndex, err: error)
+                    self.operationDelegate?.GOCROperation?(self, failureOCRImageAtIndex: self.imageIndex, error: error)
                 } else {
-                    self.operationDelegate?.GOCROperationFinish?(self, atIndex: self.imageIndex, results: results ?? [])
+                    self.operationDelegate?.GOCROperation?(self, finishOCRImageAtIndex: self.imageIndex, results: results ?? [])
                 }
             }
             if error != nil || results == nil {
@@ -214,11 +205,16 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
         return compressImageData
     }
     func responseDataResults(_ dictionary: NSDictionary) -> [Any]? { 
-        guard let responses = dictionary["responses"] as? [Any],
-                let responseData = responses.first as? NSDictionary,
-              let errorObj = dictionary["error"] as? NSDictionary else { return nil }
+        let responses = dictionary["responses"] as? [Any]
+        if responses == nil {
+            return nil
+        }
+        let responseData = responses?.first as? NSDictionary
+        if let errorObj = dictionary["error"] as? NSDictionary {
+            return nil
+        }
         var results: [Any]? = nil
-        if let textAnnotations = responseData["textAnnotations"] as? [Any] {
+        if let textAnnotations = responseData?["textAnnotations"] as? [Any] {
             results = [Any]()
             for annotation in textAnnotations {
                 var textBounds = CGRect.zero

+ 0 - 1
PDF Office/PDF Master/Class/PDFTools/OCRNew/Model/KMOCROperation.swift

@@ -41,7 +41,6 @@ protocol KMOCROperationDelegate: AnyObject {
     override var isExecuting: Bool{
         set{
             self.willChangeValue(forKey: "isExecuting")
-            self.isExecuting = newValue
             didChangeValue(forKey: "isExecuting")
         }
         get{