Created
June 7, 2017 18:39
-
-
Save Koze/71704d5d04efb8d34c84a4f77ad70b2a to your computer and use it in GitHub Desktop.
Horizon Detection with Vision Framework
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
- (void)correctAngleWithImage:(UIImage *)image | |
{ | |
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCGImage:image.CGImage options:@{}]; | |
VNDetectHorizonRequest *request = [[VNDetectHorizonRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) { | |
if (error) { | |
NSLog(@"%@", error); | |
} | |
else { | |
NSAssert(request.results.count > 0, @"No Results"); | |
VNHorizonObservation *horizonObservation = request.results.firstObject; | |
NSLog(@"%@", horizonObservation); | |
NSLog(@"%f %@", horizonObservation.angle, NSStringFromCGAffineTransform(horizonObservation.transform)); | |
self.imageView.transform = CGAffineTransformMakeRotation(-horizonObservation.angle); | |
// self.imageView.transform = CGAffineTransformInvert(CGAffineTransformMakeRotation(horizonObservation.angle)); | |
// self.imageView.transform = CGAffineTransformInvert(horizonObservation.transform); | |
} | |
}]; | |
NSError *error; | |
[handler performRequests:@[request] error:&error]; | |
if (error) { | |
NSLog(@"%@", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment