0

I am able to draw rectangles around text with the CIDetector (Using CIDetectorTypeText), but I can't find a way to detect text orientation. Does anyone know how to detect text orientation with Swift?

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • You could use CIDetectorImageOrientation and detect the text as an image if it isn't already one. – Callam Oct 26 '16 at 14:57
  • I thought `CIDetectorImageOrientation` can only be used to set the orientation, see [this answer](http://stackoverflow.com/questions/36199572/how-to-change-cidetector-orientation) – Daniel Oct 26 '16 at 14:59
  • You use them together https://developer.apple.com/reference/coreimage/citextfeature#overview – Callam Oct 26 '16 at 15:06
  • @Callam but it is the input, not the output – Daniel Oct 26 '16 at 15:12

1 Answers1

2

This will print out the recognized text orientations.

guard let image = CIImage(image: imageView.image!) else { return }

let detector = CIDetector(ofType: CIDetectorTypeText, context: nil, options: [
    CIDetectorAccuracy: CIDetectorAccuracyHigh
])

let orientations = [Int](1..<9).flatMap {
    detector?.features(in: image, options: [
        CIDetectorImageOrientation: $0
    ]).count ?? 0 > 0 ? $0 : nil
}

if orientations.filter({ $0 < 5 }).count == 4 {
    print("text has horizontal orientation")
} else if orientations.filter({ $0 > 4 }).count == 4 {
    print("text has vertical orientation")
} else {
    print("text has mixed orientation")
}
Callam
  • 11,409
  • 2
  • 34
  • 32
  • This doesn't work very good, because it still detects vertical texts, even if a horizontal value is given. – Daniel Oct 27 '16 at 08:17