From e67b65f863586a4c025bcd262e757fc99b9c7725 Mon Sep 17 00:00:00 2001 From: Shakshi3104 <51497924+Shakshi3104@users.noreply.github.com> Date: Sun, 4 Dec 2022 17:48:14 +0900 Subject: [PATCH 1/3] Add `lang` and `support-lang` options --- LiTeX/LiveTextCLI.swift | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/LiTeX/LiveTextCLI.swift b/LiTeX/LiveTextCLI.swift index cf20b6e..b49a353 100644 --- a/LiTeX/LiveTextCLI.swift +++ b/LiTeX/LiveTextCLI.swift @@ -24,16 +24,40 @@ struct Litex: AsyncParsableCommand { ) @Argument(help: "An image filepath.") - var imageFilepath: String + var imageFilepath: String? @Flag(help: "Use VNRecognizeTextRequest of Vision. This option is only available on macOS 13 and newer.") var useVision: Bool = false + + @Option(help: "Set recognition language. If you want to check supported languages, run `litex --support-lang`. This option is for VNRecognizeTextRequest.") + var lang: String = "ja-JP" + + @Flag(help: "Show supported languages.") + var supportLang: Bool = false } // MARK: - Text recognition extension Litex { func run() async throws { + if supportLang { + // show supported languages by VNRecognizeTextRequest + let request = VNRecognizeTextRequest(completionHandler: { request, error in + }) + + do { + let supportedLanguages = try request.supportedRecognitionLanguages() + print(supportedLanguages.joined(separator: "\n")) + } catch { + print("Unable to perform the request: \(error).") + } + } else { + // recognize + try? await recognize() + } + } + + func recognize() async throws { if #available(macOS 13.0, *) { if useVision { // recognize by Vision @@ -62,12 +86,17 @@ extension Litex { return } + guard let imageFilepath = imageFilepath else { + print("Error: Missing expected argument ''") + return + } + // convert image filepath string to URL let imageURL = URL(filePath: imageFilepath) // setup ImageAnalyzer var configuration = ImageAnalyzer.Configuration([.text]) - configuration.locales = ["ja-JP", "en-US"] + configuration.locales = [lang] let analyzer = ImageAnalyzer() // analyze the image @@ -98,6 +127,11 @@ extension Litex { // MARK: - recognize text via VNRecognizeTextRequest (Vision) func recognizeTextByVision() { + guard let imageFilepath = imageFilepath else { + print("Error: Missing expected argument ''") + return + } + // convert image filepath string to URL let imageURL = URL(fileURLWithPath: imageFilepath) @@ -129,7 +163,7 @@ extension Litex { // perform do { - request.recognitionLanguages = ["ja-JP", "en-US"] + request.recognitionLanguages = [lang] #if DEBUG let supportedLanguages = try request.supportedRecognitionLanguages() print("⚙️ Supported Languages: \(supportedLanguages)") From 6aa4c1324194502a42dad27c74d5d5a7832de74c Mon Sep 17 00:00:00 2001 From: Shakshi3104 <51497924+Shakshi3104@users.noreply.github.com> Date: Sun, 4 Dec 2022 17:49:15 +0900 Subject: [PATCH 2/3] Update version --- LiTeX/LiveTextCLI.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LiTeX/LiveTextCLI.swift b/LiTeX/LiveTextCLI.swift index b49a353..1dce0e3 100644 --- a/LiTeX/LiveTextCLI.swift +++ b/LiTeX/LiveTextCLI.swift @@ -19,7 +19,7 @@ struct Litex: AsyncParsableCommand { discussion: """ LiTeX allows use Live Text as the command line tool and output results to a text file. """, - version: "1.1.0", + version: "1.2.0", shouldDisplay: true ) From 4d0b4270ebaa061418e2367cfc4c296c192ffa18 Mon Sep 17 00:00:00 2001 From: Shakshi3104 <51497924+Shakshi3104@users.noreply.github.com> Date: Sun, 4 Dec 2022 17:51:19 +0900 Subject: [PATCH 3/3] Update README --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6196e1a..f1f4ebd 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,22 @@ On macOS 12, LiTeX uses [VNRecognizeTextRequest of Vision](https://developer.app ## Usage ```sh -USAGE: litex [--use-vision] +OVERVIEW: Command line tool of Live Text + +LiTeX allows use Live Text as the command line tool and output results to a text file. + +USAGE: litex [] [--use-vision] [--lang ] [--support-lang] ARGUMENTS: An image filepath. OPTIONS: --use-vision Use VNRecognizeTextRequest of Vision. This option is only - available on macOS 13. + available on macOS 13 and newer. + --lang Set recognition language. If you want to check supported + languages, run `litex --support-lang`. This option is for + VNRecognizeTextRequest. (default: ja-JP) + --support-lang Show supported languages. --version Show the version. -h, --help Show help information. ```