9 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
Mihail Babaev 708e711433 update podspec version 2017-12-10 19:10:38 +03:00
Michael d158aeeeac Merge pull request #4 from nishabe/bugfix/documents-ocr-ios_issue_3
This fixes the crash while tapping on camera button on Xcode 9.1
2017-12-10 19:05:52 +03:00
Abraham, Aneesh 6997d5dad0 This fixes the crash while tapping on camera button on Xcode 9.1 2017-12-09 23:16:53 -06:00
Michael 43c06423c4 Merge pull request #2 from manhlx3006/patch-1
Crash when clicked stop taking photo due to nil of timer
2017-09-13 07:31:04 +03:00
manhlx3006 b898780cf9 Crash when clicked stop taking photo due to nil of timer 2017-09-13 01:18:58 +08:00
5 changed files with 33 additions and 46 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = 'DocumentsOCR'
s.version = '1.0.5'
s.version = '1.0.6'
s.summary = 'A Swift framework for machine readable documents recognition'
# This description is used to generate tags and improve search results.
@@ -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)")
}
}
}
@@ -57,7 +57,7 @@ open class DocumentScanner: NSObject {
/// Recognized document information from RecognitionOperation
open var recognizedDocumentInfo: DocumentInfo? = nil
var timer: Timer!
var timer: Timer?
var codes = [String]()
var images = [UIImage]()
var recognizedInfo: DocumentInfo? = nil
@@ -137,7 +137,7 @@ open class DocumentScanner: NSObject {
extension DocumentScanner: CameraViewDelegate {
func stopTakingPictures() {
timer.invalidate()
timer?.invalidate()
containerViewController.dismiss(animated: true, completion: nil)
}
+4 -2
View File
@@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
@@ -22,6 +20,8 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
@@ -37,5 +37,7 @@
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
<key>NSCameraUsageDescription</key>
<string>Used to scan documents</string>
</dict>
</plist>