[英]Tesseract in iOS (Swift) - How to Separate Text and Numbers in UITextField?
我有一個基於Swift的應用程序,該應用程序當前實現了Tesseract OCR框架(類似於本教程中的表格: http : //www.raywenderlich.com/93276/implementing-tesseract-ocr-ios )。 因此,在拍照並使用Tesseract時,我在UITextField對象中獲得以下輸出:
Subtotal 155.60
Tax 14.02
Total 169.82
所以現在我想將文本與UITextField中的數字分開。 我正在考慮在包含價格格式的所有值的矩陣([0.01 0.02等])上使用Swift內置的“包含”功能,但這只會返回本文所述的布爾值( 如何進行文本字段掃描)迅速將數組中的所有值單獨添加? )。 有人對此有任何建議嗎? 干杯!
Tesseract實施
func performImageRecognition(image: UIImage)
// 0
// 1
let tesseract = G8Tesseract()
// 2
tesseract.language = "eng"
// 3
tesseract.engineMode = .TesseractCubeCombined
// 4
tesseract.pageSegmentationMode = .Auto
// 5
tesseract.maximumRecognitionTime = 60.0
// 6
tesseract.image = image.g8_blackAndWhite()
tesseract.recognize()
// 7
textView.text = tesseract.recognizedText
textView.editable = true
聽起來您可能想研究使用正則表達式
func seperate (text: String) -> (text: String?, value: String?) {
// You might want to do an extra check here to ensure the whole string is valid
// i.e., nothing in between the two parts of the string
let textMatch = text.rangeOfString("^([A-Z]|[a-z])+", options: .RegularExpressionSearch)
let priceMatch = text.rangeOfString("[0-9]*.[0-9]{2}$", options: .RegularExpressionSearch)
// You might want to adjust regex to handle price edge cases, such as 15 (rather than 15.00) etc
if let textMatch = textMatch, priceMatch = priceMatch {
let textValue = text.substringWithRange(textMatch)
let priceValue = text.substringWithRange(priceMatch)
return(textValue, priceValue)
} else {
return (nil, nil)
}
}
seperate("Subtotal 155.60") // -> Subtotal, 155.60
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.