Verified Commit 19e18882 authored by Geoff Pado's avatar Geoff Pado

Detect individual character observations

parent 3c078f04
......@@ -33,4 +33,17 @@ extension CGRect {
return fittingRect
}
}
static func flippedRect(from rect: CGRect, scaledTo size: CGSize) -> CGRect {
var scaledRect = rect
scaledRect.origin.y = (1.0 - scaledRect.origin.y)
scaledRect.origin.x *= size.width
scaledRect.origin.y *= size.height
scaledRect.size.width *= size.width
scaledRect.size.height *= size.height
scaledRect.origin.y -= scaledRect.size.height
return scaledRect.integral
}
}
......@@ -17,13 +17,22 @@ class PhotoEditingObservationVisualizationView: UIView {
guard let textObservations = textObservations else { return }
UIColor.red.withAlphaComponent(0.3).setFill()
UIColor.red.setStroke()
textObservations.forEach { observation in
UIColor.red.withAlphaComponent(0.3).setFill()
UIColor.red.setStroke()
let boundsPath = UIBezierPath(rect: observation.bounds)
boundsPath.fill()
boundsPath.stroke()
UIColor.blue.withAlphaComponent(0.3).setFill()
UIColor.blue.setStroke()
observation.characterObservations?.forEach { characterObservation in
let boundsPath = UIBezierPath(rect: characterObservation.bounds)
boundsPath.fill()
boundsPath.stroke()
}
}
}
......
......@@ -6,18 +6,19 @@ import Vision
struct DetectedTextObservation {
init(_ textObservation: VNTextObservation, in image: UIImage) {
var boundingBox = textObservation.boundingBox
boundingBox.origin.y = (1.0 - boundingBox.origin.y)
let boundingBox = textObservation.boundingBox
let imageSize = image.size * image.scale
boundingBox.origin.x *= imageSize.width
boundingBox.origin.y *= imageSize.height
boundingBox.size.width *= imageSize.width
boundingBox.size.height *= imageSize.height
boundingBox.origin.y -= boundingBox.size.height
self.bounds = CGRect.flippedRect(from: boundingBox, scaledTo: imageSize)
self.bounds = boundingBox.integral
self.characterObservations = textObservation.characterBoxes?.map {
DetectedCharacterObservation(bounds: CGRect.flippedRect(from: $0.boundingBox, scaledTo: imageSize))
}
}
let bounds: CGRect
let characterObservations: [DetectedCharacterObservation]?
}
struct DetectedCharacterObservation {
let bounds: CGRect
}
......@@ -28,6 +28,7 @@ class TextRectangleDetectionOperation: Operation {
self?._finished = true
self?._executing = false
}
imageRequest.reportCharacterBoxes = true
do {
try imageRequestHandler.perform([imageRequest])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment