Created
May 26, 2020 10:37
-
-
Save ozgurshn/4b49452b5a40d3198e4ec85c1caa52d7 to your computer and use it in GitHub Desktop.
Apply perpective transform to the Vision rectangle observation
This file contains hidden or 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
| guard let rectangle = request?.results?.first as? VNRectangleObservation else { | |
| guard let error = error else { return } | |
| print("Error: Rectangle detection failed - Vision request returned an error. \(error.localizedDescription)") | |
| return | |
| } | |
| guard let filter = CIFilter(name: "CIPerspectiveCorrection") else { | |
| print("Error: Rectangle detection failed - Could not create perspective correction filter.") | |
| return | |
| } | |
| let width = CGFloat(CVPixelBufferGetWidth(currentCameraImage)) | |
| let height = CGFloat(CVPixelBufferGetHeight(currentCameraImage)) | |
| let topLeft = CGPoint(x: rectangle.topLeft.x * width, y: rectangle.topLeft.y * height) | |
| let topRight = CGPoint(x: rectangle.topRight.x * width, y: rectangle.topRight.y * height) | |
| let bottomLeft = CGPoint(x: rectangle.bottomLeft.x * width, y: rectangle.bottomLeft.y * height) | |
| let bottomRight = CGPoint(x: rectangle.bottomRight.x * width, y: rectangle.bottomRight.y * height) | |
| filter.setValue(CIVector(cgPoint: topLeft), forKey: "inputTopLeft") | |
| filter.setValue(CIVector(cgPoint: topRight), forKey: "inputTopRight") | |
| filter.setValue(CIVector(cgPoint: bottomLeft), forKey: "inputBottomLeft") | |
| filter.setValue(CIVector(cgPoint: bottomRight), forKey: "inputBottomRight") | |
| let ciImage = CIImage(cvPixelBuffer: currentCameraImage).oriented(.up) | |
| filter.setValue(ciImage, forKey: kCIInputImageKey) | |
| guard let perspectiveImage: CIImage = filter.value(forKey: kCIOutputImageKey) as? CIImage else { | |
| print("Error: Rectangle detection failed - perspective correction filter has no output image.") | |
| return | |
| } | |
| delegate?.rectangleFound(rectangleContent: perspectiveImage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment