Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine code style #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 31 additions & 47 deletions ocr/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ import Vision
import ScreenCapture
import ArgumentParserKit

// MARK: Global varibles

var joiner = " "
var bigSur = false;
var bigSur = false
var recognitionLanguages = ["zh-CN"]

if #available(OSX 11, *) {
bigSur = true;
}

// MARK: Helper functions

func convertCIImageToCGImage(inputImage: CIImage) -> CGImage? {
let context = CIContext(options: nil)
if let cgImage = context.createCGImage(inputImage, from: inputImage.extent) {
return cgImage
}
return nil
guard let cgImage = context.createCGImage(inputImage, from: inputImage.extent) else { return nil }
return cgImage
}

func recognizeTextHandler(request: VNRequest, error: Error?) {
guard let observations =
request.results as? [VNRecognizedTextObservation] else {
return
}
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
Expand All @@ -45,62 +45,46 @@ func recognizeTextHandler(request: VNRequest, error: Error?) {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(joined, forType: .string)

}

func detectText(fileName : URL) -> [CIFeature]? {
if let ciImage = CIImage(contentsOf: fileName){
guard let img = convertCIImageToCGImage(inputImage: ciImage) else { return nil}

let requestHandler = VNImageRequestHandler(cgImage: img)
func detectText(fileName : URL) {
guard let ciImage = CIImage(contentsOf: fileName) else { return }
guard let img = convertCIImageToCGImage(inputImage: ciImage) else { return}

// Create a new request to recognize text.
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
request.recognitionLanguages = recognitionLanguages


do {
// Perform the text-recognition request.
try requestHandler.perform([request])
} catch {
print("Unable to perform the requests: \(error).")
}
}
return nil
// Create a new request to recognize text.
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
request.recognitionLanguages = recognitionLanguages

do {
let requestHandler = VNImageRequestHandler(cgImage: img)
// Perform the text-recognition request.
try requestHandler.perform([request])
} catch {
print("Unable to perform the requests: \(error).")
}
}



let inputURL = URL(fileURLWithPath: "/tmp/ocr.png")
var recognitionLanguages = ["en-US"]
// MARK: 🉑 Start OCR ...

do {


let arguments = Array(CommandLine.arguments.dropFirst())

let parser = ArgumentParser(usage: "<options>", overview: "macOCR is a command line app that enables you to turn any text on your screen into text on your clipboard")

if(bigSur){
if (bigSur) {
let languageOption = parser.add(option: "--language", shortName: "-l", kind: String.self, usage: "Set Language (Supports Big Sur and Above)")


let parsedArguments = try parser.parse(arguments)
let language = parsedArguments.get(languageOption)

if (language ?? "").isEmpty{

}else{
recognitionLanguages.insert(language!, at: 0)
if let language = language, !language.isEmpty {
recognitionLanguages.insert(language, at: 0)
}
}


let inputURL = URL(fileURLWithPath: "/tmp/ocr.png")
let _ = ScreenCapture.captureRegion(destination: "/tmp/ocr.png")

if let features = detectText(fileName : inputURL), !features.isEmpty{}

detectText(fileName : inputURL)
} catch {
// handle parsing error
print(error)
}

exit(EXIT_SUCCESS)