Установить VNImageOptionCameraIntrinsics из ARCamera - PullRequest
0 голосов
/ 02 сентября 2018

Я создаю приложение, которое сочетает в себе ARKit с CoreML. Я передаю кадры на VNImageRequestHandler, используя следующие строки:

// the frame of the current Scene
CVPixelBufferRef pixelBuffer = _cameraPreview.session.currentFrame.capturedImage;

NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCVPixelBuffer:pixelBuffer options:requestOptions];

Обратите внимание на requestOptions. Он должен содержать поле VNImageOptionCameraIntrinsics, которое передает встроенные функции камеры в CoreML.

Перед использованием ARKit я использовал CMSampleBufferRef для получения изображений с камеры. Признаки могут быть получены и установлены с использованием следующего:

CFTypeRef cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil);
requestOptions[VNImageOptionCameraIntrinsics] = (__bridge id)(cameraIntrinsicData);

Однако сейчас я использую ARFrame, но я все еще хочу установить правильные внутренние параметры, поскольку pixelBuffer вращается.

Глядя на документы:

https://developer.apple.com/documentation/vision/vnimageoption?language=objc

https://developer.apple.com/documentation/arkit/arcamera/2875730-intrinsics?language=objc

Мы видим, что ARCamera также предоставляет встроенные функции, однако, как мне правильно установить это значение в requestOptions?

Пока это должно быть примерно так:

ARCamera *camera = _cameraPreview.session.currentFrame.camera;
NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];
// How to put camera.intrinsics here?
requestOptions[VNImageOptionCameraIntrinsics] = camera.intrinsics;

1 Ответ

0 голосов
/ 03 сентября 2018

Как Джованни , упомянутое в комментариях, преобразование UIDeviceOrientation в CGImagePropertyOrientation исключает необходимость использования VNImageOptionCameraIntrinsics:

Utils.m

+(CGImagePropertyOrientation) getOrientation {
    CGImagePropertyOrientation orientation;
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
            orientation = kCGImagePropertyOrientationRight;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = kCGImagePropertyOrientationLeft;
            break;
        case UIDeviceOrientationLandscapeLeft:
            orientation = kCGImagePropertyOrientationUp;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = kCGImagePropertyOrientationDown;
            break;
        default:
            orientation = kCGImagePropertyOrientationRight;
            break;
    }
    return orientation;
}

ViewController.mm

- (void)captureOutput {
    ARFrame *frame = self.cameraPreview.session.currentFrame;
    CVPixelBufferRef pixelBuffer = frame.capturedImage;

    CGImagePropertyOrientation deviceOrientation = [Utils getOrientation];
    NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];

    VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCVPixelBuffer:pixelBuffer orientation:deviceOrientation options:requestOptions];

    [handler performRequests:@[[self request]] error:nil];
}
...