Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gelişmiş Metin Algılama Teknikleri (Vision + TesserAct OCR) | iOS --- http://www.bulentsiyah.com/gelismis-metin-algilama-teknikleri-vision-tesseract-ocr-ios/
//
// IleriMetinTanimaViewController.swift
// ML Ornekleri
//
// Created by Bülent Siyah on 5.07.2018.
// Copyright © 2018 Bülent Siyah. All rights reserved.
//
import UIKit
import Vision
import TesseractOCR
class IleriMetinTanimaViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var txtResult: UITextView!
var imagePicker: UIImagePickerController!
var userPickedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .scaleAspectFit
imageView.image = pickedImage
self.userPickedImage = pickedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
@IBAction func btnResimCek(_ sender: Any) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
@IBAction func btnAnalizEt(_ sender: Any) {
findText()
recognizeTextImage()
}
func findText() {
// VN Request Rectangles (faces / Text)
var textLayers: [CAShapeLayer] = []
let textDetectionRequest = VNDetectTextRectanglesRequest(completionHandler: { (request, error) in
guard let observations = request.results as? [VNTextObservation] else {
fatalError("unexpected results")
}
//self.addShapesToText
textLayers = self.addShapesToText(forObservations: observations, withImageView: self.imageView)
})
if let image = self.imageView.image, let cgImage = image.cgImage {
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
guard let _ = try? handler.perform([textDetectionRequest]) else {
return print("could not perform the text detection request")
}
for layer in textLayers {
self.imageView.layer.addSublayer(layer)
}
}
}
func addShapesToText(forObservations observations: [VNTextObservation], withImageView textImageView: UIImageView) -> [CAShapeLayer] {
let layers: [CAShapeLayer] = observations.map {observation in
let w = observation.boundingBox.size.width * textImageView.bounds.width
let h = observation.boundingBox.size.height * textImageView.bounds.height
let x = observation.boundingBox.origin.x * textImageView.bounds.width
let y = abs(((observation.boundingBox.origin.y * (textImageView.bounds.height)) - textImageView.bounds.height )) - h
let layer = CAShapeLayer()
layer.frame = CGRect(x: x, y: y, width: w, height: h)
layer.borderColor = UIColor.red.cgColor
layer.cornerRadius = 5
layer.borderWidth = 5
layer.strokeColor = UIColor.red.cgColor
layer.backgroundColor = UIColor.green.cgColor
layer.opacity = 0.5 // 0 - 1
return layer
}
return layers
}
func recognizeTextImage(){
if let tesseract = G8Tesseract(language: "eng+fra"){
tesseract.engineMode = .tesseractCubeCombined
tesseract.pageSegmentationMode = .auto
tesseract.image = userPickedImage?.g8_blackAndWhite()
tesseract.recognize()
txtResult.text = tesseract.recognizedText
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment