From c817e9897b83f82a3e5ef7ac123804220d224f50 Mon Sep 17 00:00:00 2001 From: dianshi Date: Thu, 25 Mar 2021 09:18:54 +0000 Subject: Removed default path directory --- OCRImage/main.swift | 57 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'OCRImage') diff --git a/OCRImage/main.swift b/OCRImage/main.swift index cd3fafd..7f7c9f6 100644 --- a/OCRImage/main.swift +++ b/OCRImage/main.swift @@ -118,42 +118,59 @@ if options.version { exit(0) } -//exit if input-file isnot set -//if nil == options.inputFile { -// print("Set --input-file command argument to image") -// exit(0) -//} - guard let inputFile = options.inputFile else { print("ERROR: Set --input-file command argument to image") exit(0) } - -print("OCR Input file: \(inputFile)") +if options.debug { + print("OCR Input file: \(inputFile)") +} //Try to open file from URL -var fm = FileManager.default.homeDirectoryForCurrentUser -//let fm = FileManager.default -print(fm) -let path:String = inputFile -fm.appendPathComponent(path) -let ocrImageUrl = fm.absoluteURL -print(fm) +//var runPath = Bundle.main.executablePath! + +//return true if file excists +func checkIfFileExists(_ fname: String) -> Bool { + let fm = FileManager.default + if fm.fileExists(atPath: fname) { + return true + } + return false +} + +//get current run directory +func getCurrentExecutableDir() -> String { + return Process().currentDirectoryPath +} + +var executionPath = getCurrentExecutableDir() + +var fm_full_path = FileManager.default +fm_full_path.changeCurrentDirectoryPath(executionPath) +var inputURL = URL(fileURLWithPath: fm_full_path.currentDirectoryPath) +inputURL.appendPathComponent(inputFile) +var composePath = "/"+inputURL.pathComponents.filter { ($0 != ".") && ($0 != "/") }.joined(separator: "/") +if (options.debug) { + print(composePath) + print(checkIfFileExists(composePath)) + print(getCurrentExecutableDir()) + print(executionPath) +} + //load image -let image = NSImage(contentsOf: ocrImageUrl) +let composeUrl = URL(fileURLWithPath: composePath) +let image = NSImage(contentsOf: composeUrl) if (image == nil) { - stderr.write("Cannot load image properly \(ocrImageUrl)".data(using: .utf8)!) + stderr.write("Cannot load image properly \(composeUrl)".data(using: .utf8)!) exit(0) } //Start text recognition request var ocr = TextOCR() var err:NSError? -ocr.recognizeImageUrl(ocrImageUrl, err) - - +ocr.recognizeImageUrl(composeUrl, err) print("End programm") -- cgit v1.2.3