Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created May 28, 2020 13:41
Show Gist options
  • Save ozgurshn/1815847b4e420e661e6daa14ded97c8c to your computer and use it in GitHub Desktop.
Save ozgurshn/1815847b4e420e661e6daa14ded97c8c to your computer and use it in GitHub Desktop.
Vision normalized rectangle detection projection in Swift
func observationToRect(box:VNRectangleObservation)->CGRect
{
let xCord = box.topLeft.x * imageView.frame.size.width
let yCord = (1 - box.topLeft.y) * imageView.frame.size.height
let width = (box.topRight.x - box.bottomLeft.x) * imageView.frame.size.width
let height = (box.topLeft.y - box.bottomLeft.y) * imageView.frame.size.height
return CGRect(x: xCord, y: yCord, width: width, height: height)
}
func observationToRect2(box:VNRectangleObservation)->CGRect
{
let bbBox = box.boundingBox
var bottomToTopTransform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -1)
let rect = bbBox.applying(bottomToTopTransform)
return VNImageRectForNormalizedRect(rect, Int(imageView.image!.size.width), Int(imageView.image!.size.height))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment