Created
May 28, 2020 13:41
-
-
Save ozgurshn/1815847b4e420e661e6daa14ded97c8c to your computer and use it in GitHub Desktop.
Vision normalized rectangle detection projection in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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