Created
May 18, 2018 00:12
-
-
Save jamesrochabrun/44775cdf29a0d8b4d35ee101dcf3fc08 to your computer and use it in GitHub Desktop.
VNFaceObservation extension for calculate the overall bounding box that contains face observation bounding boxes.
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
extension VNFaceObservation { | |
static func overAllBoundingBoxFrom(boundingBoxes: [CGRect], tolerance: CGFloat) -> CGRect { | |
/// Sort Max X coordinates | |
let originXCoordinates = boundingBoxes.map { $0.minX } | |
/// Sort Max Y coordinates | |
let originYCoordinates = boundingBoxes.map { $0.minY } | |
/// Find overall minX | |
guard let minOverallX = originXCoordinates.min() else { return CGRect.zero } | |
/// Find overall minY | |
guard let minOveralY = originYCoordinates.min() else { return CGRect.zero } | |
/// Find overall maxX | |
///Find originY + height = rect.maxY | |
let maxYFromRects = boundingBoxes.map { $0.maxY } | |
///Find originX + width = rect.maxX | |
let maxXFromRects = boundingBoxes.map { $0.maxX } | |
/// Find overall maxX | |
guard let maxOverallX = maxXFromRects.max() else { return CGRect.zero } | |
/// Find overall maxY | |
guard let maxOverallY = maxYFromRects.max() else { return CGRect.zero } | |
let finalWidth = maxOverallX - minOverallX + tolerance * 2 | |
let finalHeight = maxOverallY - minOveralY + tolerance * 2 | |
let overallOriginY = minOveralY - tolerance | |
let overallOriginX = minOverallX - tolerance | |
return CGRect(x: overallOriginX, y: overallOriginY, width: finalWidth, height: finalHeight) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment