我正在构建一个将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;发布于 2018-09-02 21:29:20
正如Giovanni在注释中提到的,将UIDeviceOrientation转换为CGImagePropertyOrientation避免了使用VNImageOptionCameraIntrinsics的需要
+(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;
}- (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];
}https://stackoverflow.com/questions/52140731
复制相似问题