whatever @ Adam Richardson explained is correct along with that add this 1) scaleimage method for increase size of the image(dimensions increase)
func scaleImage(image: UIImage, maxDimension: CGFloat) -> UIImage {
var scaledSize = CGSize(width: maxDimension, height: maxDimension)
var scaleFactor: CGFloat
if image.size.width > image.size.height {
scaleFactor = image.size.height / image.size.width
scaledSize.width = maxDimension
scaledSize.height = scaledSize.width * scaleFactor
} else {
scaleFactor = image.size.width / image.size.height
scaledSize.height = maxDimension
scaledSize.width = scaledSize.height * scaleFactor
}
UIGraphicsBeginImageContext(scaledSize)
image.draw(in: CGRect(x: 0, y: 0, width: scaledSize.width, height: scaledSize.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage!
}
2) store this eng.traineddata language file in filemanager
func storeLanguageFile() throws{
var fileManager: FileManager = FileManager.default
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let docDirectory = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)[0] as NSString
let path: String = docDirectory.appendingPathComponent("/tessdata/eng.traineddata")
if fileManager.fileExists(atPath: path){
var data: NSData = NSData.dataWithContentsOfMappedFile((Bundle.main.resourcePath?.appending("/tessdata/eng.traineddata"))!)! as! NSData
var error: NSError
try FileManager.default.createDirectory(atPath: docDirectory.appendingPathComponent("/tessdata"), withIntermediateDirectories: true, attributes: nil)
data.write(toFile: path, atomically: true)
}
}
3) after that you can use https://github.com/BradLarson/GPUImage for increase clarity of the image
you can use this
func preprocessedImage(for tesseract: G8Tesseract!, sourceImage: UIImage!) -> UIImage! {
var stillImageFilter: GPUImageAdaptiveThresholdFilter = GPUImageAdaptiveThresholdFilter()
stillImageFilter.blurRadiusInPixels = 4.0
var filterImage: UIImage = stillImageFilter.image(byFilteringImage: sourceImage)
return filterImage
}
these 3 steps will help you to increase the accuracy of the tesseract upto 60 ~ 70 %