Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 62dbdc435e | |||
| 05353281a0 | |||
| 67bd7b5414 | |||
| 7adca65b19 | |||
| 708e711433 | |||
| d158aeeeac | |||
| 6997d5dad0 | |||
| 43c06423c4 | |||
| b898780cf9 |
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user