this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

can recursively loop through a directory and output json

alice de2fcd5b 64f1b8c6

+49 -67
+1 -1
.gitignore
··· 29 29 *.xcuserstate 30 30 project.xcworkspace 31 31 32 - 32 + ocrbin
+4 -2
OCR.xcodeproj/project.pbxproj
··· 178 178 GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 179 179 GCC_WARN_UNUSED_FUNCTION = YES; 180 180 GCC_WARN_UNUSED_VARIABLE = YES; 181 - MACOSX_DEPLOYMENT_TARGET = 12.0; 181 + MACOSX_DEPLOYMENT_TARGET = 14.0; 182 182 MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 183 183 MTL_FAST_MATH = YES; 184 184 ONLY_ACTIVE_ARCH = YES; ··· 233 233 GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 234 234 GCC_WARN_UNUSED_FUNCTION = YES; 235 235 GCC_WARN_UNUSED_VARIABLE = YES; 236 - MACOSX_DEPLOYMENT_TARGET = 12.0; 236 + MACOSX_DEPLOYMENT_TARGET = 14.0; 237 237 MTL_ENABLE_DEBUG_INFO = NO; 238 238 MTL_FAST_MATH = YES; 239 239 SDKROOT = macosx; ··· 246 246 isa = XCBuildConfiguration; 247 247 buildSettings = { 248 248 CODE_SIGN_STYLE = Automatic; 249 + MACOSX_DEPLOYMENT_TARGET = 14.0; 249 250 PRODUCT_NAME = "$(TARGET_NAME)"; 250 251 SWIFT_VERSION = 5.0; 251 252 }; ··· 255 256 isa = XCBuildConfiguration; 256 257 buildSettings = { 257 258 CODE_SIGN_STYLE = Automatic; 259 + MACOSX_DEPLOYMENT_TARGET = 14.0; 258 260 PRODUCT_NAME = "$(TARGET_NAME)"; 259 261 SWIFT_VERSION = 5.0; 260 262 };
+42 -64
OCR/main.swift
··· 8 8 import Vision 9 9 import Cocoa 10 10 11 - var MODE = VNRequestTextRecognitionLevel.accurate // or .fast 12 - var USE_LANG_CORRECTION = false 13 - var REVISION:Int 14 - if #available(macOS 11, *) { 15 - REVISION = VNRecognizeTextRequestRevision2 16 - } else { 17 - REVISION = VNRecognizeTextRequestRevision1 18 - } 19 - 11 + var MODE = VNRequestTextRecognitionLevel.accurate 12 + var USE_LANG_CORRECTION = true 13 + var REVISION = VNRecognizeTextRequestRevision3 20 14 func main(args: [String]) -> Int32 { 21 - 22 - if CommandLine.arguments.count == 2 { 23 - if args[1] == "--langs" { 24 - let request = VNRecognizeTextRequest.init() 25 - request.revision = REVISION 26 - request.recognitionLevel = VNRequestTextRecognitionLevel.accurate 27 - let langs = try? request.supportedRecognitionLanguages() 28 - for lang in langs! { 29 - print(lang) 30 - } 31 - } 32 - return 0 33 - }else if CommandLine.arguments.count == 6 { 34 - let (language, fastmode, languageCorrection, src, dst) = (args[1], args[2],args[3],args[4],args[5]) 35 - let substrings = language.split(separator: ",") 36 - var languages:[String] = [] 37 - for substring in substrings { 38 - languages.append(String(substring)) 39 - } 40 - if fastmode == "true" { 41 - MODE = VNRequestTextRecognitionLevel.fast 42 - }else{ 43 - MODE = VNRequestTextRecognitionLevel.accurate 44 - } 45 - 46 - if languageCorrection == "true" { 47 - USE_LANG_CORRECTION = true 48 - }else{ 49 - USE_LANG_CORRECTION = false 15 + let language = "en-US" 16 + var languages:[String] = [] 17 + languages.append(language) 18 + let url = URL(fileURLWithPath: args[1]) 19 + var files = [URL]() 20 + if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { 21 + for case let fileURL as URL in enumerator { 22 + do { 23 + let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey]) 24 + if fileAttributes.isRegularFile! && fileURL.pathExtension.lowercased().range(of: "(jpe?g|png|heic)$", options: .regularExpression) != nil { 25 + files.append(fileURL) 26 + } 27 + } catch { print(error, fileURL) } 50 28 } 51 - 52 - guard let img = NSImage(byReferencingFile: src) else { 53 - fputs("Error: failed to load image '\(src)'\n", stderr) 29 + // print(files) 30 + } 31 + 32 + for file in files { 33 + guard let img = NSImage(byReferencing: file) as NSImage? else { 34 + fputs("Error: failed to load image '\(file)'\n", stderr) 54 35 return 1 55 36 } 56 37 guard let imgRef = img.cgImage(forProposedRect: nil, context: nil, hints: nil) else { 57 - fputs("Error: failed to convert NSImage to CGImage for '\(src)'\n", stderr) 38 + fputs("Error: failed to convert NSImage to CGImage for '\(file)'\n", stderr) 58 39 return 1 59 40 } 60 - 61 - 41 + 62 42 let request = VNRecognizeTextRequest { (request, error) in 63 43 let observations = request.results as? [VNRecognizedTextObservation] ?? [] 64 44 var dict:[String:Any] = [:] ··· 81 61 let rect = VNImageRectForNormalizedRect(boundingBox, 82 62 Int(imgRef.width), 83 63 Int(imgRef.height)) 84 - 64 + 85 65 line["text"] = string ?? "" 86 66 line["confidence"] = confidence ?? "" 87 67 line["x"] = Int(rect.minX) ··· 92 72 allText = allText + (string ?? "") 93 73 index = index + 1 94 74 if index != observations.count { 95 - allText = allText + "\n" 75 + allText = allText + "\n" 96 76 } 97 77 } 98 78 dict["lines"] = lines 99 79 dict["text"] = allText 100 80 let data = try? JSONSerialization.data(withJSONObject: dict, options: []) 101 - let jsonString = String(data: data!, 102 - encoding: .utf8) ?? "[]" 103 - try? jsonString.write(to: URL(fileURLWithPath: dst), atomically: true, encoding: String.Encoding.utf8) 81 + let jsonString = String(data: data!, encoding: .utf8) ?? "[]" 82 + let dstFile = file.absoluteString + ".json" 83 + do { 84 + try jsonString.write(to: URL(string: dstFile)!, atomically: true, encoding: String.Encoding.utf8) 85 + } catch { 86 + print("Unexpected error writing JSON: \(error).") 87 + exit(1) 88 + } 104 89 } 105 90 request.recognitionLevel = MODE 106 91 request.usesLanguageCorrection = USE_LANG_CORRECTION ··· 108 93 request.recognitionLanguages = languages 109 94 //request.minimumTextHeight = 0 110 95 //request.customWords = [String] 111 - try? VNImageRequestHandler(cgImage: imgRef, options: [:]).perform([request]) 112 - 113 - return 0 114 - }else{ 115 - print(""" 116 - usage: 117 - language fastmode languageCorrection image_path output_path 118 - --langs: list suppported languages 119 - 120 - example: 121 - macOCR en false true ./image.jpg out.json 122 - """) 123 - return 1 96 + do { 97 + try VNImageRequestHandler(cgImage: imgRef, options: [:]).perform([request]) 98 + } catch { 99 + print("Unexpected error with OCR: \(error).") 100 + exit(1) 101 + } 102 + print("\(file) done") 124 103 } 104 + return 0 125 105 } 126 - 127 - 128 106 129 107 exit(main(args: CommandLine.arguments))
+2
compile.sh
··· 1 + #!/bin/bash 2 + xcrun -sdk macosx swiftc OCR/main.swift -o ocrbin