Skip to content

Instantly share code, notes, and snippets.

@networkextension
Forked from Koze/HorizonDetection.m
Created June 12, 2017 01:47
Show Gist options
  • Save networkextension/2451a005a297e01c13bb8463024bbc58 to your computer and use it in GitHub Desktop.
Save networkextension/2451a005a297e01c13bb8463024bbc58 to your computer and use it in GitHub Desktop.
Horizon Detection with Vision Framework
- (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