ImageCropper
ImageCropper copied to clipboard
How does this method calculate the crop area?
private func cropImage(object: VNDetectedObjectObservation, padding: UIEdgeInsets) -> (image: CGImage, frame: CGRect)? {
let width = object.boundingBox.width * CGFloat(detectable.width)
let height = object.boundingBox.height * CGFloat(detectable.height)
let x = object.boundingBox.origin.x * CGFloat(detectable.width)
let y = (1 - object.boundingBox.origin.y) * CGFloat(detectable.height) - height
let croppingRect = CGRect(x: x - padding.left, y: y - padding.top, width: width + (padding.left + padding.right), height: height + (padding.top + padding.bottom))
guard let image = self.detectable.cropping(to: croppingRect) else { return nil }
return (image: image, frame: croppingRect)
}
- UIKit coordinate space: Origin in the top left corner Max height and width values of the screen size in points
- Vision coordinate space: Origin in the bottom left corner Max height and width of 1
There are 2 different coordinate systems we have to convert between.
I found that the Vision cannot fully recognize faces, so I added a padding property.
Thanks