Created
June 2, 2021 19:21
-
-
Save sgl0v/b51255c3d1bd01ba05fadc149cbb448b to your computer and use it in GitHub Desktop.
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 ImageScanner { | |
private func detectRectangle(on image: CIImage, orientation: CGImagePropertyOrientation) -> Result<VNRectangleObservation, Error> { | |
var result: Result<VNRectangleObservation, Error> = .failure(ImageProviderError.internalError) | |
let semaphore = DispatchSemaphore(value: 1) // ➊ | |
let rectanglesRequest = VNDetectRectanglesRequest { request, error in // ➋ | |
guard error == nil, | |
let observations = request.results as? [VNRectangleObservation], | |
let detectedRectangle = observations.first else { | |
return | |
} | |
result = .success(detectedRectangle) | |
semaphore.signal() | |
} | |
let handler = VNImageRequestHandler(ciImage: image, orientation: orientation) // ➌ | |
if (try? handler.perform([rectanglesRequest])) != nil { semaphore.wait() } // ➍ | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment