Skip to content

Commit

Permalink
Merge pull request #2 from Shakshi3104/develop
Browse files Browse the repository at this point in the history
Add the option for showing supported language and setting recognition language
  • Loading branch information
Shakshi3104 authored Dec 4, 2022
2 parents 7815ad3 + 4d0b427 commit 0352b79
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
42 changes: 38 additions & 4 deletions LiTeX/LiveTextCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,45 @@ 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
)

@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
Expand Down Expand Up @@ -62,12 +86,17 @@ extension Litex {
return
}

guard let imageFilepath = imageFilepath else {
print("Error: Missing expected argument '<image-filepath>'")
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
Expand Down Expand Up @@ -98,6 +127,11 @@ extension Litex {

// MARK: - recognize text via VNRecognizeTextRequest (Vision)
func recognizeTextByVision() {
guard let imageFilepath = imageFilepath else {
print("Error: Missing expected argument '<image-filepath>'")
return
}

// convert image filepath string to URL
let imageURL = URL(fileURLWithPath: imageFilepath)

Expand Down Expand Up @@ -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)")
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ On macOS 12, LiTeX uses [VNRecognizeTextRequest of Vision](https://developer.app
## Usage

```sh
USAGE: litex <image-filepath> [--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 [<image-filepath>] [--use-vision] [--lang <lang>] [--support-lang]

ARGUMENTS:
<image-filepath> 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 <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.
```
Expand Down

0 comments on commit 0352b79

Please sign in to comment.