Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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