ImageCropper icon indicating copy to clipboard operation
ImageCropper copied to clipboard

How does this method calculate the crop area?

Open DiwakarThapa opened this issue 6 years ago • 2 comments

  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)
    }
    

DiwakarThapa avatar Jun 12 '19 08:06 DiwakarThapa

  1. UIKit coordinate space: Origin in the top left corner Max height and width values of the screen size in points
  2. 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.

94cp avatar Jul 10 '19 02:07 94cp

Thanks

DiwakarThapa avatar Jul 11 '19 08:07 DiwakarThapa