4 Commits

Author SHA1 Message Date
Michael 62dbdc435e Merge pull request #6 from olcayertas/patch-2
Update Utils.swift
2018-04-09 13:45:10 +03:00
Olcay Ertaş 05353281a0 Update Utils.swift
Updated to Swift 4
2018-04-09 11:28:21 +03:00
Sergey Pronin 67bd7b5414 Merge pull request #5 from olcayertas/patch-1
Update StringExtension.swift
2018-04-09 10:21:10 +02:00
Olcay Ertaş 7adca65b19 Update StringExtension.swift
Updated to Swift 4 String Collections
2018-04-09 10:54:27 +03:00
2 changed files with 26 additions and 41 deletions
@@ -4,18 +4,18 @@ extension String {
subscript (i: Int) -> Character {
let index = self.index(self.startIndex, offsetBy: i)
return self.characters[index]
return self[index]
}
func substring(from: Int, to: Int) -> String {
let fromIndex = self.index(self.startIndex, offsetBy: from)
let toIndex = self.index(self.startIndex, offsetBy: to + 1)
return self.substring(with: fromIndex ..< toIndex)
return String(self[fromIndex..<toIndex])
}
func replaceNumbers() -> String {
var result = ""
for char in self.characters {
for char in self {
switch char {
case "0": result.append("O")
case "1": result.append("I")
@@ -27,34 +27,28 @@ extension String {
case "7": result.append("Z")
case "8",
"9": result.append("B")
default: result.append(char)
}
}
return result
}
func replaceLetters() -> String {
var result = ""
for char in self.characters {
for char in self {
switch char {
case "O",
"D": result.append("0")
case "I",
"L": result.append("1")
case "S": result.append("5")
case "A": result.append("4")
case "G": result.append("6")
case "Z": result.append("7")
case "B": result.append("8")
default: result.append(char)
}
}
return result
}
}
+22 -31
View File
@@ -23,51 +23,36 @@ open class Utils {
}
static func mrCodeFrom(image: UIImage, tesseractDelegate: G8TesseractDelegate? = nil) -> String? {
tesseract.delegate = tesseractDelegate!
tesseract.image = image.recognitionImage
tesseract.recognize()
if let recognizedText = tesseract.recognizedText {
NSLog("Recognized: \(recognizedText)")
NSLog("Utils : mrCodeFrom : Recognized: \(recognizedText)")
let text = recognizedText.replacingOccurrences(of: " ", with: "")
let regex = try? NSRegularExpression(pattern: passportPattern, options: [])
let range = NSRange(location: 0, length: text.characters.count)
let range = NSRange(location: 0, length: text.count)
if let result = regex!.firstMatch(in: text, options: [], range: range) {
let code = (text as NSString).substring(with: result.range)
return fixFirstRowIn(code: code)
}
}
return nil
}
fileprivate static func fixFirstRowIn(code: String) -> String {
let pattern = "(?<FirstLine>(?<Passport>[A-Z0-9])(?<PassportType>.)(?<IssuingCountry>[A-Z0-9]{3})(?<PassportOwner>(?<Surname>[A-Z0-9]+)<<(?<GivenName>(?:[A-Z0-9]+<)+)){1})"
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: code.characters.count)
let range = NSRange(location: 0, length: code.count)
let result = regex!.matches(in: code, options: [], range: range)
var resultFirstRow = (code as NSString).substring(with: result[0].range)
while resultFirstRow.characters.count != 44 {
resultFirstRow.append("<")
}
let secondRow = code.characters.split(separator: "\n", maxSplits: 2, omittingEmptySubsequences: true)[1]
while resultFirstRow.count != 44 {resultFirstRow.append("<")}
let secondRow = code.split(separator: "\n", maxSplits: 2, omittingEmptySubsequences: true)[1]
return "\(resultFirstRow)\n\(String(secondRow))\n"
}
fileprivate static func createTesseract() -> G8Tesseract {
let trainDataPath = bundle.path(forResource: "eng", ofType: "traineddata")
let cacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let tessdataURL = cacheURL.appendingPathComponent("tesseract", isDirectory: true).appendingPathComponent("tessdata", isDirectory: true)
let destinationURL = tessdataURL.appendingPathComponent("eng.traineddata")
@@ -75,30 +60,36 @@ open class Utils {
createTessdataFrom(trainDataPath!, toDirectoryURL: tessdataURL, withDestinationURL: destinationURL)
}
NSLog("\(cacheURL.path)")
NSLog("\(tessdataURL.path)")
NSLog("\(destinationURL.path)")
let tesseract = G8Tesseract(language: "eng", configDictionary: [:], configFileNames: [], cachesRelatedDataPath: "tesseract/tessdata", engineMode: .tesseractOnly)
print("Utils : createTesseract : Cache path = \(cacheURL.path)")
print("Utils : createTesseract : Tess data path = \(tessdataURL.path)")
print("Utils : createTesseract : Destination path = \(destinationURL.path)")
let tesseract = G8Tesseract(
language: "eng",
configDictionary: [:],
configFileNames: [],
cachesRelatedDataPath: "tesseract/tessdata",
engineMode: .tesseractOnly)
var whiteList = DOConstants.alphabet.uppercased()
whiteList.append("<>1234567890")
tesseract?.charWhitelist = whiteList
tesseract?.setVariableValue("FALSE", forKey: "x_ht_quality_check")
return tesseract!
}
fileprivate static func createTessdataFrom(_ filePath: String, toDirectoryURL tessdataURL: URL, withDestinationURL destinationURL: URL) {
fileprivate static func createTessdataFrom(
_ filePath: String,
toDirectoryURL tessdataURL: URL,
withDestinationURL destinationURL: URL) {
do {
let fileManager = FileManager.default
try fileManager.createDirectory(atPath: tessdataURL.path,
withIntermediateDirectories: true, attributes: nil)
try fileManager.createDirectory(atPath: tessdataURL.path, withIntermediateDirectories: true, attributes: nil)
try fileManager.copyItem(atPath: filePath, toPath: destinationURL.path)
}
catch let error as NSError {
assertionFailure("There is no tessdata directory in cache (TesseractOCR traineddata). \(error.localizedDescription)")
assertionFailure("There is no tessdata directory in cache (TesseractOCR traineddata)."
+ "\(error.localizedDescription)")
}
}
}